Sending SNS notification when an instance is stopped - amazon-web-services

At some point in a day, one of my instances gets stopped and I am trying to send an SNS notification at that time.
I can do it in multiple ways:
1.) Create an alarm on the metric StatusCheckFailed_Instance and when this reaches Insufficient_Data state, i configure it to send an SNS notification.
2.) I can write a lambda script (put_metric_alrm) to create it for me and send it when state changes to Insufficient_Data.
My problem while doing this is the notification is being sent once a day if my instance is stopped for days. Instead, I want a notification to be sent only when it gets stopped.
How can I do this? Please suggest.

Take a look at CloudWatch Events; you can create a new 'rule' such that an event will trigger on EC2 instance state-change notifications for a given instanceid where the state is 'stopped'. When the event is fired, you can then invoke an SNS topic to get notified of the change.
Here is an example configuration:
Service Name: EC2
Event Type: EC2 Instance State-change notification
Specific state(s): stopped
Specific instances: Your Instance ID
Targets: Your SNS Topic Name
The same example, as seen in the CloudWatch Events console:
Simply subscribe your SMS or email to the SNS topic, and you'll get notified when this instance enters the stopped state.
See also:
AWS API Documentation - PutRule
AWS CLI Documentation - aws events put-rule

Related

Creating an alarm for aws cloud watch when instance gets stopped/rebooted

I have been trying to create cloudwatch alarm when instance gets stopped but couldn't find direct way. From event subscriptions i can send notification when instance gets stopped. Is there any way cloud watch alarm can be triggered for the same.
Amazon EventBridge can be configured to trigger an event when a state change occurs on an EC2 instance. Use:
Event source: EC2
Event type: EC2 Instance State-change Notification
You can setup a target for the event to be a Lambda
The Lambda can API_PutMetricData for a metric you create. You can setup a CloudWatch Alarm on this metric
This tutorial shows you how to setup the EventBridge rule and the Lambda.

python lambda code for aws ec2 gets stopped

could any one please help me the lambda code , whenever AWS Ec2 instances get stopped, we need to get the email notifications with sns. In the email we need instance name. I could able to get instance id but not the instance name.
AWS CloudTrail allows you to identify and track EC2 instance lifecycle API calls (launch, start, stop, terminate). See How do I use AWS CloudTrail to track API calls to my Amazon EC2 instances?
And you can trigger a Lambda function to run arbitrary code when CloudTrail logs certain events. See Triggering a Lambda function with AWS CloudTrail events.
You can also create an Amazon CloudWatch alarm that monitors an Amazon EC2 instance and triggers a Lambda via CloudWatch Events.
You can create a rule in Amazon CloudWatch Events that:
Triggers when an instance enters the Stopped state
Sends a message to an Amazon SNS Topic
Like this:
If you want to modify the message that is being sent, then configure the Rule to trigger an AWS Lambda function instead. Your function should:
Extract the instance information (eg InstanceId) from the event parameter
Call describe-instances to obtain the Name of the instance (presumably the Tag with a Key of Name)
Publish a message to the Amazon SNS Topic

How to enable cloud watch alarm / event rule for AWS spot interruption notification?

Hi came to know that I can enable AWS cloud watch alarm for AWS EC2 spot intance if there is intrputtion for termination notice, here is more details
Now CloudWatch users can setup a rule that automatically sends the EC2
Spot two-minute warning to an SNS topic to get a push notification.
I have no clue how to setup SNS topic to get intrputtion for termination notice?
Create an SNS topic and subscribe to this topic to get notifications.
Create a CloudWatch Event Rule to trigger an action whenever a spot instance is terminated.
Configure your event as shown in the screenshot below.
Select SNS Topic as the target and enter the ARN of the topic you created.

Is there a EC2 terminated and or Running event that I can publish to SNS topic?

I want to run an AWS lambda function that is triggered by an SNS topic that publishes when an EC2 instance state is changed to running or terminated.
All that I can find online is relating to Autoscaling group events. Are there events that can publish to SNS when an instance is terminated or created?
You can create a Cloudwatch event rule for that particular case.

Get Email notifications when ec2 instance in terminated

I need to receive notifications whenver my instance in terminated. I know it can be done by cloudtrail and then using sns and sqs to get email for it, if you receive event of termination.
Is there a simpler way to do that ?
Any solution will is appreciated, but I prefer is doing using boto.
While it is not possible to receive a notification directly from Amazon EC2 when an instance is terminated, there are a couple of ways this could be accomplished:
Auto Scaling can send a notification when an instance managed by Auto Scaling is terminated. See: Configure Your Auto Scaling Group to Send Notifications
AWS Config can also be configured to send a Simple Notification Service (SNS) notification when resources change. This would send many notifications, so you would need to inspect and filter the notifications to find the one(s) indicating an instance termination. See the SNS reference in: Set Up AWS Config Using the Console and Example Amazon SNS Notification and Email from AWS Config.
Amazon Simple Notification Service (SNS) can also push a message to Amazon Queueing Service (SQS), which can be easily polled with the boto python SDK.
Receiving notifications via CloudTrail and CloudWatch Logs is somewhat messier, so I'd recommend the AWS Config method.
Now AWS introduced "rules" Under "Events" in AWS CloudWatch. In your case, you can select EC2 as Event Selector and SNS or SQS as Targets.
https://aws.amazon.com/blogs/aws/new-cloudwatch-events-track-and-respond-to-changes-to-your-aws-resources/
According to the AWS doc: Spot Instance Interruptions, it is possible to pool the instance-metadata in order to get an approximation of the termination time. You can build any custom monitoring solution around that.
> curl http://169.254.169.254/latest/meta-data/spot/instance-action
{"action": "stop", "time": "2017-09-18T08:22:00Z"}
If the instance is not scheduled for termination a http:400 will be returned.