in scripts/CheckDmsReplicationStatus.py [0:0]
def lambda_handler(event, context):
print(event)
str_subject = event['Records'][0]['Sns']['Subject']
if 'APPROVAL NEEDED' in str_subject:
print('This is a Codepipeline approval action')
str_sns = event['Records'][0]['Sns']['Message']
sns_msg = json.loads(str_sns)
pipeline = sns_msg['approval']['pipelineName']
stage = sns_msg['approval']['stageName']
action = sns_msg['approval']['actionName']
token = sns_msg['approval']['token']
approve_param ="pipelineName='%s',stageName='%s',actionName='%s',token='%s'" % ( pipeline , stage , action , token)
print(approve_param)
response = ssm.put_parameter(Name=ssm_parameter,
Value=approve_param,
Type='String',
Overwrite=True
)
elif 'DMS' in str_subject:
print('This is a message from DMS')
str_sns = event['Records'][0]['Sns']['Message']
if 'attempt' in str_sns:
print(str_sns)
print('Event notification nothing will be done')
else:
sns_msg = json.loads(str_sns)
print(sns_msg['Event Message'])
dms_status = sns_msg['Event Message']
if 'STOPPED_AFTER_FULL_LOAD' in dms_status:
print('DMS task replication is stopped after full load, proceeding to put an approval in Codepipeline')
result_pipeline('Approved')
elif 'started' in dms_status:
print('Lambda will do nothing at this step as the task is started')
elif 'Create' in dms_status:
print('Lambda will do nothing at this step as the task is created')
elif 'FAIL' in dms_status.upper():
status = 'DMS task failed. Please check the task'
print(status)
subj = 'Status Update on DMS Task ' + os.environ['dms_task']
sns.publish(TopicArn = topic, Message = status, Subject = subj)
result_pipeline('Rejected')
else:
status = 'DMS task did not stop or errored out after full load. Please check the task'
print(status)
subj = 'Status Update on DMS Task ' + os.environ['dms_task']
sns.publish(TopicArn = topic, Message = status, Subject = subj)
result_pipeline('Rejected')
else:
print('This message is from neither Codepipeline Approval or DMS event. Nothing will be done')