terraform.aws-baseline-infra/modules/compute/ec2-instance-scheduler/Ec2Scheduler.py

17 lines
544 B
Python
Raw Normal View History

2024-02-26 11:05:53 +08:00
import boto3
import os
import json
# reference: https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-eventbridge/
2024-02-27 10:23:48 +08:00
ec2 = boto3.client('ec2', region_name=os.environ['AWS_REGION'])
2024-02-26 11:05:53 +08:00
def lambda_handler(event, context):
2024-02-27 10:23:48 +08:00
if event['action'] == 'start':
2024-02-26 11:05:53 +08:00
resp = ec2.start_instances(InstanceIds=json.loads(os.environ['instances']))
2024-02-27 10:23:48 +08:00
elif event['action'] == 'stop':
2024-02-26 11:05:53 +08:00
resp = ec2.stop_instances(InstanceIds=json.loads(os.environ['instances']))
else:
resp = "Event action not provided"
2024-02-27 10:23:48 +08:00
return resp