This Terraform module automates the scheduling of AWS EC2 instances by starting and stopping them based on user-defined tags. It utilizes Amazon EventBridge to trigger actions hourly, optimizing resource usage and reducing costs.
- ⏰ Automatically starts/stops EC2 instances based on
start_at
andstop_at
tags. - 🌍 Supports multiple AWS regions via
region_list
. - 🕒 Optional support for time zone adjustment using the
time_zone
variable (based on pytz time zones). - 🔁 EventBridge rule triggers every hour to evaluate and apply schedule logic.
- 🔒 Built-in validation for allowed time zones.
-
Tag EC2 instances with:
start_at
: Hour (00–23) when the instance should startstop_at
: Hour (00–23) when the instance should stop
-
Deploy the module in your Terraform configuration to enable the automation.
-
Every hour, EventBridge invokes a Lambda function that:
- Checks the current hour in the configured time zone.
- Starts/stops instances as needed based on their tags.
module "start-stop-ec2" {
source = "../../../../public-cloud-iac-terraform-modules/aws/modules/ec2_scheduler"
region_list = ["eu-north-1", "eu-west-1"]
time_zone = "Europe/Stockholm"
}
Name | Description | Type | Default | Required |
---|---|---|---|---|
region_list |
List of AWS regions to target for scheduling. | list(string) |
["eu-north-1", "eu-west-1", "eu-central-1", "us-west-1"] |
No |
time_zone |
Time zone used for evaluating start_at /stop_at tags. Should match pytz names. |
string |
"Europe/Stockholm" |
No |
To schedule an EC2 instance to start at 8:00 AM and stop at 6:00 PM:
aws ec2 create-tags \
--resources i-0123456789abcdef0 \
--tags Key=start_at,Value=8:00 Key=stop_at,Value=18:00
No outputs are currently defined by this module.
- Terraform >= 1.0
- AWS provider