Hi All,
I have codecommit repository on AWS and this codecommit repo have two notification rule(codecommit-notify-rule1 and codecommit-notify-rule2 ) with same lambda target. Then i have two more SNS rule(mail-notify-rule1 and mail-notify-rule2) with Email target. So whenever changes happen on codecommit then notification rule trigger the lambda fucntion. Im using lambda function with python 3.8(boto3). My question, So whenever trigger the lambda function then it will check both notification rule (codecommit-notify-rule1 and codecommit-notify-rule2 ) by if condition or anything. So "if codecommit-notify-rule1 match then it will call mail-notify-rule1" whereas "if codecommit-notify-rule2 match then it will call mail-notify-rule2". How to do with python3.8(boto3)? I hope you people understand my problem.
import json
import boto3
codecommit = boto3.client('codecommit')
def lambda_handler(event, context):
#Log the updated references from the event
#references = { reference['ref'] for reference in event['Records'][0]['codecommit']['references'] }
print("References: " + str(event))
message = event['Records'][0]['Sns']['Message']
mesageJson = json.loads(message)
referencename = mesageJson['detail']['referenceName']
user = mesageJson['detail']['callerUserArn']
snsmessage = "Branch : "+referencename+"\n"+"User : "+user
topics = sns.get_all_topics()
topic_list = topics['ListTopicsResponse']['ListTopicsResult']['Topics']
topic_names = [t['TopicArn'].split(':')[5] for t in topic_list]
if 'notification-1' in topic_names:
#try:
sns = boto3.client('sns')
sns.publish(
TopicArn="arn:aws:sns:xxxxxxxxx:Sample-Notification",
Subject="Test",
Message=str(snsmessage)
)
return ('Sent a message to an Amazon SNS topic.')
elif 'notification-2' in topic_names:
# try:
sns = boto3.client('sns')
sns.publish(
TopicArn = "arn:aws:sns:yyyyyyyyy:lambda-notification",
Subject="Test SNS Model",
Message=str(snsmessage)
)
else:
return none
#except Exception as e:
#print(e)
#print('Error getting repository {}. Make sure it exists and that your repository is in the same region as this function.'.format(repository))
#raise e
Whenever i trying to run lambda function it thrown error like below
References: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
[ERROR] KeyError: 'Records'
Kindly check my code and reply if any correction. Thanks in advance.