Skip to content

Automatically stop and start EC2 instances at scheduled times with Eventbridge + Lambda

Notifications You must be signed in to change notification settings

Lily-G1/stop_start_instances

Repository files navigation

stop_start_instances

Automate the stop & start of EC2 instances to optimize costs

This code stops running instances at 5pm and starts them at 9am every weekday (Mon - Fri). Lambda runs the actual python functions that start and stop instances, while Eventbridge sets a schedule and triggers the Lambda functions at specified times. With automation and strategies as this, you can effectively reduce AWS expenditure without compromise on performance or functionality.

stop_start drawio (1)

Prerequisites

  • An AWS account
  • Terraform installed on your local system
  • One or more running AWS instances

To run:

  • Clone this repo
  • Enter root directory: cd stop_start_instances
  • Enter AWS instances' IDs in .py files
  • Edit the cron expressions in eventbridge.tf to specify your time schedules
  • terraform init
  • terraform plan
  • terraform apply

To destroy:

  • terraform destroy