Last modified: Jan 01, 2026 By Alexander Williams
Monitor AWS with Python Scripts Guide
Cloud infrastructure needs constant oversight. Manual checks are slow and error-prone. Python scripts offer a powerful, automated solution.
This guide shows you how to monitor AWS resources using Python. We will use the Boto3 library to interact with AWS services.
Why Use Python for AWS Monitoring?
Python is simple and readable. The Boto3 SDK provides full AWS API access. This combination is perfect for automation.
Scripts can run on schedule via cron or AWS Lambda. They provide real-time insights into your cloud environment.
Automated monitoring saves time and reduces risk. It helps you catch issues before they affect users.
Setting Up Your Python Environment
First, ensure Python is installed. Then, install the Boto3 library using pip. You will also need AWS credentials configured.
Credentials can be set via the AWS CLI or environment variables. This allows your script to authenticate with AWS.
pip install boto3
aws configure
For a deeper dive into Boto3 setup, see our guide on Getting Started with AWS SDK for Python Boto3.
Monitoring Key AWS Services
Let's explore scripts for core AWS services. Each script focuses on a specific monitoring task.
1. Check EC2 Instance Health
EC2 instances are virtual servers. You need to know their state and performance. This script lists all instances and their status.
import boto3
def check_ec2_instances():
"""Fetches and prints the state of all EC2 instances."""
# Create an EC2 client
ec2_client = boto3.client('ec2')
# Describe all instances
response = ec2_client.describe_instances()
print("EC2 Instance Status Report:")
print("-" * 30)
for reservation in response['Reservations']:
for instance in reservation['Instances']:
instance_id = instance['InstanceId']
state = instance['State']['Name']
# Get the instance name tag if it exists
name_tag = "N/A"
for tag in instance.get('Tags', []):
if tag['Key'] == 'Name':
name_tag = tag['Value']
break
print(f"ID: {instance_id} | Name: {name_tag} | State: {state}")
return response
if __name__ == "__main__":
check_ec2_instances()
EC2 Instance Status Report:
------------------------------
ID: i-0a1b2c3d4e5f67890 | Name: Web-Server-01 | State: running
ID: i-1b2c3d4e5f67890a1 | Name: DB-Backup | State: stopped
This script uses the describe_instances method. It is a fundamental Boto3 call for EC2 data. You can extend it to check CPU usage or disk space.
For more EC2 automation, read Automate AWS EC2 Management with Python Boto3.
2. Monitor S3 Bucket Size and Objects
S3 stores data in buckets. Monitoring size and object count is crucial for cost control. This script calculates bucket metrics.
import boto3
from datetime import datetime, timezone
def monitor_s3_bucket(bucket_name):
"""Calculates total size and object count for a given S3 bucket."""
s3_client = boto3.client('s3')
paginator = s3_client.get_paginator('list_objects_v2')
total_size = 0
total_objects = 0
print(f"Scanning bucket: {bucket_name}")
# Paginate through all objects
for page in paginator.paginate(Bucket=bucket_name):
if 'Contents' in page:
for obj in page['Contents']:
total_size += obj['Size']
total_objects += 1
size_in_mb = total_size / (1024 * 1024)
print(f"Total Objects: {total_objects}")
print(f"Total Size: {size_in_mb:.2f} MB")
return total_objects, total_size
if __name__ == "__main__":
# Replace with your bucket name
monitor_s3_bucket('my-monitoring-bucket-example')
Scanning bucket: my-monitoring-bucket-example
Total Objects: 1247
Total Size: 548.76 MB
The script uses the list_objects_v2 paginator. This handles buckets with many objects. It's essential for tracking storage costs.
Learn more about S3 operations in Manage AWS S3 Buckets with Python Boto3.
3. Track RDS Database Performance
Amazon RDS manages relational databases. Monitoring connections and storage is key. This script fetches CloudWatch metrics.
import boto3
from datetime import timedelta, datetime, timezone
def get_rds_metrics(db_instance_id):
"""Fetches CPU and connection metrics for an RDS instance from CloudWatch."""
cloudwatch = boto3.client('cloudwatch')
# Set time range for metrics (last 1 hour)
end_time = datetime.now(timezone.utc)
start_time = end_time - timedelta(hours=1)
# Get CPU Utilization metric
cpu_response = cloudwatch.get_metric_statistics(
Namespace='AWS/RDS',
MetricName='CPUUtilization',
Dimensions=[{'Name': 'DBInstanceIdentifier', 'Value': db_instance_id}],
StartTime=start_time,
EndTime=end_time,
Period=300, # 5-minute intervals
Statistics=['Average']
)
# Get Database Connections metric
conn_response = cloudwatch.get_metric_statistics(
Namespace='AWS/RDS',
MetricName='DatabaseConnections',
Dimensions=[{'Name': 'DBInstanceIdentifier', 'Value': db_instance_id}],
StartTime=start_time,
EndTime=end_time,
Period=300,
Statistics=['Average']
)
print(f"Metrics for RDS Instance: {db_instance_id}")
print(f"Time Range: {start_time} to {end_time}")
print("\nCPU Utilization (%):")
for datapoint in cpu_response['Datapoints']:
print(f" {datapoint['Timestamp']}: {datapoint['Average']:.2f}")
print("\nDatabase Connections:")
for datapoint in conn_response['Datapoints']:
print(f" {datapoint['Timestamp']}: {datapoint['Average']:.0f}")
return cpu_response, conn_response
if __name__ == "__main__":
get_rds_metrics('my-production-db')
Metrics for RDS Instance: my-production-db
Time Range: 2023-10-27 09:15:00+00:00 to 2023-10-27 10:15:00+00:00
CPU Utilization (%):
2023-10-27 09:30:00+00:00: 12.50
2023-10-27 09:45:00+00:00: 15.80
Database Connections:
2023-10-27 09:30:00+00:00: 45
2023-10-27 09:45:00+00:00: 52
This uses the CloudWatch get_metric_statistics method. It retrieves performance data for analysis. You can add alarms for high CPU usage.
Automating and Scheduling Scripts
Running scripts manually is not efficient. Automation is the goal. You can use cron jobs on a server.
AWS Lambda is a better serverless option. It runs code without managing servers. You can trigger it on a schedule with CloudWatch Events.
Packaging a script for Lambda requires dependencies. The script must be structured as a handler function.
import boto3
import json
def lambda_handler(event, context):
"""Main function for AWS Lambda execution."""
ec2_report = check_ec2_instances()
# Add logic to send report via SNS or store in S3
return {
'statusCode': 200,
'body': json.dumps('Monitoring check completed.')
}
For deploying such scripts, see Deploy Python Apps on AWS Lambda Guide.
Best Practices for AWS Monitoring Scripts
Follow these tips for robust monitoring. They prevent common pitfalls.
Use IAM Roles with Least Privilege. Never use admin credentials. Create a custom IAM policy for your script.
Implement Error Handling. Use try-except blocks. Log errors for debugging.
Add Logging. Use the Python logging module. Send logs to CloudWatch Logs.
Set Up Alerts. Use Amazon SNS to send email or SMS. Trigger alerts when metrics breach thresholds.
Schedule Regular Runs. Consistency is key. Monitor during peak and off-peak hours.
Conclusion
Python and Boto3 simplify AWS monitoring. You can track EC2, S3, RDS, and more. Automated scripts provide timely insights.
Start with the basic scripts in this guide. Expand them to suit your needs. Add alerting and logging for production use.
Monitoring is a continuous process. Regular updates to your scripts will keep your AWS environment healthy and cost-effective.