Last modified: Jan 01, 2026 By Alexander Williams
AWS Task Scheduling with Python & CloudWatch
Automation is key in modern cloud operations.
AWS CloudWatch Events provides a powerful scheduler.
You can trigger tasks at specific times or intervals.
Combine it with Python and Boto3 for flexible automation.
This guide shows you how to set it up step by step.
Understanding AWS CloudWatch Events
CloudWatch Events is a serverless event bus service.
It delivers a near real-time stream of system events.
You can create rules to match incoming events.
These rules route events to one or more target services.
For scheduling, you use cron or rate expressions.
This is perfect for periodic tasks like data backups.
It is also great for report generation or cleanup jobs.
Prerequisites and Setup
First, you need an AWS account with appropriate permissions.
Install Python and the Boto3 library on your local machine.
Configure AWS credentials using the AWS CLI or IAM role.
You should have a basic understanding of IAM policies.
Our Getting Started with AWS SDK for Python Boto3 guide can help.
Ensure your IAM user has events:* and lambda:* permissions.
Creating a Scheduled Lambda Function
The most common target for a scheduled task is AWS Lambda.
Lambda runs your code without provisioning servers.
Let's create a simple Python function for our task.
This function will log a message and simulate a job.
We will then deploy it to AWS Lambda.
For more on deployment, see our Deploy Python Apps on AWS Lambda Guide.
# scheduled_task.py
import json
import boto3
from datetime import datetime
def lambda_handler(event, context):
"""
Main handler for the scheduled Lambda function.
Logs the execution time and performs a sample task.
"""
# Get current time
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"Scheduled task executed at: {current_time}")
# Example Task: List S3 buckets (ensure Lambda has s3:ListAllMyBuckets permission)
s3_client = boto3.client('s3')
try:
response = s3_client.list_buckets()
bucket_names = [bucket['Name'] for bucket in response['Buckets']]
print(f"Found {len(bucket_names)} S3 buckets.")
# You can add your real task logic here
# e.g., process data, send notifications, clean up resources
except Exception as e:
print(f"Error during task execution: {e}")
return {
'statusCode': 200,
'body': json.dumps('Task completed successfully!')
}
Package this code and its dependencies into a ZIP file.
Upload it to create a new Lambda function in the AWS Console.
Alternatively, use the AWS CLI or Boto3 to deploy it.
Building the Scheduler with Boto3
Now, we use Python and Boto3 to create the CloudWatch rule.
We will define a cron expression to run our task daily at 5 AM UTC.
The script creates the rule and adds the Lambda function as a target.
It also sets up the necessary IAM permissions for CloudWatch.
This allows CloudWatch to invoke the Lambda function.
# create_schedule.py
import boto3
import json
def create_scheduled_rule(rule_name, schedule_expression, lambda_function_arn):
"""
Creates a CloudWatch Events rule and links it to a Lambda target.
"""
client = boto3.client('events')
lambda_client = boto3.client('lambda')
# Step 1: Create the CloudWatch Events Rule
print(f"Creating rule: {rule_name}")
response = client.put_rule(
Name=rule_name,
ScheduleExpression=schedule_expression, # e.g., 'cron(0 5 * * ? *)'
State='ENABLED',
Description='Rule to trigger a daily task at 5 AM UTC'
)
print(f"Rule ARN: {response['RuleArn']}")
# Step 2: Add Lambda function as a target for the rule
print(f"Adding Lambda target: {lambda_function_arn}")
target_response = client.put_targets(
Rule=rule_name,
Targets=[
{
'Id': 'ScheduledLambdaTarget',
'Arn': lambda_function_arn,
}
]
)
print(f"Targets added: {target_response}")
# Step 3: Allow CloudWatch to invoke the Lambda function
# This creates a resource-based policy on the Lambda function
try:
permission_response = lambda_client.add_permission(
FunctionName=lambda_function_arn,
StatementId='AllowCloudWatchToInvoke',
Action='lambda:InvokeFunction',
Principal='events.amazonaws.com',
SourceArn=response['RuleArn']
)
print("Permission added to Lambda function.")
except lambda_client.exceptions.ResourceConflictException:
print("Permission already exists. Skipping.")
print("Scheduled rule setup complete!")
if __name__ == "__main__":
# Configure these variables
RULE_NAME = "Daily-5AM-Task"
# Cron expression: Minute, Hour, Day-of-month, Month, Day-of-week, Year
# This runs at 5:00 AM UTC every day
SCHEDULE_EXPRESSION = "cron(0 5 * * ? *)"
# Replace with your Lambda function's ARN
LAMBDA_ARN = "arn:aws:lambda:us-east-1:123456789012:function:MyScheduledTask"
create_scheduled_rule(RULE_NAME, SCHEDULE_EXPRESSION, LAMBDA_ARN)
# Example output when running the script
Creating rule: Daily-5AM-Task
Rule ARN: arn:aws:events:us-east-1:123456789012:rule/Daily-5AM-Task
Adding Lambda target: arn:aws:lambda:us-east-1:123456789012:function:MyScheduledTask
Targets added: {'FailedEntryCount': 0, 'FailedEntries': []}
Permission added to Lambda function.
Scheduled rule setup complete!
Managing and Monitoring Scheduled Tasks
After setup, you need to manage and monitor your tasks.
Use the CloudWatch console to view your rules.
You can see the rule's schedule and its targets.
CloudWatch Logs stores the output from your Lambda function.
Check the log group /aws/lambda/<your-function-name>.
This is where your function's print statements go.
You can also use Boto3 to disable, enable, or delete rules.
For broader monitoring strategies, read our Monitor AWS with Python Scripts Guide.
# manage_schedule.py
import boto3
def manage_rule(rule_name, action='disable'):
"""
Enables or disables a CloudWatch Events rule.
"""
client = boto3.client('events')
if action.lower() == 'disable':
client.disable_rule(Name=rule_name)
print(f"Rule '{rule_name}' disabled.")
elif action.lower() == 'enable':
client.enable_rule(Name=rule_name)
print(f"Rule '{rule_name}' enabled.")
else:
print("Action must be 'enable' or 'disable'.")
# Example usage
# manage_rule('Daily-5AM-Task', 'disable')
Best Practices and Common Use Cases
Follow best practices for reliable task scheduling.
Always set up proper error handling in your Lambda function.
Use CloudWatch Alarms to get notified of task failures.
Keep your Lambda functions idempotent where possible.
This means running them multiple times is safe.
Use tags on your rules for better cost management.
Common Use Cases:
1. Data Processing: Run ETL jobs nightly.
2. Resource Management: Start/stop non-production EC2 instances.
Learn more about this in our Automate AWS EC2 Management with Python Boto3 guide.
3. Application Health Checks: Ping your endpoints periodically.
4. Backup and Cleanup: Archive old files from S3 daily.
Conclusion
Scheduling tasks on AWS is straightforward with Python.
CloudWatch Events and Lambda form a powerful serverless duo.
You can automate routine operations without managing servers.
This improves efficiency and reduces operational overhead.
Start by automating a simple task like a daily report.
Then expand to more complex, event-driven workflows.
The combination of Boto3 and CloudWatch is very flexible.
It empowers developers to build robust, scheduled systems.