How Do You Write a Python Program to Show the Status of the Servers in AWS?

Problem scenario
You want to list the statuses of each EC-2 instance in AWS using Python. How do you do this for a given region in AWS (e.g., us-west-1)?

Solution
Prerequisites
This assumes you have installed Boto3. If you do not know how, see this posting "How do you install Boto 3 on a RHEL server in AWS?"

Procedures
Use this program:

# Usage instructions 
# 1. replace "aaa111" with the aws_access_key_id of your account
# 2. replace "bbb222" with the aws_secret_access_key of your account
# If you are not sure how to find these credentials do the following:
#  To find the AWS Access Key ID and AWS Secret Access Key, in the AWS console, click on your name in the upper right hand corner. 
# Then click on "My Security Credentials."  Click "Create New Access Key"  Click "Show Access Key." 
#  3.  Replace us-west-1 with the region that you want to list Ec-2 instances for.
#  To run it, call it test3.py and run it with a command like this: "python test3.py"

import boto3

ec2 = boto3.resource('ec2', aws_access_key_id='aaa111', aws_secret_access_key='bbb222', region_name='us-west-1')

for status in ec2.meta.client.describe_instance_status()['InstanceStatuses']:
    print(status)

As a reference, you may go here to learn more about Boto's features.

Leave a comment

Your email address will not be published. Required fields are marked *