How to check if an EC2 instance is up and running/one particular service is up or down using boto3. The instance is created from a newly created AMI.
-
What do you mean by "one particular service is up or down"? What service is it? If this was a normal computer (not on EC2), how would you check whether this service is up or down? – John Rotenstein Feb 20 '20 at 06:48
-
if i run a script, it will returns the Apache service is running, failed, down etc. Otherwise i want to ssh into the newly launched instance and send a mail with subject "Successfully logged in". – AWS Coder Feb 26 '20 at 04:53
-
Since Apache is a web server, you can attempt to go to a page served by the instance. While the operating system and Apache are starting up, it will not serve content. Then, once Apache is running, it will be able to serve the requested web page. You would need to write your own software to do this. Alternatively, if the instance is in a Load Balancing Target Group, then the Load Balancing service performs this type of check for you before delivering traffic to the instance. – John Rotenstein Feb 26 '20 at 04:57
3 Answers
You can uses the following Instance:
ec2 = boto3.resource('ec2')
instance = ec2.Instance('instance_id')
print(instance.image_id)
Compare instance.image_id
to the id of image you expect.
To check if instance is already running, use EC2.Waiter.InstanceRunning waiter.

- 215,873
- 14
- 235
- 294
To check if an EC2 instance is up or down:
import sys
import boto3
ec2 = boto3.client('ec2')
if sys.argv[1] == 'ON':
response = ec2.monitor_instances(InstanceIds=['INSTANCE_ID'])
else:
response = ec2.unmonitor_instances(InstanceIds=['INSTANCE_ID'])
print(response)
To check the health of an instance:
# Boto 2.x
for status in ec2_connection.get_all_instance_statuses():
print(status)
# Boto 3
for status in ec2.meta.client.describe_instance_status()['InstanceStatuses']:
print(status)
For more details refer to: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/migrationec2.html
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/ec2-example-managing-instances.html
Update: (To check if particular service in EC2 is running or not)
- You can publish a custom metric to CloudWatch in the form of a "heart beat".
- Have a small script running via cron on your server checking the process list to see whether my_service is running and if it is, make a
put-metric-data
call to CloudWatch. - The metric could be as simple as pushing the number "1" to your custom metric in CloudWatch.
- Set up a CloudWatch alarm that triggers if the average for the metric falls below 1
- Make the period of the alarm be >= the period that the cron runs e.g. cron runs every 5 minutes, make the alarm trigger if it sees the average is below 1 for two 5 minute periods.
- Make sure you also handle the situation in which the metric is not published (e. g. cron fails to run or whole machine dies). you would want to setup an alert in case the metric is missing. (see here:AWS Cloudwatch heartbeat alarm)
- Be aware that the custom metric will add an additional cost of 50c to your AWS bill (not a big deal for one metric - but the equation changes drastically if you want to push hundred/thousands of metrics - i.e. good to know it's not free as one would expect)
- You have to create a topic in SNS (Simple Notification Service) service in AWS. There you have to define your email under SNS on which you will receive the notification.
References:

- 1,565
- 2
- 13
- 28
-
How to run a script to check a service is up or not inside the instance?? – AWS Coder Feb 20 '20 at 06:05
-
@PrejithPrasannan There is no python specific way to check this using boto3. You have to set this up using AWS Cloudwatch. Have updated the answer – Karthick Mohanraj Feb 20 '20 at 06:18
While you ask specifically about boto3, there is a way to ensure some service is in ready state more AWS natively.
Move creation of your instance to CloudFormation template and define CreationPolicy
, like described in AWS Blog post:
https://aws.amazon.com/blogs/devops/use-a-creationpolicy-to-wait-for-on-instance-configurations/
TL;DR: when the instance is launched, it will run a script which will check if whatever service you need is working, and then signal CloudFormation "all systems go". If it does not signal in time, instance is marked as CREATE_FAILED
and deleted.

- 2,911
- 10
- 22