Skip to Content

Automating AWS EC2 Management with Python and Boto3

Posted on October 29, 2022 by

Categories: AWS

Tags:

In this post, I’ll show how to use Python and the Boto3 Amazon Web Services Software Development Kit (SDK), which enables people who are familiar with Python programming to leverage the complex AWS REST APIs to manage their cloud resources. I will only be concentrating on the AWS Elastic Cloud Compute (EC2) service due to the size of the AWS REST API and related cloud services.

These are the subjects I’ll be talking about:

  • launch of an EC2 instance
  • putting an EC2 instance to rest
  • putting an EC2 instance to rest
  • generating an image to backup an EC2 instance
  • using an image to launch an EC2 instance

Setting up the environment and dependencies for AWS Lambda and scheduling backup and cleanup on a server

I must first establish a user with programmatic access to the REST APIs in my AWS account. Please be aware that I am giving this user admin permissions exclusively for the sake of making this tutorial. If you are following along, before employing this user in a production setting, check your company’s IT security regulations.

Step 1: I must navigate to the IAM part of the services menu in my AWS interface. From there, I must select the Users link and then the Add User option to arrive at the screen below. Before clicking the next button, I give the user the name “boto3-user” and tick the option for programmatic access.

As seen below, I click the Attach current policies directly tile on the permissions page, check the box for AdministratorAccess, and then click the next button.

Since I am not adding any optional tags, go to the next step by clicking.

After reviewing the soon-to-be-created user, I click Create user.

Lastly, I save the credentials that I downloaded as a CSV file.

The essential Python 3 libraries must then be locally installed in a virtual environment as follows:

  • Python: $ python -m venv
  • venv/bin/activate at $ source (venv)
  • $ pip install pprint, boto3, and awscli

Then, using the awscli library, I configure the credentials for the boto3 library, making sure to include the credentials for the Access Key and Secret Key I downloaded in step 5 above.

  • Region name by default [None]:
  • Output format by default [None]:
  • Making a Work-Ready EC2 Instance

In this section, I’ll explain how to launch an EC2 client using the active session object and build a boto3 session unique to a certain AWS region. I will then communicate with the EC2 instances controlling startup, shutdown, and termination in that area using the EC2 boto3 client.

I use these procedures to build an EC2 instance for this article:

I access the EC2 Dashboard by clicking the EC2 link in the Services menu, and then I click the Launch Instance button in the centre of the screen.

I select the Amazon Linux AMI by clicking the Select button next to it on the Choose Amazon Machine Image (AMI) page.

Select the Review and Launch option after accepting the default t2.micro instance type.

I expand the Tags area on the Review page and click Edit Tags to add the tags Name and BackUp. After that, I click Launch Review and Launch Again to return to the Review page, and then I click Launch to launch the instance.