Python & Boto3 for AWS Automation
Part of the AWS Roadmap
In short
Clicking through the console to stop 30 test servers every evening gets old by day two. A 15-line Python script can do it in seconds, every day, without forgetting one.
Boto3 is the AWS SDK for Python. With a few lines you can list, create, tag or stop resources, which makes it the usual choice for automation scripts and Lambda functions.

Think of it like this
Washing machine, not bucket
Hand-washing clothes every day works, but it's tiring and you'll skip a day. A washing machine program does the same steps the same way every time while you drink chai. Python scripts are your cloud's washing machine.
Watch it happen
Stopping dev servers with boto3
describe_instances()
1/5Your script asks boto3 for running instances tagged env=dev.
The big ideas
What you need to know
SDKs do the heavy lifting
boto3 (AWS), google-cloud libraries (GCP) and azure-sdk (Azure) wrap every API so you call functions, not raw HTTP.
Credentials stay outside code
The SDK reads keys from your CLI profile, environment or the server's IAM role. Never paste keys into the script.
Tags make scripts safe
Filter by tags like env=dev so your script never touches production by accident.
Schedule it
Run it with cron, a CI job or as a Lambda on a timer, and the task happens even when you forget.
See it in code
Walk through the code
1import boto32 3ec2 = boto3.client("ec2", region_name="ap-south-1")4 5resp = ec2.describe_instances(6 Filters=[7 {"Name": "tag:env", "Values": ["dev"]},8 {"Name": "instance-state-name", "Values": ["running"]},9 ]10)11 12ids = [i["InstanceId"] for r in resp["Reservations"] for i in r["Instances"]]13 14if ids:15 ec2.stop_instances(InstanceIds=ids)16print(f"Stopped: {ids}")1/4Create an EC2 client for the Mumbai region. boto3 picks up your credentials on its own.
Install with pip install boto3. Credentials come from aws configure or the machine's IAM role, never from the code.
Save yourself the pain
Common mistakes
Oops
Hard-coding access keys in the script and pushing it to GitHub.
Do this instead
Use aws configure locally and IAM roles on servers. Leaked keys get found and abused fast.
Oops
Testing a delete or stop script on all resources first.
Do this instead
Print what you would change first (a dry run), filter by tags, and test on one throwaway resource.
Your mission
List before you act
Quick check
Where should a Python script running on an EC2 instance get its AWS credentials?
Go deeper
- 1Install boto3 in a virtual environment and list your S3 buckets.
- 2Write a script that stops every EC2 instance tagged Environment=dev.
- 3Read about boto3 clients versus resources and paginators.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.