Python scripting on AWS: Introduction to Boto3

Python scripting on AWS: Introduction to Boto3

Introduction

When applying for different DevOps, Cloud, and System administrator roles, you are required to know one scripting language: Python. When working with cloud providers like AWS, having good knowledge of Python will help you automate some major manual processes. This saves you and your company time, energy, and of course money.

One of the major libraries used for Python scripting on AWS is the Boto3 Library. Boto3 library is the AWS SDK for Python that helps developers to write software that uses AWS packages like s3, ec2, and the rest.

The Boto3 library is built and maintained by the AWS library. The SDK comprises two packages, the first one is the Botocore Library. This library provides the right functionality between the Python SDK and the AWS CLI.

The second package is the Boto3 package for implementing the Python SDK within your app.

Getting Started

Install Python

Before installing the Boto library, you must check your python package. Install Python 3.7 or later. Any support for previous versions of Python is depreciated.

Install Boto3

The best way to install the Boto3 is by using the pip package. The code is written below as thus;

pip install boto3

If your python project requires a specific version of Boto3, you can install it by signifying the version you'd like to work with. An example is shown below.

pip install boto3==1.0.0

AWS configuration

Before working on the Boto3 package, you should set up the right credentials for your AWS account. if you have an existing account, you can either choose that user account or create a new one.

If you have the AWS CLI installed on your system, use the was configure command to configure the necessary files.

aws configure

When you put in this command, AWS will require you to put in your access key and your secret key. The output will be displayed as seen below:

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

You may also need to add a region to the configure. This setup will look like this

[default]
region=us-west-1

You can use any region of your choice. This is the region for my configuration.

Using the Boto3 Library

Using the AWS EC2 client

Just like any other library, to use the Boto3 library, you need to import it into your Python file. We will name our python file main.py, and import the library using the following command.

import boto3

When using the Boto3 library, there are several clients that you can initiate to work with different parts of the AWS infrastructure. However, in this project, we will be looking at the AWS ec2 instance and how to create a client to access it with the Boto3 library.

To create a client, you run the following commands

ec2_client = boto3.client('ec2', region_name="us-west-1")

Describing VPCs

Within your ec2 instance, there will be vpcs installed. When working on your project using the Boto3 library, one simple way to access the details of your VPC is by using the describe_vpcs() function.

all_available_vpcs = ec2_client.describe_vpcs()

There are so many commands and functions you can use when working with the AWS ec2 client. A lot of these commands can be found here for better referencing.

For this article, we will only focus on getting the parameter, and reading the results of the VPC we had installed.

To print the output of your VPCs, run the print(all_available_vpcs) commands. A dictionary containing your VPC parameters will appear on your terminal in the format below.

'Vpcs': [
        {
            'CidrBlock': 'string',
            'DhcpOptionsId': 'string',
            'State': 'pending'|'available',
            'VpcId': 'string',
            'OwnerId': 'string',
            'InstanceTenancy': 'default'|'dedicated'|'host',
            'Ipv6CidrBlockAssociationSet': [
                {
                    'AssociationId': 'string',
                    'Ipv6CidrBlock': 'string',
                    'Ipv6CidrBlockState': {
                        'State': 'associating'|'associated'|'disassociating'|'disassociated'|'failing'|'failed',
                        'StatusMessage': 'string'
                    },
                    'NetworkBorderGroup': 'string',
                    'Ipv6Pool': 'string'
                },
            ],
            'CidrBlockAssociationSet': [
                {
                    'AssociationId': 'string',
                    'CidrBlock': 'string',
                    'CidrBlockState': {
                        'State': 'associating'|'associated'|'disassociating'|'disassociated'|'failing'|'failed',
                        'StatusMessage': 'string'
                    }
                },
            ],
            'IsDefault': True|False,
            'Tags': [
                {
                    'Key': 'string',
                    'Value': 'string'
                },
            ]
        },
    ],
    'NextToken': 'string'

This result contains a Dictionary 'Vpcs' with a key-value pair of all the params and their types provided by your vpcs.

To pick out a particular key-value for your project, eg the VpcId, you use a for loop to loop through all key-values in the dictionary.

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

Create VPCs using Boto3

In case VPCs are not defined, you need to create a new Vpc on your ec2 client. To create this VPc, you define a new resource as follow

ec2_resource = boto3.resource('ec2', region_name="us-west-2")

new_vpc = ec2_resource.create_vpc(
      CidrBlock="10.0.0.0/16"
)

new_vpc.create_subnet(
      CidrBlock="10.0.1.0/24"
)

new_vpc.create_subnet(
      CidrBlock="10.0.2.0/24"
)
new_vpc.create_tags(
      Tags=[
    {
        'Key': 'Name',
        'Value': 'my-vpc'
    }, 
  ]
)

Once you run the code, a VPC with the following Cidr Blocks will be created. The codes above shows you the simple way to create an ec2 instance with different VPCs and Subnets.

The Boto3 library makes AWS integrations easy. You can create ec2 instances, create and delete Cidr blocks, Create, and delete s3 buckets, and working with the EBS (Elastic block Storage).

Conclusion

Congratulations, you now know how to work with Python on your AWS infrastructure. The next time, you need to using a part of AWS on your Python project, the Boto3 library should be the best bet.

Resources

Boto3 Documentation here