Skip to content

Instantly share code, notes, and snippets.

@sairamkrish
Created May 2, 2020 18:21
Show Gist options
  • Save sairamkrish/72b60d6f616c9e18336231ed7ccd88e8 to your computer and use it in GitHub Desktop.
Save sairamkrish/72b60d6f616c9e18336231ed7ccd88e8 to your computer and use it in GitHub Desktop.
AWS lambda function to start or stop EC2 based on SNS
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