Automation of Aws instances using python sdk

Boto is python sdk in amazon, which can be used for automation purpose for ec2,s3 etc.

Installation: Boto can be installed using using pip or offline. for pip the command:    pip install boto3

Offline installation we need to download the offline bundle and install, it has the dependency , download and install it.

Configuration : Before we use boto we need to set the configuration, which can be done using : aws configure

Alternatively, you can create the credential file yourself. By default, its location is at ~/.aws/credentials:

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

You may also want to set a default region. This can be done in the configuration file. By default, its location is at ~/.aws/config:

[default]
region=us-east-1

Alternatively, you can pass a region_name when creating clients and resources.

Lets see some the sample code:

import boto3

ec=boto3.resource(‘ec2’)

if we print ec, it shows “ec2.ServiceResource()”

Just give help(ec),it will show up the complete description what we can do ,what methods are available. Below example we want to check all the instance status

# Boto 3
#!/usr/bin/python
import boto3
ec=boto3.resource('ec2')
#help(ec)
#check the status of all the ec2 instances
for status in ec.meta.client.describe_instance_status()['InstanceStatuses']:
    print(status)