def handler(event, context):
log.debug("Received event {}".format(json.dumps(event)))
accountInfo = dynamodb.Table(os.environ['TAILOR_TABLENAME_ACCOUNTINFO'])
try:
print('context:resource-path', event['context']['resource-path'] == '/cloudtrail/{accountId}')
print('path:accountId', re.match("^[0-9]{12}$", event['params']['path']['accountId']))
except Exception as e:
print(e)
print("regex not matching any values passed in request")
raise Exception({"code": "4000", "message": "ERROR: Bad request"})
# Payload processing logic
if event['context']['resource-path'] == '/cloudtrail/{accountId}' and \
re.match("^[0-9]{12}$", event['params']['path']['accountId']):
requestId = str(uuid.uuid4())
accountId = event['params']['path']['accountId']
stage = event['stage-variables']['stage']
# Check if account is known to Tailor
getAccountId = accountInfo.scan(
ProjectionExpression='accountId, accountEmailAddress',
FilterExpression=Attr('accountId').eq(accountId)
)
if getAccountId['Count'] == 0:
print("Account not found")
raise Exception({"code": "4040", "message": "ERROR: Not found"})
elif int(getAccountId['Count']) > 0:
# Update accountInfo with new requestId
accountInfo.update_item(
Key={
'accountEmailAddress': getAccountId['Items'][0]['accountEmailAddress']
},
UpdateExpression='SET #requestId = :val1',
ExpressionAttributeNames={'#requestId': "requestId"},
ExpressionAttributeValues={':val1': requestId}
)
# Build Lambda invoke payload
message = {"requestId": requestId,
"accountId": accountId,
"accountEmailAddress": getAccountId['Items'][0]['accountEmailAddress']}
payload = {"message": message}
# Call Lambda
awslambda.invoke(
FunctionName='talr-cloudtrail-' + stage,
InvocationType='Event',
Payload=json.dumps(payload),
)
return {"code": "2020", "message": "Request Accepted", "requestId": requestId}
else:
raise Exception({"code": "4000", "message": "ERROR: Bad request"})
评论列表
文章目录