AWS Automation using Python(BOTO3)

AWS Automation using Python(BOTO3)

·

3 min read

Boto3: Introduction

Boto3 is the AWS SDK for Python, which provides APIs to interact with AWS services. It has the following key features:

Boto3 has two types of APIs:

  • Client APIs: Low-level APIs that map to HTTP operations.

  • Resource APIs: Higher-level APIs that expose resource objects.

The resource APIs make the code more Pythonic.

for i in ec2.instances.all():
    if i.state['Name'] == 'stopped':
        i.start()

Here we are using the EC2 resource API to start stopped EC2 instances.

Boto3's interfaces are dynamically generated from JSON models of AWS APIs. This allows Boto3 to quickly update as AWS services change.

Boto3 supports both Python 2.7+ and Python 3.4+.

Boto3 has "waiters" that automatically poll for status changes in AWS resources. For example, you can start an EC2 instance and use a waiter to wait until it reaches the "running" state.

Boto3 has features tailored for specific AWS services, like:

  • Multi-part uploads for Amazon S3

  • Condition expressions for Amazon DynamoDB

In summary, Boto3 provides:

  • High-level resource APIs for a more Pythonic experience

  • Up-to-date interfaces that align with AWS API changes

  • Support for both Python 2 and 3

  • "Waiters" that make waiting for resource status changes easier

  • Service-specific features that simplify using individual AWS services.

Pip is the package installer for Python.

sudo apt update
sudo apt install python3-pip

Boto3 is the AWS SDK for Python, which provides APIs to interact with AWS services.

pip install boto3

Now, to connect Boto3 to AWS we need to do some configurations and setting up some credentials

aws configure

Here, you need to add your credentials for the AWS account and you can find those credentials in the AWS account settings

AWS Access Key ID [****************5OX7]:
AWS Secret Access Key [****************LM6b]:
Default region name [us-east-1]:
Default output format [json]:

Now, in our mini project, we will describe the list of all VPCs available in our AWS account First we will create a new folder in which we will write some Python code for describing VPCs

Now after successful Installation create a new folder and open it on your favourite code editor in our demo we will use VS Code

mkdir python-auto
cd python-auto
#to open vs code editor
code .

Now create a new file main.py and paste the code inside the file

import boto3
ec2_client = boto3.client('ec2')

all_available_vpcs = ec2_client.describe_vpcs()
print(all_available_vpcs)

Now, if you open vs code terminal and write:

python3 main.py

all the active VPCs will be printed inside your terminal

Now if you want to get a specific parameter of vpc like CIDR block, VPC ID, owner ID, etc we can do that using for loop

import boto3
ec2_client = boto3.client('ec2')

all_available_vpcs = ec2_client.describe_vpcs()
vpcs = all_available_vpcs["Vpcs"]
for vpc in vpcs:
print(vpc[VpcId])

Now, if you again run the file you will see that VPC Id will be printed inside your terminal

That's a wrap................................