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
- Lambda
- IAM
- 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
Lab date
26-09-2021
Prerequisites
- AWS account
Lab source
Lab steps
- Create an EC2 Instance of type 2.micro, with attached Security Group with allowed Ingress rules for SSH, HTTP and HTTPS.
- In IAM create role for Lambda Service with permissions to “Allow” wide action on resources set up in policy.
- 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.
- 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.
- 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