I would like to get notified via email, whenever an ec2 instance is created/terminated in my AWS account. How can I achieve this?
问题:
回答1:
The easiest method (IMO) to do that is to use CloudWatch Events service along with an SNS topic configured to send out emails to your email ID.
In AWS web console, go to CloudWatch -> Events (On left panel) -> Rules -> 'Create rule'
In 'Event Source' choose 'Event Pattern'. Then choose 'Events By Service'. In front of 'Service Name' select 'EC2' and for 'Event Type', select 'EC2 Instance State-change Notification'. You can either choose 'Any State' to get a notification on every state-change or choose specific state you are interested in ('Running' and 'Terminated'|'Stopped' might be of your interest). On the right, Click 'Add target' and then choose 'SNS topic' from dropdown. Then choose an SNS topic that you have configured already to send notification emails to your email ID.
Hope this helps!
回答2:
I can think of 2 ways to solve this:
- Enable Cloudtrail and stream it to Cloudwatch. In Cloudwatch you create rules to send an SNS when an instance is created. There'll be some charges for Cloudtrail and Cloudwatch.
- Use Lambda: When CloudTrail creates an object in S3, trigger an event that causes a Lambda function to execute. The Lambda function then parses the S3 object and check for
RunInstances
event. If it findsRunInstances
, extracts other attributes of the instance and use AWS SES to send a mail.
Solution 1: AWS handles everything. Incurs a tiny AWS charge (Cloudwatch + SNS). Not much opportunity to know how things work.
Solution 2: From Cloudtrail onwards, you control the flow. Lambda and SES are practically free (Lambda executions are free up to certain limit, up to 200 emails are free in SES sandbox mode). Opportunity to learn about S3/Lambda/SES.