import boto3 import os import json # reference: https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-eventbridge/ def lambda_handler(event, context): ec2 = boto3.client('ec2', region_name=os.environ['region_name']) instances = json.loads(os.environ['instances']) if event['action'] == 'start': resp = ec2.start_instances(InstanceIds=instances) elif event['action'] == 'stop': resp = ec2.stop_instances(InstanceIds=instances) else: raise ValueError("Invalid event action") return resp