Skip to content

Instantly share code, notes, and snippets.

@daniel-woods
Last active April 11, 2019 16:58
Show Gist options
  • Save daniel-woods/dd26dd750c353d06e35a296b67f5fa01 to your computer and use it in GitHub Desktop.
Save daniel-woods/dd26dd750c353d06e35a296b67f5fa01 to your computer and use it in GitHub Desktop.
Lambda function that uses the pathParameters.proxy to determine the action taken. (EC2 Start/Stop/Reboot)
import boto3
import json
# Add any instance IDs to the list instances
# instances = ["i-xxxxxxx", "i-yyyyyyy", "i-zzzzzzz"]
instances = ["i-xxxxxxx"]
def lambda_handler(event, context):
ec2_client = boto3.client("ec2")
ifttt_event = event['pathParameters']['proxy']
if ifttt_event == "start_instances":
print("Attempting to start instance(s): {}".format(' '.join(instances)))
response = ec2_client.start_instances(InstanceIds=instances)
elif ifttt_event == "stop_instances":
print("Attempting to stop instance(s): {}".format(' '.join(instances)))
response = ec2_client.stop_instances(InstanceIds=instances)
elif ifttt_event == "reboot_instances":
print("Attempting to reboot instance(s): {}".format(' '.join(instances)))
response = ec2_client.reboot_instances(InstanceIds=instances)
else:
# Feel free to add your own ifttt_event elif statements!
print("Unsupported ifttt_event type: {} ()".format(str(ifttt_event), type(ifttt_event)))
if response:
print(response)
return {"statusCode": 200}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment