Lambda function to mail notification using if condition

0 votes
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.
Jul 14 in AWS by Lakshminarayanan
• 700 points

edited Jul 14 by Lakshminarayanan 100 views

1 answer to this question.

+1 vote
Hi,

It seems you are passing the wrong key in your code. You have used the Records key in your code, but it is not available. Check this key name properly.
answered Jul 15 by MD
• 42,420 points
You are exactly right bro. Now i have corrected my code. Its running successfully. My message body contains like "snsmessage = "Branch : "+referencename+"\n"+"User : "+user". How can we get codecommit url in lambda handler? Is there any possible to get much details on message body?
Check the attribute list. You can able to find it.
Hi bro, i have checked , But i cant find what exactly. Can you share me any doc or find solution. I really need to add message body with codecommit actions "like commit , pullrequest" and need to add url (if we create pull request then will create one url). Kindly let me know bro.

I am not able to find any document regarding this. But like in EC2 instance we can find the metadata of the instances. In the same way, we may find that also.