Boto3 is the Amazon Web Services (AWS) SDK for Python. It allows you to create, configure, and manage AWS services directly from your Python scripts.
1. Getting Started
Before you start, you must install boto3 and configure your AWS credentials using the AWS CLI.
Action:
pip install boto3
aws configure
# Follow the prompts to enter your access keys and region2. S3 (Simple Storage Service)
S3 is the most common service used with Boto3 for storing backups and artifacts.
Listing Buckets
Action:
import boto3
# Create an S3 client
s3 = boto3.client('s3')
# List all buckets
response = s3.list_buckets()
print("Available Buckets:")
for bucket in response['Buckets']:
print(f" - {bucket['Name']}")Result:
Available Buckets:
- production-backups
- logs-archive
- app-assets-20263. EC2 (Elastic Compute Cloud)
You can manage your virtual servers programmatically using the EC2 client.
Describing Instances
Action:
import boto3
ec2 = boto3.client('ec2')
# Get all running instances
response = ec2.describe_instances(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]
)
for reservation in response['Reservations']:
for instance in reservation['Instances']:
print(f"ID: {instance['InstanceId']}, Type: {instance['InstanceType']}")Result:
ID: i-0a1234567890abcdef, Type: t3.medium
ID: i-0b9876543210fedcb, Type: t2.micro4. Resource vs Client
Boto3 provides two ways to interact with AWS:
- Client: Lower-level service access (returns dictionaries).
- Resource: Higher-level, object-oriented access (easier for most tasks).
Using Resource (Object-Oriented)
Action:
import boto3
# Use the higher-level resource
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket('production-backups')
print(f"Bucket Name: {bucket.name}")
print(f"Objects in bucket:")
for obj in bucket.objects.limit(2):
print(f" - {obj.key}")Result:
Bucket Name: production-backups
Objects in bucket:
- backup-2026-04-01.sql
- backup-2026-04-02.sqlSummary
- Clients are mapped 1:1 with AWS APIs.
- Resources provide an abstraction that feels more like "Python code."
- Credentials should never be hardcoded; use
aws configureor environment variables. - Install with
pip install boto3.