Skip to main content

Manage AWS EC2 Instances Using Python Boto3 – Complete Automation Guide

Hey Welcome! back to Automations with Python for AWS!! 

Amazon Elastic Compute Cloud (EC2) is one of the most widely used AWS services for hosting applications and workloads in the cloud.

While AWS Management Console provides a user-friendly interface, managing hundreds of EC2 instances manually becomes inefficient and error-prone. Python automation with Boto3 allows organizations to perform instance operations consistently, securely, and at scale.

Boto3 is the official AWS SDK for Python that enables developers and DevOps engineers to interact with AWS services through code. It provides APIs for launching, monitoring, stopping, rebooting, and terminating EC2 instances.


AWS EC2 instances managing with Python Boto3 SDK



What are we doing?

Have you ever logged into the AWS Console just to start, stop, or reboot EC2 instances and wondered if there was a faster way?

As DevOps Engineers, Cloud Administrators, and Automation Architects, we perform repetitive EC2 management tasks every day. Instead of manually navigating through the AWS Console, we can automate these operations using Python and the AWS SDK called Boto3.

In this article, we will learn how to manage AWS EC2 instances programmatically using Python and unlock the power of cloud automation.


What We'll Learn

In this post we will be exploring the AWS EC2 Instance related operations, and manage them in a reusable form. By the end of this article, you will be able to:

  • Configure AWS credentials for Python automation
  • Connect to AWS EC2 using Boto3
  • List EC2 instances and retrieve instance details
  • Start EC2 instances programmatically
  • Stop EC2 instances programmatically
  • Reboot EC2 instances using Python
  • Handle exceptions and permissions securely
  • Understand real-world DevOps automation use cases
  • Optimize AWS costs using scheduled automation
  • Build reusable cloud automation scripts

Why Use Python for EC2 Management?

1. Faster Operations

Execute actions on multiple EC2 instances within seconds instead of manually selecting instances through the console.

2. Reduce Human Errors

Automation ensures consistent execution and eliminates accidental operational mistakes.

3. Cost Optimization

Automatically stop development and testing servers during non-business hours and restart them when needed. This helps reduce unnecessary cloud spending.

4. Infrastructure Automation

Integrate EC2 management into GitLab CI/CD pipelines, operational workflows, and self-healing systems.

5. Better Scalability

Manage a few servers or thousands of instances using the same automation code.

Real-World DevOps Use Cases

Environment Scheduling

  • Start Development servers at 8 AM
  • Stop Development servers at 8 PM
  • Reduce AWS operational costs

Maintenance Windows

  • Reboot application servers automatically
  • Apply patches during approved maintenance periods

Disaster Recovery

  • Start standby servers during DR testing
  • Validate infrastructure readiness

Auto-Healing

  • Detect unhealthy instances
  • Trigger automated restart or recovery actions

Cloud Governance

  • Identify untagged instances
  • Generate compliance reports
  • Enforce operational standards

How do you Create EC2 instance using Python3 Boto3? 

 Creating EC2 instance using Boto3 Python code
#=============================================
# File : create_ec2.py
# Description: Create EC2 instance by Boto3

import boto3
ec2 = boto3.resource('ec2')

instances = ec2.create_instances(
        ImageId="ami-0dafa01c8100180f8",
        MinCount=1,
        MaxCount=1,
        InstanceType="t2.micro",
        KeyName="KeyPair1"
    )
    
Launch instance
import boto3
ec2_client = boto3.client('ec2')

# This function will requires 
# image_id [Required] based on the region this will be changing
# instance_type [optional] default t2.micro type otherwise you can provide
# max [optional] default as 1, you can provide maximum number of instances
def launch_instance(image_id, instance_type='t2.micro', max=1):
	resource = ec2_client.run_instances(ImageId=image_id,
					InstanceType=instance_type,
					MinCount=1, MaxCount=max)
									
	for instance in resource['Instances']:
		print(instance['InstanceId'])

# main program
launch_instance('yourami-id')
To control the ec2 instances individual AWS cli commsnd  test Start, Stop
ec2.start_instances 
ec2.stop_instances

and terminate the ec2 instance
List AWS EC2 Instances using python boto3 script
import boto3
ec2_client = boto3.client('ec2')
resp = ec2_client.describe_instances()
for reservation in resp['Reservations']:
	for instance in reservation['Instances']:
		print("Running Instance Image ID: {} Running instance Instance Type: {} Running Instance Keyname {}".format(instance['InstanceId'],instance['InstanceType'],instance['KeyName']))

Python code with Menu driven program to manage EC2 instances
import boto3
import time

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

## Display all instances
def display_instances():
    for instance in ec2.instances.all():
        print (instance.id , instance.state)

## Stop instance by given instance id
def stopinstance():    
    instanceid=input("Please enter instanceid:")
    response = ec2_client.stop_instances(InstanceIds=[instanceid])
    time.sleep(90)
    display_instances()

## Terminate instance by given instance id
def terminateinstance():    
    instanceid=input("Please enter instanceid:")
    response = ec2_client.terminate_instances(InstanceIds=[instanceid])
    time.sleep(90)
    display_instances()
    
## Start instance by given instance id
def startinstance():    
    instanceid=input("Please enter instanceid:")
    response = ec2_client.start_instances(InstanceIds=[instanceid])
    time.sleep(90)
    display_instances()

