Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 

Repository files navigation

SERVERLESS EC2 INSTANCE SHUT DOWN SCHEDULER FOR ORGANISATION WORKING HOURS

** Scenario:**: In some companies, there is no need to run EC2 instances round the clock. they require instances to operate during specific time periods. To address this, We will implement Lambda function for stopping the instances and set up cloudwatch events that will triger the lambda fucntion at exactly 12:00.

266763781-287063a4-964a-4f8b-b88e-25535b7f4691 Steps: Step 1 Creating Instances or use existing instances

Step 2:

Creating the Policy:

  1. Navigate to the IAM Console

  2. Click on "Policies" and then click on " Create policy" 266684770-92ea0ecd-8106-4e7f-80fc-c815093cf319

  3. Select services as Ec2.

  4. And Actions are DescribeInstances, StopInstances

  5. 266685226-ca8dc00e-4931-4646-a984-f251d83ebf6a

  6. stop instance

  7. 266685318-8543cf1c-26d3-41dd-9121-99015183f760 NB: change the policy name to stop-ec2-instance and the description to stop ec2 running instances

  8. 266685384-78b57e8c-b8b5-470e-9af1-e7789b09f522

  9. 266685404-1a1da31e-d564-4264-9b4e-fdb5438ed705

  10. Now that we have created a policy for stopping instances, we are going to create a Lambda function with its role and attach the policy to that role.

    Step 3

    Creating the Lambda functions:

    1. Navigate to Lambda on the management console

    2. Follow the outlined steps below

    3. 266688772-0c16bb73-c4e2-4d0d-94a3-d7eb6a7b2468

    4. 266688789-0e1249d5-b472-46bb-8f8f-9cfffaed63e9

    5. Remove the default function and paste stop-ec2-instance-py python code

    6. lambda function

    7. Click on "Deploy" to save the changes

    8. Click on "Test"

    9. 266689003-00a475ec-d570-47ef-9487-9a7ad6f99c67 NB. change event name to stop instance

    10. Click on " Configuration" on the Lambda functin Tab and click on the "Stop-Ec2-Instances-role-ik9mtlab". It will open a new tab to add permision to the role

    11. lambda role

    12. Select the policy you created before and click "Add permision"

    13. Now go back to the Lambda console and then test the code. You will see Execution succeeded and the instances in stopped state.

    14. Now we are ready to proceed and create schedules for this Functions

    Step 4

    Creating the Schedues Using Cloud watch

    1. Navigate to the CloudWatch console

    2. Follow the outlined steps below

    3. 266693720-a932c63f-8a3b-46aa-ade2-ed0d9dd8cdac

    4. 266693744-cfd0914c-7c0c-4064-ae3e-72874467fe7c

    5. name stop

    6. Click "Continue in EventBrigde Scheduler"

    7. Select "Recurring Schedule" and "Cron-Based Schedule"

    8. Set the Cron Tab as follow to automatically triger by 12:00 based on your time zone

    9. cron job

    10. 266693988-70f29eaa-73fc-49ec-9c39-a6d1214d14f3

    11. 266694020-8935ecd6-ce42-4347-b48c-3b5564679c03

    12. Choose your Lambda function to stop ec2 instance and click " Next"

    13. Also choose "NONE" from the Action after schedule completion

    14. 266694241-ea584d14-d997-46d7-b1ec-f07cb88a9ede

    15. 266694270-b76d4878-b391-4d9c-9b12-07b1b1ebfb5d

    We have successfully cretaed a Lambda function that will be triggered by CloudWatch EventBridge schedule to shut down running ec2 instances by 12:00am everyday.

About

Lambda function to stop ec2 instances

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages