Skip to content

Instantly share code, notes, and snippets.

@nktstudios
Created August 11, 2020 20:24
Show Gist options
  • Save nktstudios/5150810740be1b8a6f3a31408dc42d42 to your computer and use it in GitHub Desktop.
Save nktstudios/5150810740be1b8a6f3a31408dc42d42 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"
}
@jov316
Copy link

jov316 commented Jan 9, 2022

Thanks NKT Studios. for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment