Hibernate

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

Prerequisites

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:

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:

List Schedules

hibernate rule list --profile <PROFILE_NAME> --instance-name <INSTANCE_NAME> [--start] [--stop]

Options:

Update a Schedule

hibernate rule update --profile <PROFILE_NAME> --rule <RULE_NAME> --start <NEW_CRON_START> --stop <NEW_CRON_STOP> [--state <enable|disable>]

Options:

Remove a Schedule

hibernate rule remove --profile <PROFILE_NAME> --instance-name <INSTANCE_NAME> --rule <RULE_NAME>

Options:

Additional Resources