Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Automated EC2 & RDS Rightsizing

Automate Amazon EC2 and Amazon RDS rightsizing using EventBridge Scheduler, AWS Lambda and Terraform.

Not every workload requires the same capacity 24/7. This solution automatically upgrades or downgrades EC2 instances and RDS databases based on scheduled dates and times, helping reduce AWS costs while ensuring sufficient capacity when needed.


Architecture

Architecture


Features

EC2 Rightsizing

  • Scheduled EC2 instance upgrades
  • Scheduled EC2 instance downgrades
  • Multiple EC2 instance support
  • Recurring schedules
  • One-time event schedules
  • Date-range schedules
  • Automated stop, resize and start workflow

RDS Rightsizing

  • Scheduled RDS database upgrades
  • Scheduled RDS database downgrades
  • Multiple database support
  • Recurring schedules
  • One-time event schedules
  • Date-range schedules
  • Automated database class modification

Operational Features

  • Event-driven architecture
  • Serverless automation
  • Terraform Infrastructure as Code
  • CloudWatch logging
  • SNS notifications
  • Configuration-driven deployment

Scheduling Use Cases

The following examples use the configured schedule_timezone.

Daily Business Hours

Time Action
08:00 AM Upgrade
07:00 PM Downgrade

Weekly Schedule

Schedule Action
Every Wednesday 08:00 AM Upgrade
Every Wednesday 06:00 PM Downgrade

One-Time Event

Date Action
2026-11-25 Upgrade
2026-12-01 Downgrade

Date-Range Schedule

Configuration Value
Start Date 2026-06-01
End Date 2026-11-30
Upgrade Every Wednesday 08:00 AM
Downgrade Every Wednesday 06:00 PM

Scheduled Capacity Expansion

Date Action
November 1 Increase Capacity
January 5 Restore Normal Capacity

Prerequisites

  • AWS Account
  • Terraform >= 1.10, < 2.0
  • AWS CLI configured

Repository Structure

automated-ec2-rds-rightsizing/
├── terraform/
│
│   ├── modules/
│   │   ├── naming/
│   │   ├── iam/
│   │   ├── sns/
│   │   ├── lambda/
│   │   └── eventbridge/
│   │
│   ├── main.tf
│   ├── variables.tf
│   ├── outputs.tf
│   ├── providers.tf
│   ├── versions.tf
│   └── terraform.tfvars.example
│
├── lambda/
│   ├── ec2_resizer.py
│   └── rds_resizer.py
│
├── diagrams/
│   └── architecture.png
│
└── README.md

Example Configuration

Recurring Weekly Schedule

mobile_app_server = {

  instance_id = "i-0123456789abcdef0"

  upgrade_type   = "t3.large"
  downgrade_type = "t3.small"

  upgrade_schedule = {
    expression = "cron(0 8 ? * WED *)"
    start_date = "2026-06-01T00:00:00Z"
    end_date   = "2026-11-30T23:59:59Z"
  }

  downgrade_schedule = {
    expression = "cron(0 18 ? * WED *)"
    start_date = "2026-06-01T00:00:00Z"
    end_date   = "2026-11-30T23:59:59Z"
  }
}

One-Time Special Event (EC2)

mobile_app_server = {

  instance_id = "i-0abcdef1234567890"

  upgrade_type   = "c6i.2xlarge"
  downgrade_type = "t3.medium"

  upgrade_schedule = {
    expression = "at(2026-11-25T06:00:00)"
  }

  downgrade_schedule = {
    expression = "at(2026-12-01T00:00:00)"
  }
}

One-Time Special Event (RDS)

mobile_app_database = {

  db_identifier = "mobile-app-mysql"

  upgrade_class   = "db.r6g.xlarge"
  downgrade_class = "db.t3.medium"

  upgrade_schedule = {
    expression = "at(2026-07-15T00:00:00)"
  }

  downgrade_schedule = {
    expression = "at(2026-07-16T00:00:00)"
  }
}

Timezone

The solution supports configurable EventBridge Scheduler timezones.

Example:

schedule_timezone = "Asia/Singapore"

Schedules run according to the configured timezone allowing automation to align with local business hours.


Deployment

Initialize Terraform:

terraform init

Validate configuration:

terraform validate

Review planned changes:

terraform plan

Deploy infrastructure:

terraform apply

Remove infrastructure:

terraform destroy

License

MIT License

About

Automated EC2 and RDS rightsizing using Terraform, AWS Lambda, EventBridge Scheduler and SNS.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages