Tech Blog

Cloud Computing

AWS Automation with Python Boto3

By Keerthik Shenoy3 min read
← Back to blog

Automation plays a major role in In Cloud and DevOps, we have many tools to automate manual processes like Infrastructure automation with Terraform, for CICD Jenkins, GitLab, AWS, code pipeline, etc. In the same way, we can automate some of the AWS tasks with a simple python script using the oto3 library.

Let’s see some basic as well as advanced examples of python scripts to automate some day-to-day AWS tasks.

If we check some background details of boto3, it uses the AWS SDK for Python to interact with AWS services and send requests to AWS API endpoints.

First, you have to install python and boto3 library on your laptop also AWS CLI should be configured.

  1. Download and install the latest version of python https://www.python.org/downloads/
  2. Install pip by running command python get-pip.py
  3. Install boto3 by running command pip install boto3

To configure aws cli: <url>

Example 1:

This is a simple and basic script to start, stop and reboot ec2 instances

import boto3


ec2 = boto3.client('ec2',region_name='ap-south-1') 
#Add your region


instances = ["<instance-id>"] 
# or ["<instance-id-1>","<instance-id-2>",...,"<instance-id-n>"]

#start the instance
ec2.start_instances(InstanceIds=instances)

#stop the instance
ec2.stop_instances(InstanceIds=instances)

#reboot the instance
ec2.reboot_instances(InstanceIds=instances)

Example 2:

In this example, we will list ec2 instances from all regions with details in .csv format

Also, it will scan all configured AWS profiles automatically and use those credentials and create one .csv file with ec2 details.

import csv
import jmespath
import boto3
import itertools
import configparser
import os
 
# Get list of profiles
config = configparser.ConfigParser()
path = os.path.join(os.path.expanduser('~'), '.aws/credentials')
config.read(path)
profiles = config.sections()
 
# Get list of regions
ec2_client = boto3.client('ec2')
regions = [region['RegionName']
            for region in ec2_client.describe_regions()['Regions']]
 
# Get list of EC2 attributes from all profiles and regions
myData = []
for profile in profiles:
    for region in regions:
        current_session = boto3.Session(profile_name = profile, region_name = region)
        client = current_session.client('ec2')
        response = client.describe_instances()
        output = jmespath.search("Reservations[].Instances[].[NetworkInterfaces[0].OwnerId, InstanceId, InstanceType, \
            State.Name, Placement.AvailabilityZone, PrivateIpAddress, PublicIpAddress, KeyName, [Tags[?Key=='Name'].Value] [0][0]]", response)
        myData.append(output)
 
# Write myData to CSV file with headers
output = list(itertools.chain(*myData))
with open("ec2-inventory-latest.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerow(['AccountID','InstanceID','Type','State','AZ','PrivateIP','PublicIP','KeyPair','Name'])
    writer.writerows(output)

To execute this script open cmd on script path and run

python <your_script_name.py>

These are some examples of boto3 but we can automate hundreds of aws tasks using boto3, also we can use boto3 in the Lambda function. 

To understand more about boto3 you can refer to the official documentation.
https://boto3.amazonaws.com/v1/documentation/api/latest/index.html

Keep reading

You may be interested in

Browse all articles →
How to Configure AWS CLI?
December 30, 20224 min read

How to Configure AWS CLI?

In Cloud platforms such as AWS, GCP, and Azure we have multiple ways to access cloud services and our resources like GUI (Console), CDK, and CLI. In today’s blog, we are going to look at CLI access in AWS. Like all other cloud providers, AWS also provides CLI access to their services for that first […]

By Keerthik ShenoyRead More →
AWS Cost Optimization Techniques
December 15, 20224 min read

AWS Cost Optimization Techniques

AWS is the world’s leading cloud computing platform, providing a wide range of services including computer computing, storage, networking, and platform as a service (PaaS). AWS cost optimization requires identifying areas for cost-saving measures, tracking your usage, and putting advice into practice. Your Amazon costs are largely influenced by the cost of EC2 instances provided […]

By Keerthik ShenoyRead More →
Terraform As a DevOps Tool (IaC) And Benefits (Part 2)
September 21, 20222 min read

Terraform As a DevOps Tool (IaC) And Benefits (Part 2)

In this article, we will explore terminologies and basic commands that can be used to interact with Terraform. Using Terraform init, an existing or new Terraform configuration can be initialized. As a beginner, you may get syntax errors or indentation errors. To validate theYAML data, use the Terraform “validate” command. The “Terraform apply“ command changes […]

By Utsav SolankiRead More →