AWS Boto3 is the official Python SDK for interacting with Amazon Web Services (AWS). It allows developers to automate cloud tasks such as:
Creating EC2 instances
Managing S3 buckets
Deploying Lambda functions
Handling IAM roles and permissions
Automating CloudWatch monitoring
This guide covers installing Boto3, authenticating AWS access, and automating AWS resources using Python.
1. Installing Boto3 and Configuring AWS Credentials
1.1 Installing Boto3
First, install the Boto3 library:
pip install boto3
1.2 Setting Up AWS Credentials
Boto3 requires AWS credentials for authentication. You can configure them in multiple ways:
Option 1: Using AWS CLI (Recommended)
Run the following command:
aws configure
You’ll be prompted to enter:
AWS Access Key ID: <your_access_key>
AWS Secret Access Key: <your_secret_key>
Default region name: us-east-1
Default output format: json
This saves credentials in ~/.aws/credentials
.
Option 2: Using Environment Variables
export AWS_ACCESS_KEY_ID='your_access_key'
export AWS_SECRET_ACCESS_KEY='your_secret_key'
export AWS_DEFAULT_REGION='us-east-1'
Option 3: Using a Credentials File
Create ~/.aws/credentials
and add:
[default]
aws_access_key_id=your_access_key
aws_secret_access_key=your_secret_key
region=us-east-1
2. Automating AWS with Boto3
Boto3 has two interfaces:
Client (low-level) – Direct access to AWS APIs
Resource (high-level) – Object-oriented approach
3. Managing AWS Resources with Boto3
3.1 Creating and Managing EC2 Instances
Launching an EC2 instance
import boto3
ec2 = boto3.resource('ec2')
instance = ec2.create_instances(
ImageId='ami-12345678', # Replace with a valid AMI ID
InstanceType='t2.micro',
MinCount=1,
MaxCount=1,
KeyName='your-key-pair',
SecurityGroupIds=['sg-xxxxxxxx'],
SubnetId='subnet-xxxxxxxx'
)[0]
print(f'EC2 instance {instance.id} is launching...')
Stopping and Terminating an EC2 instance
ec2_client = boto3.client('ec2')
# Stop an instance
ec2_client.stop_instances(InstanceIds=['i-1234567890abcdef'])
# Terminate an instance
ec2_client.terminate_instances(InstanceIds=['i-1234567890abcdef'])
Fetching all running instances
instances = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
for instance in instances:
print(instance.id, instance.state)
3.2 Managing S3 Buckets
Creating an S3 bucket
s3 = boto3.client('s3')
s3.create_bucket(
Bucket='my-boto3-bucket',
CreateBucketConfiguration={'LocationConstraint': 'us-east-1'}
)
print("S3 bucket created successfully!")
Uploading a file to S3
s3.upload_file('localfile.txt', 'my-boto3-bucket', 'uploadedfile.txt')
Listing all S3 buckets
buckets = s3.list_buckets()
for bucket in buckets['Buckets']:
print(bucket['Name'])
Deleting an S3 bucket
s3.delete_bucket(Bucket='my-boto3-bucket')
3.3 Automating AWS Lambda Deployment
Deploying a Lambda function
lambda_client = boto3.client('lambda')
with open('lambda_function.zip', 'rb') as f:
lambda_code = f.read()
response = lambda_client.create_function(
FunctionName='myLambdaFunction',
Runtime='python3.8',
Role='arn:aws:iam::123456789012:role/lambda-role',
Handler='lambda_function.lambda_handler',
Code={'ZipFile': lambda_code}
)
print("Lambda function deployed:", response['FunctionArn'])
Invoking a Lambda function
response = lambda_client.invoke(
FunctionName='myLambdaFunction',
InvocationType='RequestResponse'
)
print(response['Payload'].read().decode())
Deleting a Lambda function
lambda_client.delete_function(FunctionName='myLambdaFunction')
3.4 Managing IAM Users and Roles
Creating an IAM user
iam = boto3.client('iam')
iam.create_user(UserName='newUser')
print("IAM user created!")
Attaching a policy to an IAM user
iam.attach_user_policy(
UserName='newUser',
PolicyArn='arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess'
)
Deleting an IAM user
iam.delete_user(UserName='newUser')
3.5 Monitoring AWS Services with CloudWatch
Fetching EC2 CPU utilization
cloudwatch = boto3.client('cloudwatch')
response = cloudwatch.get_metric_statistics(
Namespace='AWS/EC2',
MetricName='CPUUtilization',
Dimensions=[{'Name': 'InstanceId', 'Value': 'i-1234567890abcdef'}],
StartTime=datetime.utcnow() - timedelta(minutes=10),
EndTime=datetime.utcnow(),
Period=300,
Statistics=['Average']
)
print(response['Datapoints'])
Setting up an alarm for high CPU usage
cloudwatch.put_metric_alarm(
AlarmName='HighCPUUsage',
MetricName='CPUUtilization',
Namespace='AWS/EC2',
Statistic='Average',
Period=300,
EvaluationPeriods=2,
Threshold=80.0,
ComparisonOperator='GreaterThanThreshold',
AlarmActions=['arn:aws:sns:us-east-1:123456789012:MyTopic']
)