## Main Program
def main():
    while True:
        menu_list=["Display All Instance","Stop Instance","Start Instance","Terminate Instance","Exit"]
        i=1
        for item in menu_list:
            print (i,item)
            i=i+1
        choice=int(input("Please enter Menu Choice:"))
        if choice==1:
            display_instances()
        elif choice==2:
            stopinstances()
        elif choice==3:
            startinstances()
        elif choice==4:
            terminateinstance()            
        else:
            exit()

if __name__ == '__main__':
    main()        
 

Best Practices cloud Automations

1. Use IAM Roles Instead of Hardcoded Credentials

Avoid storing AWS Access Keys inside Python scripts.

Use:

  • IAM Roles
  • Instance Profiles
  • AWS SSO
  • Temporary Security Credentials

This improves security and aligns with AWS best practices.


2. Use DryRun Before Production Actions

Before stopping or rebooting instances, perform a DryRun operation to verify permissions and prevent accidental outages. AWS EC2 APIs support DryRun validation for many instance management actions.


3. Use Tags for Better Automation

Instead of hardcoding instance IDs:

Environment=Dev
Application=WebApp
Owner=DevOps

You can dynamically locate and manage resources using tags.

Major Benefits we can observe:

  • Easier maintenance
  • Better governance
  • Simplified automation

Benefits of Boto3-Based EC2 Automation

BenefitDescription
Faster ExecutionPerform operations in seconds
Cost SavingsAutomatically stop unused resources
ScalabilityManage hundreds of instances
ReliabilityConsistent execution every time
SecurityIAM-based access control
IntegrationWorks with Jenkins, GitLab, GitHub Actions
MonitoringCollect operational information automatically
ComplianceEnforce cloud governance policies

Reader Challenge

Try extending this script with the following features:

Beginner

  • List only running EC2 instances
  • Display instance Name tags

Intermediate

  • Start or stop instances based on tags
  • Generate CSV reports

Advanced

  • Schedule automatic shutdowns
  • Send notifications through SNS
  • Create self-healing workflows
  • Integrate with AWS Lambda and EventBridge 

Conclusion

Python and Boto3 provide a powerful combination for automating AWS EC2 operations. Whether you are a DevOps Engineer, Cloud Administrator, Site Reliability Engineer, or Automation Architect, EC2 automation helps reduce manual effort, improve reliability, and optimize cloud costs.

By leveraging Boto3 APIs, you can build scalable automation solutions for monitoring, scheduling, maintenance, compliance, and disaster recovery operations. As cloud environments continue to grow, automation becomes an essential skill for every modern infrastructure engineer.

Reader Question

What is the most useful EC2 automation you have implemented in your organization?
Share your experience in the comments and help fellow cloud engineers learn from real-world use cases.


Comments

Popular posts from this blog

AWS Auto Scaling using Python Boto3

Auto Scaling Group in AWS configure using Python Boto3  What is Auto Scaling means?  This is key capability or power of Cloud Computing Engineers believe in their skills on scaling abilities.  Amazon EC2 Auto Scaling helps to maintain application availability and lets automatically add or remove EC2 instances using scaling policies that we define.  There are 2 types of scaling policies : Dynamic or predictive. These scaling policies let us add or remove EC2 instances capacity to service established or real-time demand patterns. It contains various steps involved in Auto Scaling process using Python Boto3 we will explore every step that accumulate to form a complete automation solution for a DevOps project. ASG Groups associated with ELB and EC2 instances   Understanding AWS Auto scaling configuration steps Check any running instances Create launch configuration Configure ASG for Auto scaling Verify the configuration Disable Auto Scaling In order to setup A...

Program Controls and loops in Python

Python Programming controls The flow of program will be controlled with conditional statements where this flow  if condition if-else condition if-elif condition while loop while - else loop for loop for with if- else loop if condition Here I would like to work on program control statements, wherein relate some operating system functionalities so that, this would give some basic prototype for the sys admin scripts. #!/usr/bin/python # This illustrate if condition # Filename : ifex.py import os d=os.listdir('/home/pavanbsd/pybin') print d f='ifex.py' if f in d: print "File exists" else: print "Not found..." if-elif-else ladder No worries, coming soon... When you need the task that need to be done repeatedly then your choice is using loops. Python provides two simple loop constructs easy to use! More powerful then other scripting compare to, it has 'else' block. while loop Understand the power of while loop,...

Python Automations using Boto3 for AWS

The objective of this post is for experimenting with AWS Boto3 automations and AWS Lambda, most of the realtime usecase on AWS cloud controlled and acceleration with them. We should know as SRE or DevOps Engineer how to refer to the Boto3 documentation where AWS team provided lot of details and examples of each Boto3 method, I'm pretty sure you could make great automations with this Boto3 module explore ideas. Python for AWS Using Boto3 Let's jump on it... Prerequisites You must have AWS account [this can be your company provided or free-tire account]. Getting Started Python Automations using Boto3  Step 1: Add User in IAM  Let's get into the IAM adding user you can provide username as devops-admin Select AWS Access TYPE {tic} programmatic access this allows to access key ID and secret access key for the AWS API, CLI SDK (Boto3 uses this). Set permissions for 'devopsuser' Select policy type as 'AdministrationAccess' to access AWS resources and services [For...