I have an autoscaling group created by CloudFormation. When a scale out or scale in event occurs, I have configured an SNS topic to trigger a lambda function. Everything works as expected, except when I delete my CloudFormation stack.
When I delete my CloudFormation stack (I use short-lived stacks for integration testing), the autoscaling group is deleted and the instances enter the Terminating:Wait stage as expected. But, the autoscaling:EC2_INSTANCE_TERMINATING lifecycle hook is never called (neither the Lambda monitoring nor the CloudWatch logs show any evidence of the lifecycle hook getting called). The autoscaling group appears to wait for the heartbeat timeout to expire, then deletes the instances and the autoscaling group.
Is there a way I can have the autoscaling:EC2_INSTANCE_TERMINATING lifecycle hook called when the EC2 instances are terminated because the ASG is deleted?
I figured this out. In my case I had a AWS::Lambda::Permission resource which granted SNS permission to invoke my lambda function. The permission was being deleted before the Autoscaling group so the SNS topic did not have permission to invoke my lambda function when the message arrived at the SNS topic.
Adding a DependsOn attribute to my ASG so it depends on the permission object solved this.
Related
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.
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
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.
I have an AWS auto scaling group. From the instances I collect a variety of metrics and placed some cloud watch alarms on these metrics. In specific scenarios I would like to add a cloud watch alarm action that terminates the entire auto scaling group. Is this possible? I am going over aws documentation but does not seem to be possible.
Thanks!!
You can do this by invoking Lambda from your custom Cloudwatch event
You will need to write a Lambda that can use STS to assume a role that permits it to issue an EC2 Terminate command
The workflow would be:
Cloudwatch event triggers
Lambda function is invoked
Lambda function assumes role via STS
Lambda function retrieves list of instances in the ASG
Lambda function cycles through instances, issuing termination commands
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.