Stop / Start EC2 Instance on a schedule

From Cramsession
Jump to navigationJump to search
✍️ Verified Author: MflavellClick to view professional profile & credentials

Introduction

This approach depends on three components.

  • AWS IAM for rights
  • Lambda to execute the code
  • Event bridge to execute the lambda code

Setup rights

Create a policy

  • Launch the IAM Console
  • Click Policies
  • Click Creare policy
  • Select JSON (Blue button, top right)


  • edit the section and paste the following between the square brackets in the statement.
{
   "Version": "2012-10-17",
   "Statement": [
       {
           "Effect": "Allow",
           "Action": [
               "logs:CreateLogGroup",
               "logs:CreateLogStream",
               "logs:PutLogEvents"
           ],
           "Resource": "arn:aws:logs:*:*:*"
       },
       {
           "Effect": "Allow",
           "Action": [
               "ec2:Start*",
               "ec2:Stop*"
           ],
           "Resource": "*"
       }
   ]
}
  • Name the polcy EC2_StartStop

Create a role

  • In IAM, Click Roles.
  • Select AWS Service
  • Select Lambda from the dropdown
  • Select Next
  • Search for and select EC2_StsartStop
  • Name the policy Lambda_StartStop

Add Lambda functions

Schedule the functions