Project Overview
This project automates the start and stop of AWS EC2 instances based on scheduled CloudWatch Events using an AWS Lambda function. It leverages Ruby AWS SDK for creating the Lambda function, CloudWatch Events, and EC2 instance management. You can configure the instance and schedule using the CLI.
graph TD A[User - Command Line Interface] B[CloudWatch Events] C[Lambda Function] D{Start or Stop EC2 Instance?} E[Start EC2 Instance] F[Stop EC2 Instance] G[CloudWatch Logs] subgraph AWS Cloud B C D E F G end A -->|Configure CloudWatch Events| B B -->|Trigger Event| C C -->|Execute Script| D D -->|Start| E D -->|Stop| F C -->|Log Execution| G A -.->|Monitor Logs| G classDef default fill:#f7f7f7,stroke:#2c3e50,stroke-width:2px; classDef input fill:#3498db,stroke:#2c3e50,stroke-width:2px,color:#fff; classDef process fill:#1abc9c,stroke:#2c3e50,stroke-width:2px,color:#fff; classDef decision fill:#f39c12,stroke:#2c3e50,stroke-width:2px; classDef output fill:#2ecc71,stroke:#2c3e50,stroke-width:2px,color:#fff; classDef storage fill:#e74c3c,stroke:#2c3e50,stroke-width:2px,color:#fff; class A input; class B,C process; class D decision; class E,F output; class G storage;
Features
- Lambda function: Automatically manages the start and stop of EC2 instances.
- CloudWatch Events: Schedules based on cron expressions to run the Lambda function at specific times.
- CLI Interface: Easy-to-use interface for setting up the Lambda function and managing EC2 instance operations.
Prerequisites
- AWS CLI: Ensure that the AWS CLI is installed and configured with access to your AWS account.
- Ruby 3.x: The project requires Ruby version 3.x. Install Ruby using a version manager like RVM or rbenv.
- AWS SDK for Ruby: The project uses
aws-sdk-ec2
andaws-sdk-cloudwatchevents
gems. - IAM Permissions: Ensure the AWS user you are using has permissions to manage CloudWatch Events, EC2, Lambda, and IAM roles.
Setup Instructions
1. Clone the Repository
git clone https://github.com/maniSHarma7575/hibernate.git
cd hibernate
2. Environment Configuration
Create a config.yaml
file at the root of your project and configure your AWS account details as follows:
aws_accounts:
profile_name:
account_id: ""
region: us-east-1
credentials:
access_key_id: ACCESS_KEY_1
secret_access_key: SECRET_KEY_1
profile_other:
account_id:
region: us-west-2
credentials:
access_key_id: ACCESS_KEY_2
secret_access_key: SECRET_KEY_2
3. Install Dependencies
Make sure you have the required gems and build the gem by running:
rake install
Command Usage
The hibernate
CLI tool provides commands for setting up the Lambda function and managing EC2 instance schedules.
Setup Command
hibernate setup --profile <PROFILE_NAME>
Options:
- -p, --profile: Specify the AWS profile name (required).
Example:
hibernate setup --profile production
Rule Command
Manage schedules for EC2 instance start/stop actions.
Create a Schedule
hibernate rule create --profile <PROFILE_NAME> --instance-name <INSTANCE_NAME> --start <CRON_START> --stop <CRON_STOP>
Options:
- -p, --profile: Specify the AWS profile name (required).
- -i, --instance-name: Specify the EC2 instance name (required).
- -s, --start: Specify the cron expression for starting the instance (optional).
- -e, --stop: Specify the cron expression for stopping the instance (optional).
List Schedules
hibernate rule list --profile <PROFILE_NAME> --instance-name <INSTANCE_NAME> [--start] [--stop]
Options:
- -p, --profile: Specify the AWS profile name (required).
- -i, --instance-name: Specify the EC2 instance name (required).
- -s, --start: List only the start action rules (optional).
- -e, --stop: List only the stop action rules (optional).
Update a Schedule
hibernate rule update --profile <PROFILE_NAME> --rule <RULE_NAME> --start <NEW_CRON_START> --stop <NEW_CRON_STOP> [--state <enable|disable>]
Options:
- -p, --profile: Specify the AWS profile name (required).
- -r, --rule: Specify the rule name to update (required).
- -s, --start: Specify the new cron expression for starting the instance (optional).
- -e, --stop: Specify the new cron expression for stopping the instance (optional).
- -a, --state: Set the rule state to either enable or disable (optional).
Remove a Schedule
hibernate rule remove --profile <PROFILE_NAME> --instance-name <INSTANCE_NAME> --rule <RULE_NAME>
Options:
- -p, --profile: Specify the AWS profile name (required).
- -i, --instance-name: Specify the EC2 instance name (required).
- -r, --rule: Specify the rule name to remove (required).