Git Product home page Git Product logo

serverless-ec2-scheduler's Introduction

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.

A 2

A 3

A 4

A 5

A 6

A 7

Step 2 :

Creating the Policy :

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

A 8

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

A 9

A 10

A 11

A 12

A 13

A 14 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.

A 15

A 16

A 17

A 18

A 19

A 20

A 21

A 22

A 23

A 24

A 25

A 26

A 27

Now again , go to the Lambda console and then test the code. A 28

A 29

  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.

A 30

  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.

A 31

A 32

A 33

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

B 34

B 35

36

37

38

39

40

42

43

  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).

44

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

B 45

  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.

B 46

serverless-ec2-scheduler's People

Contributors

themannu avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.