Created
May 2, 2020 18:21
-
-
Save sairamkrish/72b60d6f616c9e18336231ed7ccd88e8 to your computer and use it in GitHub Desktop.
AWS lambda function to start or stop EC2 based on SNS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import json | |
region = 'us-west-2' | |
ec2 = boto3.client('ec2', region_name=region) | |
def lambda_handler(event, context): | |
sns = event['Records'][0]['Sns'] | |
print('Message :' + sns['Message']) | |
message = json.loads(sns['Message']) | |
instanceIds = message['ec2InstanceIds'] | |
if (message['action'] == "Start"): | |
ec2.start_instances(InstanceIds=instanceIds) | |
print('Started your instances: ' + str(instanceIds)) | |
else: | |
ec2.stop_instances(InstanceIds=instanceIds) | |
print('Stoped your instances: ' + str(instanceIds)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment