Writing April 12, 2023 · 4 min read

Manage AWS EC2 Instances from the Command Line Using Python and Boto3


Introduction

Managing Amazon Web Services (AWS) EC2 instances from the command line can be quite convenient. In this blog post, we’ll demonstrate how to create a simple Python script using the Boto3 library, which (among other things) allows you to manage your EC2 instances directly from the command line. The skeleton of this script can even be adapted to other kinds of AWS python utilities. Here’s a link to the GitHub repo if you want to take a closer look at the finished product.

Now let’s dive in.

Prerequisites

  • An AWS account with EC2 instances
  • Python 3 with pip installed on your local machine
  • AWS CLI installed and configured

Step 1: Installing the Boto3 library

To get started, you’ll first need to install the Boto3 library. This can be done using pip:

    
        
pip install boto3

    

Step 2: Creating the Python script

Create a new Python file called ec2_manager.py. We’ll add our code to this file.

Step 3: Importing required modules

We start by importing the necessary modules for our script:

    
        
import boto3
import argparse

    
  • boto3: The main library for interacting with AWS services.
  • argparse: To parse command-line arguments.

Step 4: Initializing the Boto3 session

In order to interact with AWS, we need to create a session using AWS credentials. There are a few ways to do this, but we’ll support two methods. The first is to allow the user to pass an AWS CLI config profile name and region to the application. The second is to use the local environment variables where the script is run. This will allow users with SSO credentials to use the script. The function below gives users the option of passing it a profile and region but will default to the environment variables if none are passed.

    
        
def start_session(profile=None, region=None):
   AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
   AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
   AWS_DEFAULT_REGION = os.environ.get('AWS_DEFAULT_REGION')
  
   try:
       aws_region = AWS_DEFAULT_REGION
       if region:
           aws_region = region

       if profile:
           boto_session = boto3.Session(
               profile_name=profile,
               region_name=aws_region
           )
       else:
           boto_session = boto3.Session(
               aws_access_key_id=AWS_ACCESS_KEY_ID,
               aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
               region_name=aws_region
           )
       return boto_session
   except botocore.exceptions.ClientError as error:
       print('Invalid profile: {}'.format(profile))
       raise error

    

Step 5: Creating an EC2 resource object

After creating the session, we’ll use it to create an EC2 resource object to interact with the EC2 instances.

    
        
ec2 = session.resource('ec2')

    

Step 6: Defining functions for managing EC2 instances

We’ll define four functions to perform the following actions on our EC2 instances:

  1. List all instances
  2. Start an instance
  3. Stop an instance
  4. Terminate an instance

These functions use the EC2 resource object to interact with instances in the target account:

    
        
def list_instances():
    ...

def start_instance(instance_id):
    ...

def stop_instance(instance_id):
    ...

def terminate_instance(instance_id):
    ...

    

Step 7: Parsing command-line arguments

We’ll use the argparse module to parse command-line arguments. This makes our script user-friendly and improves ease of use. This also allows our script to come with a built-in manual that users can access via the –help flag.

    
        
def parse_arguments():
   parser = argparse.ArgumentParser(description='Manage EC2 instances.')
   parser.add_argument('-l', '--list', action='store_true', help='List all instances')
   parser.add_argument('-s', '--start', type=str, help='Start an instance with given ID')
   parser.add_argument('-p', '--stop', type=str, help='Stop an instance with given ID')
   parser.add_argument('-t', '--terminate', type=str, help='Terminate an instance with given ID')
   parser.add_argument('-pr', '--profile', type=str, help='Specify an AWS CLI config profile.')
   parser.add_argument('-r', '--region', type=str, help='Specify the AWS region where the command will run.')

   return parser.parse_args()

    

Step 8: Running the script

Finally, we’ll use the if name == ‘main’: construct to run our script. This ensures that the script will only execute when called directly, not when imported as a module. This function also handles the logic of our argument parser, routing the script to the correct function based on the provided flags.

    
        
if __name__ == '__main__':
   args = parse_arguments()

   if args.profile and args.region:
       session = start_session(args.profile, args.region)
   elif args.profile:
       session = start_session(args.profile)
   else:
       session = start_session()

   # Create an EC2 resource object
   ec2 = session.resource('ec2')

   if args.list:
       list_instances(ec2)
   elif args.start:
       start_instance(ec2, args.start)
   elif args.stop:
       stop_instance(ec2, args.stop)
   elif args.terminate:
       terminate_instance(ec2, args.terminate)
   else:
       print("No arguments provided. Use '-h' or '--help' for usage information.")

    

Conclusion

In this blog post, we’ve demonstrated how to create a simple Python script using the Boto3 library to manage EC2 instances from the command line. This script can be easily modified and expanded to include additional functionality as needed. By understanding the concepts and code presented here, you’ll be well-equipped to manage your AWS resources efficiently and effectively.

Farewell, Cloud Surfers!

All writing Start a conversation