Launch an instance with preconfigured alarm

0

I want to create an alarm when the CPU Utilization is greater than 90%. I can do this using console or command line or SDK if the instance is already created.

How can I do this automatically when an EC2 instance is launched first time (say as part of autoscaling group)

I tried doing via cloud formation template but could not do so.
Also, I tried to bake the alarm configurtion in the AMI but could not do so.

Chid
asked 5 years ago682 views
3 Answers
0
Accepted Answer

Hello
when creating the auto-scaling launch configuration, you can set the user data with the CLI command to create the alarm. The user data gets executed when the instance is created

https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_CreateLaunchConfiguration.html
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html#user-data-api-cli

hope this helps
RT

rtt
answered 5 years ago
0

Hello
for Cloudformation, you can create the instance and the alarm in the same template. Here is the template on how to create the alarm notice that you need the instance ID which shouldnt be a problem if you have both in the same template
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-cloudwatch.html#cloudwatch-sample-cpu-utilization-alarm

the problem with the autoscale group is that the alarm requires the actual instance ID which it cannot get directly. Here is something i found online in where the alarm is created in the userdata
https://stackoverflow.com/questions/25002240/aws-cloudwatch-alarm-on-startup-of-new-ec2-instance
I havent tried but it might work. there is also some discussion on that page.

hope this helps,
RT

rtt
answered 5 years ago
0

Thank you for your reply. I already had a look at the link.

There are two suggestions

Run a script when an instance is created. This can be done, if I launch the instance, but I am not sure that it would work if the autoscaling group creates new instance (until I trigger the script by a CloudWatch alert when an instance is added/removed).

Second suggestion was to bake the alert into the AMI. I am not sure how this can be done. I can add custom metrics to the base AMI but not sure how I can add custom alerts

Chid
answered 5 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions