This project automates the start and stop of EC2 instances based on office hours. It:
- Starts instances at 09:00 IST and stops them at 20:00 IST (Monday–Saturday)
- Targets instances tagged with
AutoSchedule=OfficeHours - Skips instances in production (
Environment=prod) - Sends notifications to a Google Chat channel when instances are started or stopped
- Uses Terraform for infrastructure as code and manages Lambda, EventBridge rules, IAM roles, and backend state
Application Workflow
- This flow represents what happens during periodic operations when the scheduler runs.
flowchart LR
EventBridgeStart[EventBridge Rule<br/>Start @ 09:00 IST]
EventBridgeStop[EventBridge Rule<br/>Stop @ 20:00 IST]
Lambda[Lambda Function<br/>ec2-office-hours-scheduler]
EC2[EC2 Instances<br/>Tag: AutoSchedule=OfficeHours]
CloudWatch[CloudWatch Logs]
GoogleChat[Google Chat Channel]
EventBridgeStart -->|invoke| Lambda
EventBridgeStop -->|invoke| Lambda
Lambda -->|start / stop| EC2
Lambda -->|logs| CloudWatch
Lambda -->|status notification| GoogleChat
- Please find the further Detailed Architecture & Documentation
Before setting up this project, ensure you have:
- AWS Account with access to EC2, Lambda, IAM, S3, CloudWatch, and DynamoDB
- S3 bucket created for Terraform backend, e.g.:
coloredcow-devops-aws-bucket(we will use folderec2-office-hours-scheduler/inside this bucket) - Terraform v1.5+
- AWS CLI v2 with a named profile (
coloredcowor your project name) - Python 3.12+ (for Lambda function)
- macOS (or Linux) terminal
- Google Chat room with a Webhook URL
For more detailed instructions, please refer to the docs/ folder:
- Setup and Configuration
- Deploying the Infrastructure
- Backend (State Management)
- Terraform Resources
- Lambda Code
- Google Chat Setup
| Script | Purpose |
|---|---|
build_lambda.sh |
Package Lambda code into a zip (optional if using Terraform archive_file) |
deploy.sh |
Build Lambda (optional) + Terraform apply |
destroy.sh |
Terraform destroy for cleaning up all resources |
- Lambda logs are available in CloudWatch Logs under
/aws/lambda/ec2-office-hours-scheduler - EventBridge triggers are set up for start (09:00 IST) and stop (20:00 IST) - (Based on the time added in the envs/common.tfvars)
- Google Chat notifications provide immediate feedback on actions taken
- Adding new dependencies to Lambda → update
lambda/requirements.txt - Updating schedules → modify EventBridge cron expressions in Terraform
- Skipping production instances → controlled via
SKIP_ENVIRONMENT - Scripts can be integrated into CI/CD pipelines
This repo is a DevOps spoke of ColoredCow/coloredcow-devops — the hub that consolidates what the fleet teaches. Project-specific DevOps learnings (and the patterns harvested up to the hub) live in DEVOPS_LEARNINGS.md.
When you complete work that teaches a DevOps-function lesson — a new cost lever, a safety guard, a scoping decision, a capability gained or a gap found — record one line there in the same change as the work (backward-integration: extract from work done, never pre-write). Tag it specific (stays here) or generic (also flagged for harvest to the hub). No secrets — no webhook URL, bucket/region, account or instance IDs; teach the pattern, not the asset. See the file's header for the full ritual.