Email notification through SNS and Lambda

Email notification through SNS and Lambda



I am facing an issue. My main motive is to send an email whenever there is state change happened to ec2 instances.



I tried cloud watch events directly with SNS and its work also but the email template which I am receiving is not having the proper information to understood.



I was expecting Server name and its IP in the email template which SNS does not give me the option to modify it. So What I am thinking is to involve lambda so that



Let me know if you think this is correct method for what I am expecting or not. and give some insights on how to get Lambda in between Cloud watch and SNS



Thanks & Regards




1 Answer
1



As shown in the Amazon CloudWatch Events console, a sample event triggered by an instance state change is:



"version": "0",
"id": "7bf73129-1428-4cd3-a780-95db273d1602",
"detail-type": "EC2 Instance State-change Notification",
"source": "aws.ec2",
"account": "123456789012",
"time": "2015-11-11T21:29:54Z",
"region": "us-east-1",
"resources": [
"arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111"
],
"detail":
"instance-id": "i-abcd1111",
"state": "pending"




CloudWatch events can then directly trigger an AWS Lambda function, passing in this information.



The Lambda function can use the Instance ID to retrieve further details about the instance (eg server name, IP address).



The function can then either:



Using SNS would be the easiest, if you don't mind the text-based content.



Here is some sample code that will receive an event from Amazon CloudWatch Events when an instance changes state, then send a message to an Amazon SNS topic with further details:


import boto3

def lambda_handler(event, context):

# Extract Instance ID from event
instance_id = event['detail']['instance-id']

# Obtain information about the instance
ec2_client = boto3.client('ec2')
instance_info = ec2_client.describe_instances(InstanceIds=[instance_id])
instance = instance_info['Reservations'][0]['Instances'][0]

# Extract name tag
name_tags = [t['Value'] for t in instance['Tags'] if t['Key']=='Name']
name = name_tags[0] if name_tags is not None else ''

# Send message to SNS
MY_SNS_TOPIC_ARN = 'arn:aws:sns:ap-southeast-2:123456789012:foo'
sns_client = boto3.client('sns')
sns_client.publish(
TopicArn = MY_SNS_TOPIC_ARN,
Subject = 'Instance Change State: ' + instance_id,
Message = 'Instance: ' + instance_id + ' has changed staten' +
'State: ' + instance['State']['Name'] + 'n' +
'IP Address: ' + instance['PublicIpAddress'] + 'n' +
'Name: ' + name
)



To setup:






Thank you @John for your directions. as I am not a coder and finding it difficult to write it down some lambda functions for this purpose. can you please help to get one such functions or any link which you saw having the same methodology so that I can use and tweak it accordingly. that would be helpful.

– Linux For Everyone
Sep 6 '18 at 20:43






Working code added. Python 3.6.

– John Rotenstein
Sep 6 '18 at 23:14






Thank you @John, I will try this code. one more thing I would like to clarify. The lambda function will need IAM permission to send data to SNS and to fetch data from cloudwatch ? Thanks & Regards

– Linux For Everyone
Sep 7 '18 at 23:07







It will only need permission for EC2 describe_instances and SNS publish. The information from CloudWatch is automatically sent when the Lambda function is invoked, so it does not need to call CloudWatch.

– John Rotenstein
Sep 8 '18 at 0:32


describe_instances


publish






Thank you @John for your help and inputs, your code worked for me :) your lambda code did 80% of my work. can you please suggest how can I master lambda functions, any link, pdf online which I can refer to learn. Thanks & Regards

– Linux For Everyone
Sep 8 '18 at 1:32



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế