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'] == '/cloudabilty')
print('body-json:accountId', re.match("^[0-9]{12}$", event['body-json']['accountId']))
except Exception as e:
print(e)
print("regex not matching any values passed in request")
raise Exception({"code": "4000", "message": "ERROR: Bad request"})
# VPC DNS logic
if event['context']['resource-path'] == '/cloudability' and \
re.match("^[0-9]{12}$", event['body-json']['accountId']):
requestId = str(uuid.uuid4())
accountId = event['body-json']['accountId']
stage = event['stage-variables']['stage']
# Check if account already exists
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 = {"Records": [{"Sns": {"Message": message}}]}
# Call Lambda
awslambda.invoke(
FunctionName='talr-cloudability-' + stage,
InvocationType='Event',
Payload=json.dumps(payload),
)
return {"code": "2020", "message": "Request Accepted", "requestId": requestId}
else:
raise Exception({"code": "4000", "message": "ERROR: Bad request"})
评论列表
文章目录