Quick Start with Boto3 and AWS

In this post, we’ll take a look at some basics examples of using Python scripts to interact with the infrastructure provided by Amazon Web Services (AWS).

Take in count if you want to replicate this with your own AWS account, you will need:

  • An AWS account with admin or power user privileges.
  • Internet connection and Linux shell environment.
  • Some experience with Python and Bash command line interface.
  • An EC2 instance with Python, Boto3 and AWS CLI tool configured. You can use the Amazon Linux AMI which has all these packets.

After you setup your environment you can check is all is configured running the following command:

_config.yml

Where the yellow part is the instance ID of your EC2 instance.

Examples of how to use some basics scripts:

List Instances:


_config.yml

Results:

_config.yml

Create an Instance:


_config.yml

Results:

_config.yml

_config.yml

Terminate an Instance:


_config.yml

In this case the ID is our input for the terminate_instance.py script.

_config.yml

Results:

_config.yml

Checking status of the instances:


_config.yml

Results:

_config.yml

Create a Bucket:


_config.yml

For this case we need to define the name of the bucket due to the restrictions and limitations of S3 (Bucket names must be globally unique and, Bucket names must follow DNS naming conventions).

_config.yml

_config.yml

Just playing…


There are a lot of ways to create different scripts, and every person will create and adapt the scripts based on their necessity. As last example here is one that I’ve made just playing with this:

_config.yml

This script will create an instance and wait until change the state to running, then create one file with the details of the instance and also it’s uploaded to our S3 bucket.

_config.yml

_config.yml

_config.yml

In conclusion boto3 can help you if you want to automate your AWS environment, in my particularly opinion it’s a great way to maintain your cloud environment well managed.

Link for the python scripts: HERE!

Thanks for read!!!

Stormtroopocat

Written on October 17, 2024