-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_ssm_lambda.py
More file actions
25 lines (21 loc) · 928 Bytes
/
run_ssm_lambda.py
File metadata and controls
25 lines (21 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from __future__ import print_function
import json
import urllib
import boto3
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
print('Loading function')
ec2 = boto3.client('ec2')
ssm = boto3.client('ssm')
import boto3
def lambda_handler(event,context):
logger.info('got event{}'.format(event))
instanceId = event['detail']['instance-id']
instanceName = ec2.describe_instances(InstanceIds=[str(instanceId),])['Reservations'][0]['Instances'][0]['Tags']
instanceName = [y['Value'] for y in filter(lambda x: 'Name' in x.values(), instanceName)][0]
print ("Instance Name: " + instanceName)
print("Instance ID: " + instanceId)
command = 'sh ##PUT_YOUR_COMMAND_HERE## ' + instanceName
print("Command: " + command)
ssmresponse = ssm.send_command(InstanceIds=['##instanceID_to_run_command_on##'], DocumentName='AWS-RunShellScript', Parameters= { 'commands': [command] } )