Aksh0110 / serverless-ec2-scheduler

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serverless EC2 Instance Scheduler for Company Working Hours

Scenario :

In some companies, there is no need to run their EC2 instances 24/7; they require instances to operate during specific time periods, such as company working hours, from 8:00 AM in the morning to 5:00 PM in the evening. To address this scenario, I will implement two Lambda functions responsible for starting and stopping instances. These Lambda functions will be triggered by two CloudWatch Events in the morning and evening. This solution is fully serverless.

Blank diagram

Steps :

Step 1 :

Creating the Instance :

  1. Navigate to the EC2 Console.
  2. Follow the Outlined steps below.

a1

a2

a3

a4

a5

a6

Step 2 :

Creating the Policy :

  1. Navigate to the IAM Console.
  2. Click on "Policies" and then Click on "Create policy"

a7

  1. Select services as EC2.
  2. And Actions are DescribeInstances , StartInstances.

a8

a9

a10

a11

a12

a13 5. Now we have created a policy for starting instances. We also need to create a policy for stopping the instances. This is because we are going to create two Lambda functions: one for starting and one for stopping the instances. Each function will have its own role, and we will attach these two policies to their respective roles.
6. Now we are going to repeat the same steps for Creating Stopping Policy also.
7. Everything is same , Except Actions because we are going to stop the instance.
8. The Actions are DescribeInstances , StopInstances .
9. Keep your Plolicy name as "stop-ec2-instance".

Step 3 :

Creating the Lambda functions :

  1. Navigate to the lambda Console.
  2. Follow the Outlined steps below.

a14

a15

a16

a17

Screenshot 2024-02-23 065451

Screenshot 2024-02-23 065512

a18

a19

a20

a21

a22

a23

a24

Now again , go to the Lambda console and then test the code. a25 a26

  1. Now we Created alambda function for Starting Instance.
  2. We have to Reapeat the same steps again to Create a Lambda function for Stopping Instance , Keep your lambda function name as "Stop-EC2-demo".
  3. The only changes we have to make are to replace the default code with the 'stop-ec2-instance.py' code and attach the policy we created for stopping instances to the role of this Lambda function.

a27

  1. As demonstrated above, when I test my Python code, it runs successfully and stops the instance.
  2. Now, we are ready to proceed and create schedules for this functions.

Step 5 :

Creating the Schedules Using Cloud Watch :

  1. Navigate to the Cloud Watch Console.
  2. Follow the Outlined Steps below.

a28

a29

a30

Note : Keep your rule name as "start-ec2-rule" , I mistakenly named it 'role' Please do not name it as 'role.'

b1

b1 1

b2

b3

b4

b5

b6

b7

b8

  1. We have now created a schedule for starting the instance every day at 8:00 AM.
  2. Next, we need to create a schedule for stopping instances.
  3. To create the schedule for stopping instances, follow the same steps as for starting instance scheduling with a few changes, Keep your rule name as "stop-ec2-rule".
  4. The changes include modifying the scheduled time and selecting the appropriate scheduling function.
  5. We need to change the schedule time to 17:00 because it will stop the Lambda function at 17:00 IST (5:00 PM).

b9

  1. We have to Change the Function as Stop-EC2-demo

b10

  1. Now, we have successfully created two schedules: one to start the instance every day at 8:00 AM and the other to stop the instance every day at 5:00 PM.

b11

About


Languages

Language:Python 100.0%