Skip to content

Instantly share code, notes, and snippets.

@cvargas-xbrein
Forked from nktstudios/Lambda Python Code
Created March 10, 2021 20:51
Show Gist options
  • Save cvargas-xbrein/1ff6b67bb690290833d80789bb507854 to your computer and use it in GitHub Desktop.
Save cvargas-xbrein/1ff6b67bb690290833d80789bb507854 to your computer and use it in GitHub Desktop.
Start Stop EC2
import json
import boto3
region = 'us-east-2'
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
instances = event["instances"].split(',')
action = event["action"]
if action == 'Start':
print("STARTing your instances: " + str(instances))
ec2.start_instances(InstanceIds=instances)
response = "Successfully started instances: " + str(instances)
elif action == 'Stop':
print("STOPping your instances: " + str(instances))
ec2.stop_instances(InstanceIds=instances)
response = "Successfully stopped instances: " + str(instances)
return {
'statusCode': 200,
'body': json.dumps(response)
}
{
"instances": "PUT YOUR INSTANCE ID's HERE",
"action": "Start"
}
{
"instances": "PUT YOUR INSTANCE ID's HERE",
"action": "Stop"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment