Schedule the start and stop activities of your AWS EC2 instance using a Lambda function
here am going to explain how to create the lambda function to start and stop your ec2 instances in the scheduled time interval.
go to IAM
create policy
select service as ec2
action; start instance and stop instance
instance add arn
specify arn
next
policy name
create policy
now create role
create role
trusted entity type
use case
stop-ec2
role name stop-ec2
create role
create a lambda function
create function
function name
change default execution role
create function
now lambda function created
in lamba write the code to stop instance
code like this enter instance id; region name; click deploy code saved
import boto3 region = 'ap-south-1' instances = ['i-0a627784fd87373be'] ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context): ec2.start_instances(InstanceIds=instances) print('started your instances: ' + str(instances))
then click test
event name; stop-ec2; save
now see instance stopped
test
Subscribe to my newsletter
Read articles from Sundar blog directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by