Automates the start/stop lifecycle of EC2 and RDS resources using AWS Lambda and EventBridge. Terraform provisions the required infrastructure while Python Lambda functions perform the actions through Boto3.
.
├── lambda_layer.tf # Lambda Layer configuration and local build setup
├── ec2_scheduler.tf # EC2 scheduler module instantiation
├── rds_scheduler.tf # RDS scheduler module instantiation
├── codebuild.tf # S3 bucket configuration for artifacts
├── build_layer.sh # Local build script for Lambda Layer
├── requirements.txt # Python dependencies for the Lambda Layer
└── modules/ # Reusable Terraform modules
├── ec2_scheduler/ # EC2 automation
└── rds_scheduler/ # RDS automation
-
Automated Scheduling
- Start/stop EC2 instances based on tags
- Start/stop RDS instances based on tags
- Configurable schedule using cron expressions
-
Infrastructure as Code
- Terraform for provisioning and management
- Modular design for easy maintenance
- Local build process for Lambda Layer
-
Monitoring & Logging
- CloudWatch Logs for all Lambda executions
- Detailed logging for troubleshooting
- Terraform >= 1.3
- AWS CLI configured with appropriate permissions
- Python 3.9+ for local development
- AWS credentials configured with sufficient permissions
-
Clone the repository
git clone https://github.com/your-repo/aws-scheduler.git cd aws-scheduler
-
Install Python dependencies
pip install -r requirements.txt
-
Make the build script executable
chmod +x build_layer.sh
The Lambda Layer is built locally using the build_layer.sh
script. This script:
- Creates a temporary directory for the layer
- Installs dependencies from
requirements.txt
- Creates a ZIP archive of the dependencies
To manually build the layer:
./build_layer.sh
-
Initialize Terraform
terraform init
-
Review the execution plan
terraform plan
-
Apply the configuration
terraform apply
Terraform will automatically build the Lambda Layer and deploy all resources.
Variable | Description | Default |
---|---|---|
AWS_REGION |
AWS region to deploy resources | eu-west-1 |
github_repo_url |
GitHub repository URL | https://github.com/your-repo/aws-scheduler.git |
- EC2 Instances: Tag your instances with
stopstart = enabled
- RDS Instances: Tag your instances with
stopstart = enabled
Default schedule (configurable in Terraform variables):
- Start: 13:00 local time
- Stop: 18:00 local time
- CloudWatch Logs:
/aws/lambda/dev-ec2-scheduler-to-start
/aws/lambda/dev-ec2-scheduler-to-stop
/aws/lambda/dev-rds-scheduler-to-start
/aws/lambda/dev-rds-scheduler-to-stop
To remove all resources:
terraform destroy
State is stored in an S3 bucket with a DynamoDB table for locking. Update the backend
configuration in versions.tf
with your bucket and table names before running terraform init
.
- Terraform – explore modules, variables and outputs to customize deployments.
- AWS EventBridge & Lambda – learn how cron expressions work in UTC and how to manage permissions.
- Python/Boto3 – check the methods used in the Lambda functions (
describe_instances
,stop_instances
,start_instances
, etc.). - Pre-commit – enable the hooks in
.pre-commit-config.yaml
for formatting and documentation generation.
Name | Version |
---|---|
terraform | >= 1.3 |
aws | ~> 4.0 |
No providers.
Name | Source | Version |
---|---|---|
ec2_scheduler_default | ./modules/ec2_scheduler | n/a |
rds_scheduler_default | ./modules/rds_scheduler | n/a |
No resources.
Name | Description | Type | Default | Required |
---|---|---|---|---|
region | AWS region to deploy resources | string |
"us-east-1" |
no |
Name | Description |
---|---|
ec2_scheduler_lambda_arns | ARNs of the EC2 scheduler Lambda functions |
rds_scheduler_lambda_arns | ARNs of the RDS scheduler Lambda functions |