Home » People & Blogs » How to Automatically Stop/Start EC2 instances using AWS Lambda| Cloudwatch Events Scheduler

How to Automatically Stop/Start EC2 instances using AWS Lambda| Cloudwatch Events Scheduler

Written By Sumi P on Monday, Jun 29, 2020 | 04:43 PM

 
In this video we have walked through to Start/Stop instance using Lambda function and manage using Cloudwatch Events . Refer Link : https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/ Please Find the Scripts To Create IAM policy using JSON policy editor:- { "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": "*" } ] } Stop the instances:- import boto3 region = 'us-west-1' instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26'] ec2 = boto3.client('ec2', region_name=region) def lambda_handler(event, context): ec2.stop_instances(InstanceIds=instances) print('stopped your instances: ' + str(instances)) Start the instances:- import boto3 region = 'us-west-1' instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26'] ec2 = boto3.client('ec2', region_name=region) def lambda_handler(event, context): ec2.start_instances(InstanceIds=instances) print('started your instances: ' + str(instances))