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 new policy in the IAM Console.

  • 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": "*"
       }
   ]

}

Add Lambda functions

Schedule the functions