Stop / Start EC2 Instance on a schedule: Difference between revisions

From Cramsession
Jump to navigationJump to search
✍️ Verified Author: MflavellClick to view professional profile & credentials
Line 9: Line 9:
= Setup rights =
= Setup rights =


Create a new policy in the IAM Console.
== Create a policy ==


* Launch the IAM Console
* Launch the IAM Console
Line 18: Line 18:


* Select '''JSON''' (Blue button, top right)
* Select '''JSON''' (Blue button, top right)


* edit the section and paste the following between the square brackets in the statement.
* edit the section and paste the following between the square brackets in the statement.


  {
  {
Line 44: Line 44:
     ]
     ]
  }
  }
* 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 =
= Add Lambda functions =


= Schedule the functions =
= Schedule the functions =

Revision as of 03:23, 29 March 2025

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