aws lambda function to start and stop ec2 instance with tag value
AWS Lambda is a server less computing service that allows users to run code for applications and back end services without managing servers.
Create a instance
create lambda function
function name stopec2
Create a new role with basic Lambda permissions; create function
configuration; edit
timeout 5min; save
permission; choose role
open therole; attach policies
add ec2fullpermissions
now write code; mention service_name, region_name,tag,values then test; function name each.stop() using stop instance; when you want start instance using function each.start().
import json import boto3
ec2 = boto3.client('ec2')
def lambda_handler(event,context):
response = ec2.stop_instances(InstanceIds=["i-05d5d81c58986c020"])
return { 'statusCode':200, 'body':json.dumps(response) }