lab96_diagram

Purpose of this lab is to create an CloudWatch Event Rule that will trigger the Lambda function on a schedule.

Lab 96

Services Covered

  • EC2 EC2
  • lambda Lambda
  • IAM IAM
  • CloudWatch CloudWatch Events

Lab description

Purpose of this lab is to create an CloudWatch Event Rule that will trigger the Lambda function on a schedule. Lambda will check the status of an instance in the same region, and then change it. If instance is running it’ll be stopped and vice versa.

  • Creating Lambda functions
  • Creating CloudWatch Event Rules
  • Checking EC2 Instance status with Lambda function

Lab diagram

lab97diagram

Lab date

26-09-2021

Prerequisites

  • AWS account

Lab source

Whizlabs.com

Lab steps

  1. Create an EC2 Instance of type 2.micro, with attached Security Group with allowed Ingress rules for SSH, HTTP and HTTPS.
  2. In IAM create role for Lambda Service with permissions to “Allow” wide action on resources set up in policy.
  3. Create Lambda function with Python as runtime and attach the above mentioned Role to it. This function will check status of Instances and change it accordingly, if it’s running it will stop the instance and so on.
  4. In CloudWatch create a Rule in Events with either fixed rate or a CRON job. Add the Lambda function as target, this will trigger function.
  5. Edit created Lambda Configuration and set the Timeout to 1 minute.

Lab files

  • lambda_role.json – policy attached to the Lambda execution Role, giving it wide permission on EC2
  • lambda_func.py – Lambda function that will check instance status and change it accordingly

Acknowledgements

Tags:

Leave a Reply