hibernate 0.1.4 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb37df3a1d0e0b64ae191626bfe015bca529ff81582816dea8a9198acc388f0b
4
- data.tar.gz: 60eb78bd95762490b910a6dc949c666126c4e72fbd0822c5103b197642de8fbb
3
+ metadata.gz: 2bb58dd4aa7579fbedf120fb3e84975ec3cb721363abc73be70f2c5baea3242a
4
+ data.tar.gz: e551822ad245329bfaea6acdde6146dd038272637a60e51020016c2a62c4c31a
5
5
  SHA512:
6
- metadata.gz: 3111686232c5dc02bb56a7c9f3b59ed0d9fc90a3ac1f986b32038cbbc41abeb62840228712e10ada6d7920082f1201aaa52b8cc88fbb4a33f53ec6ee843ed42a
7
- data.tar.gz: 7d0389381b47eaa7be1d3938219c770b10ed5cca756cb861f4aceca5567e5abb4c460cbc56d9cddab42d548dfb768976573301b359083d045c38c453831aca72
6
+ metadata.gz: a6eb42d7aaea0b1d3b5e4e1ba8a65d22e5967c1b7b48b2895e49eb702a43a404f393237d737af928b2efb092c64893f0f617998eb0d572f50d41706dd0874d9f
7
+ data.tar.gz: 49b90600784c6ccd06b53dd9d60e0611671ede1ab0818ffdc0fd580b7555231eeb61df14daa4c7406745999eabc1dfb03fb08cbdd18005770c8bfe0f12709795
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .env
2
+ *.zip
3
+ *.gem
4
+ config.yaml
5
+ pkg
6
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'pry'
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,102 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hibernate (0.1.7)
5
+ aws-sdk-cloudwatch (~> 1.103)
6
+ aws-sdk-cloudwatchevents (~> 1.83)
7
+ aws-sdk-ec2 (~> 1.478)
8
+ aws-sdk-iam (~> 1.111)
9
+ aws-sdk-lambda (~> 1.136)
10
+ dotenv (~> 3.1)
11
+ json (~> 2.7)
12
+ optimist (~> 3.1)
13
+ rubyzip (~> 2.3)
14
+
15
+ GEM
16
+ remote: https://rubygems.org/
17
+ specs:
18
+ ast (2.4.2)
19
+ aws-eventstream (1.3.0)
20
+ aws-partitions (1.981.0)
21
+ aws-sdk-cloudwatch (1.103.0)
22
+ aws-sdk-core (~> 3, >= 3.207.0)
23
+ aws-sigv4 (~> 1.5)
24
+ aws-sdk-cloudwatchevents (1.83.0)
25
+ aws-sdk-core (~> 3, >= 3.207.0)
26
+ aws-sigv4 (~> 1.5)
27
+ aws-sdk-core (3.209.1)
28
+ aws-eventstream (~> 1, >= 1.3.0)
29
+ aws-partitions (~> 1, >= 1.651.0)
30
+ aws-sigv4 (~> 1.9)
31
+ jmespath (~> 1, >= 1.6.1)
32
+ aws-sdk-ec2 (1.478.0)
33
+ aws-sdk-core (~> 3, >= 3.207.0)
34
+ aws-sigv4 (~> 1.5)
35
+ aws-sdk-iam (1.111.0)
36
+ aws-sdk-core (~> 3, >= 3.207.0)
37
+ aws-sigv4 (~> 1.5)
38
+ aws-sdk-lambda (1.136.0)
39
+ aws-sdk-core (~> 3, >= 3.207.0)
40
+ aws-sigv4 (~> 1.5)
41
+ aws-sigv4 (1.10.0)
42
+ aws-eventstream (~> 1, >= 1.0.2)
43
+ coderay (1.1.3)
44
+ diff-lcs (1.5.1)
45
+ dotenv (3.1.4)
46
+ jmespath (1.6.2)
47
+ json (2.7.2)
48
+ language_server-protocol (3.17.0.3)
49
+ method_source (1.1.0)
50
+ optimist (3.1.0)
51
+ parallel (1.26.3)
52
+ parser (3.3.5.0)
53
+ ast (~> 2.4.1)
54
+ racc
55
+ pry (0.14.2)
56
+ coderay (~> 1.1)
57
+ method_source (~> 1.0)
58
+ racc (1.8.1)
59
+ rainbow (3.1.1)
60
+ rake (13.2.1)
61
+ regexp_parser (2.9.2)
62
+ rspec (3.13.0)
63
+ rspec-core (~> 3.13.0)
64
+ rspec-expectations (~> 3.13.0)
65
+ rspec-mocks (~> 3.13.0)
66
+ rspec-core (3.13.1)
67
+ rspec-support (~> 3.13.0)
68
+ rspec-expectations (3.13.3)
69
+ diff-lcs (>= 1.2.0, < 2.0)
70
+ rspec-support (~> 3.13.0)
71
+ rspec-mocks (3.13.1)
72
+ diff-lcs (>= 1.2.0, < 2.0)
73
+ rspec-support (~> 3.13.0)
74
+ rspec-support (3.13.1)
75
+ rubocop (1.66.1)
76
+ json (~> 2.3)
77
+ language_server-protocol (>= 3.17.0)
78
+ parallel (~> 1.10)
79
+ parser (>= 3.3.0.2)
80
+ rainbow (>= 2.2.2, < 4.0)
81
+ regexp_parser (>= 2.4, < 3.0)
82
+ rubocop-ast (>= 1.32.2, < 2.0)
83
+ ruby-progressbar (~> 1.7)
84
+ unicode-display_width (>= 2.4.0, < 3.0)
85
+ rubocop-ast (1.32.3)
86
+ parser (>= 3.3.1.0)
87
+ ruby-progressbar (1.13.0)
88
+ rubyzip (2.3.2)
89
+ unicode-display_width (2.6.0)
90
+
91
+ PLATFORMS
92
+ arm64-darwin-22
93
+
94
+ DEPENDENCIES
95
+ hibernate!
96
+ pry
97
+ rake
98
+ rspec (~> 3.13)
99
+ rubocop (~> 1.66)
100
+
101
+ BUNDLED WITH
102
+ 2.5.4
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler/gem_tasks'
data/Readme.md ADDED
@@ -0,0 +1,346 @@
1
+ # EC2 Auto Shutdown and Start
2
+
3
+ As a software engineering team, we are working with Amazon EC2 instances on a daily basis While EC2 instances are great for running applications and services but its important to properly manage our instances to avoid unnecessary costs and resource consumption. One way to do this is by automating the shutdown and start of your EC2 instances
4
+
5
+ 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.
6
+
7
+ ## Project Overview
8
+
9
+ The project allows you to:
10
+ - Create a Lambda function that automatically starts or stops EC2 instances based on their current state (i.e., running or stopped).
11
+ - Schedule EC2 instance start/stop operations using CloudWatch Events.
12
+ - Manage the schedule and EC2 instance operations through a Ruby-based CLI.
13
+
14
+ ```mermaid
15
+ flowchart TB
16
+ A[User - Command Line Interface]
17
+ B[CloudWatch Events]
18
+ C[Lambda Function]
19
+ D{Start or Stop EC2 Instance?}
20
+ E[Start EC2 Instance]
21
+ F[Stop EC2 Instance]
22
+ G[CloudWatch Logs]
23
+
24
+ subgraph AWS Cloud
25
+ B
26
+ C
27
+ D
28
+ E
29
+ F
30
+ G
31
+ end
32
+
33
+ A -->|Configure CloudWatch Events| B
34
+ B -->|Trigger Event| C
35
+ C -->|Execute Script| D
36
+ D -->|Start| E
37
+ D -->|Stop| F
38
+ C -->|Log Execution| G
39
+ A -.->|Monitor Logs| G
40
+
41
+ classDef default fill:#f7f7f7,stroke:#2c3e50,stroke-width:2px;
42
+ classDef input fill:#3498db,stroke:#2c3e50,stroke-width:2px,color:#fff;
43
+ classDef process fill:#1abc9c,stroke:#2c3e50,stroke-width:2px,color:#fff;
44
+ classDef decision fill:#f39c12,stroke:#2c3e50,stroke-width:2px;
45
+ classDef output fill:#2ecc71,stroke:#2c3e50,stroke-width:2px,color:#fff;
46
+ classDef storage fill:#e74c3c,stroke:#2c3e50,stroke-width:2px,color:#fff;
47
+
48
+ class A input;
49
+ class B,C process;
50
+ class D decision;
51
+ class E,F output;
52
+ class G storage;
53
+ ```
54
+
55
+ ## Features
56
+
57
+ - **Lambda function**: Automatically manages the start and stop of EC2 instances.
58
+ - **CloudWatch Events**: Schedules based on cron expressions to run the Lambda function at specific times.
59
+ - **CLI Interface**: Easy-to-use interface for setting up the Lambda function and managing EC2 instance operations.
60
+
61
+ ---
62
+
63
+ ## Prerequisites
64
+
65
+ - **AWS CLI**: Ensure that the AWS CLI is installed and configured with access to your AWS account.
66
+ - **Ruby 3.x**: The project requires Ruby version 3.x. Install Ruby using a version manager like RVM or rbenv.
67
+ - **AWS SDK for Ruby**: The project uses `aws-sdk-ec2` and `aws-sdk-cloudwatchevents` gems.
68
+ - **IAM Permissions**: Ensure the AWS user you are using has permissions to:
69
+
70
+ Required IAM Permissions
71
+
72
+
73
+ 1. CloudWatch Events Permissions
74
+
75
+ - events:PutRule: Create or update CloudWatch event rules.
76
+ - events:ListRules: List existing event rules.
77
+ - events:DescribeRule: Retrieve details about a specific event rule.
78
+ - events:PutTargets: Associate targets with CloudWatch event rules.
79
+ - events:RemoveTargets: Remove targets from CloudWatch event rules.
80
+ - events:DeleteRule: Delete an event rule.
81
+ - events:ListTargetsByRule: List targets associated with a specific rule.
82
+
83
+ 2. EC2 Permissions
84
+
85
+ - ec2:DescribeInstances: Retrieve information about EC2 instances.
86
+ - ec2:StartInstances: Start EC2 instances (if applicable).
87
+ - ec2:StopInstances: Stop EC2 instances (if applicable).
88
+
89
+ 3. Lambda Permissions
90
+
91
+ - lambda:AddPermission: Allow CloudWatch Events to invoke the Lambda function.
92
+ - lambda:RemovePermission: Remove permissions that allow invocation of the Lambda function.
93
+ - lambda:InvokeFunction: Allow invocation of the specified Lambda function (if needed directly).
94
+ - lambda:CreateFunction: Create a new Lambda function.
95
+ - lambda:GetFunction: Retrieve details about a specific Lambda function.
96
+
97
+ 4. IAM Permissions
98
+
99
+ - iam:GetRole: Retrieve details about a specific IAM role.
100
+ - iam:PassRole: Allow passing a role to the Lambda service, if required.
101
+
102
+ ```json
103
+ {
104
+ "Version": "2012-10-17",
105
+ "Statement": [
106
+ {
107
+ "Effect": "Allow",
108
+ "Action": [
109
+ "events:PutRule",
110
+ "events:ListRules",
111
+ "events:DescribeRule",
112
+ "events:PutTargets",
113
+ "events:RemoveTargets",
114
+ "events:DeleteRule",
115
+ "events:ListTargetsByRule"
116
+ ],
117
+ "Resource": "*"
118
+ },
119
+ {
120
+ "Effect": "Allow",
121
+ "Action": [
122
+ "ec2:DescribeInstances",
123
+ "ec2:StartInstances",
124
+ "ec2:StopInstances"
125
+ ],
126
+ "Resource": "*"
127
+ },
128
+ {
129
+ "Effect": "Allow",
130
+ "Action": [
131
+ "lambda:AddPermission",
132
+ "lambda:RemovePermission",
133
+ "lambda:InvokeFunction",
134
+ "lambda:CreateFunction",
135
+ "lambda:GetFunction"
136
+ ],
137
+ "Resource": "arn:aws:lambda:<region>:<account-id>:function:*"
138
+ },
139
+ {
140
+ "Effect": "Allow",
141
+ "Action": [
142
+ "iam:GetRole",
143
+ "iam:PassRole",
144
+ "lambda:GetFunction"
145
+ ],
146
+ "Resource": [
147
+ "arn:aws:iam::<account-id>:role/<role-name>",
148
+ "arn:aws:iam::<account-id>:role/*",
149
+ "arn:aws:lambda:<region>:<account-id>:function:<function-name>"
150
+ ]
151
+ }
152
+ ]
153
+ }
154
+ ```
155
+
156
+ ---
157
+
158
+ ## Setup Instructions
159
+
160
+ ### 1. Clone the Repository
161
+
162
+ ```bash
163
+ git clone [https://github.com/maniSHarma7575/hibernate.git](https://github.com/maniSHarma7575/hibernate.git)
164
+ cd hibernate
165
+ ```
166
+
167
+ ### 2. Environment Configuration
168
+
169
+ Create a `config.yaml` file at the root of your project and configure your AWS account details as follows:
170
+
171
+ ```yaml
172
+ aws_accounts:
173
+ profile_name:
174
+ account_id: "<account_id_1>"
175
+ region: us-east-1
176
+ credentials:
177
+ access_key_id: ACCESS_KEY_1
178
+ secret_access_key: SECRET_KEY_1
179
+ profile_other:
180
+ account_id:<account_id_2>
181
+ region: us-west-2
182
+ credentials:
183
+ access_key_id: ACCESS_KEY_2
184
+ secret_access_key: SECRET_KEY_2
185
+ ```
186
+
187
+ ### 3. Install Dependencies
188
+
189
+ Make sure you have the required gems and build the gem by running:
190
+
191
+ ```bash
192
+ rake install
193
+ ```
194
+
195
+ Here’s how the Usage section for the Readme.md can be structured based on your script:
196
+
197
+ ## Usage
198
+
199
+ Automate the shutdown and start of EC2 instances using scheduled rules.
200
+
201
+ ### Available Commands
202
+
203
+ - **setup**: Set up the AWS IAM role and Lambda function for managing EC2 instance schedules.
204
+ - **rule**: Manage schedules for EC2 instance start/stop actions.
205
+ - **create**: Create a new start/stop schedule.
206
+ - **list**: List existing schedules.
207
+ - **update**: Update an existing schedule.
208
+ - **remove**: Remove an existing schedule.
209
+
210
+ ```bash
211
+ hibernate --help
212
+ ```
213
+
214
+ ### Command Usage
215
+
216
+ #### Setup Command
217
+
218
+ ```bash
219
+ hibernate setup --profile <PROFILE_NAME>
220
+ ```
221
+
222
+ Options:
223
+
224
+ - -p, --profile: Specify the AWS profile name (required).
225
+
226
+ #### Rule Command
227
+
228
+ **Create a Schedule**
229
+
230
+ ```bash
231
+ hibernate rule create --profile <PROFILE_NAME> --instance-name <INSTANCE_NAME> --start <CRON_START> --stop <CRON_STOP>
232
+ ```
233
+
234
+ Options:
235
+
236
+ - -p, --profile: Specify the AWS profile name (required).
237
+ - -i, --instance-name: Specify the EC2 instance name (required).
238
+ - -s, --start: Specify the cron expression for starting the instance (optional).
239
+ - -e, --stop: Specify the cron expression for stopping the instance (optional).
240
+
241
+ **List Schedules**
242
+
243
+ ```bash
244
+ hibernate rule list --profile <PROFILE_NAME> --instance-name <INSTANCE_NAME> [--start] [--stop]
245
+ ```
246
+ Options:
247
+
248
+ - -p, --profile: Specify the AWS profile name (required).
249
+ - -i, --instance-name: Specify the EC2 instance name (required).
250
+ - -s, --start: List only the start action rules (optional).
251
+ - -e, --stop: List only the stop action rules (optional).
252
+
253
+ **Update a Schedule**
254
+
255
+ ```bash
256
+ hibernate rule update --profile <PROFILE_NAME> --rule <RULE_NAME> --start <NEW_CRON_START> --stop <NEW_CRON_STOP> [--state <enable|disable>]
257
+ ```
258
+
259
+ Options:
260
+
261
+ - -p, --profile: Specify the AWS profile name (required).
262
+ - -r, --rule: Specify the rule name to update (required).
263
+ - -s, --start: Specify the new cron expression for starting the instance (optional).
264
+ - -e, --stop: Specify the new cron expression for stopping the instance (optional).
265
+ - -a, --state: Set the rule state to either enable or disable (optional).
266
+
267
+ **Remove a Schedule**
268
+
269
+ ```bash
270
+ hibernate rule remove --profile <PROFILE_NAME> --instance-name <INSTANCE_NAME> --rule <RULE_NAME>
271
+ ```
272
+
273
+ Options:
274
+
275
+ - -p, --profile: Specify the AWS profile name (required).
276
+ - -i, --instance-name: Specify the EC2 instance name (required).
277
+ - -r, --rule: Specify the rule name to remove (required).
278
+
279
+ Examples
280
+
281
+ 1. Setting up the environment:
282
+
283
+ ```bash
284
+ hibernate setup --profile production
285
+ ```
286
+
287
+ 2. Creating a start/stop schedule for an EC2 instance:
288
+
289
+ ```bash
290
+ hibernate rule create --profile production --instance-name my-ec2-instance --start "cron(0 8 * * ? *)" --stop "cron(0 18 * * ? *)"
291
+ ```
292
+
293
+
294
+ 3. Listing all schedules for an EC2 instance:
295
+
296
+ ```bash
297
+ hibernate rule list --profile production --instance-name my-ec2-instance
298
+ ```
299
+
300
+ 4. Updating an existing rule:
301
+
302
+ ```bash
303
+ hibernate rule update --profile production --rule my-rule --start "cron(0 7 * * ? *)" --state enable
304
+ ```
305
+
306
+ 5. Removing a schedule:
307
+
308
+ ```bash
309
+ hibernate rule remove --profile production --instance-name my-ec2-instance --rule my-rule
310
+ ```
311
+
312
+ #### Shortcut Options
313
+
314
+ You can use these shortcut flags for commonly used options:
315
+
316
+ - -p for --profile
317
+ - -i for --instance-name
318
+ - -s for --start
319
+ - -e for --stop
320
+ - -r for --rule
321
+ - -a for --state
322
+
323
+ ```bash
324
+ hibernate rule create -p production -i my-ec2-instance -s "cron(0 8 * * ? *)" -e "cron(0 18 * * ? *)
325
+ ```
326
+ ---
327
+
328
+ ### Resources:
329
+ - [AWS Cron Expressions](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html)
330
+
331
+ ## Contributing
332
+
333
+ Feel free to submit pull requests or report issues. We welcome contributions to enhance the functionality of the project.
334
+
335
+ ---
336
+
337
+ ## License
338
+
339
+ This project is licensed under the MIT License. See the `LICENSE` file for details.
340
+
341
+ ---
342
+
343
+ ### Final Notes
344
+
345
+ - Ensure your AWS credentials are properly configured in the `.env` file.
346
+ - Test the Lambda function thoroughly before deploying it to production environments.