This CloudFormation template creates an automated EC2 instance scheduler using AWS Lambda and EventBridge. It helps reduce AWS costs by automatically starting and stopping non-production instances based on defined working hours. Additionally, it includes a resource-checking Lambda function that identifies untagged resources and sends notifications via Slack.
- Automated Scheduling: Uses EventBridge to trigger Lambda functions for starting and stopping instances.
- Tag-Based Control: Only instances tagged with
AutoStop: trueare affected. - Slack Notifications: Notifies a Slack channel whenever instances are started or stopped.
- Daily Resource Audit: Checks for untagged EC2 instances, EBS volumes, and Elastic IPs, sending a report to Slack.
- Secrets Manager Security: Stores Slack Webhook URL securely.
- Production-Safe: Ensures production workloads remain unaffected.
- Monthly EC2 costs reduced from $8,500 to $3,145.
- Identified $950 worth of untagged resources in the first week.
- AWS account with appropriate IAM permissions for CloudFormation, Lambda, and EC2.
- Slack Webhook URL for notifications.
- Slack Webhook URL stored in AWS Secrets Manager.
- Deploy the CloudFormation template.
- Provide working hours (UTC format) for instance start/stop times.
- Add the
AutoStop: truetag to all non-production instances. - Monitor Slack notifications for instance actions and untagged resources giving cost savings insights.
- Configure working hours using the
WorkingHoursStartandWorkingHoursEndparameters.
This repository contains a CloudFormation YAML file that defines:
- An IAM role with permissions for EC2 and Lambda.
- Two Lambda functions:
InstanceSchedulerFunctionfor managing EC2 instances.UntaggedResourcesFunctionfor identifying untagged resources.
- EventBridge rules to trigger Lambda functions on schedule.
- Slack integration for automated notifications.
Modify the WorkingHoursStart and WorkingHoursEnd parameters in the CloudFormation template to adjust instance schedules.
- Instance Scheduling:
- Starts instances at
WorkingHoursStart. - Stops instances at
WorkingHoursEnd. - Sends a Slack notification after each action.
- Starts instances at
- Daily Resource Audit:
- Scans EC2, EBS volumes, and Elastic IPs for missing tags.
- Sends a Slack report listing untagged resources.
- Deploy the CloudFormation stack.
- Tag your instances appropriately.
- Monitor Slack for instance activity and untagged resources.
- Lambda Fails? Check CloudWatch logs for errors.
- Slack Notifications Not Sending? Verify the webhook URL in AWS Secrets Manager.
- EC2 Not Starting/Stopping? Ensure instances are tagged with
AutoStop: true.
Feel free to submit pull requests for improvements!
MIT