Skip to content

Instantly share code, notes, and snippets.

@edreinoso
Created November 21, 2020 21:23
Show Gist options
  • Save edreinoso/03f16d715526c5499db1dd32ac2ecddf to your computer and use it in GitHub Desktop.
Save edreinoso/03f16d715526c5499db1dd32ac2ecddf to your computer and use it in GitHub Desktop.
ebsAvailableLambda.py
import boto3
import json
import time
def lambda_handler(event, context):
client = boto3.client('ec2')
documentName = "ebs_available"
# event['resources'] will return the arn
for elements in event['resources']:
arn = str(elements)
print(arn)
volumeId = arn[42:63] # getting the volumeId from the arn
print(volumeId)
# this sleep function is here to have a wait time
# while the volume gets attached
time.sleep(40)
response = client.describe_volumes(
VolumeIds=[
volumeId,
],
)
for volume in response['Volumes']:
print(volume)
for attchmnt in volume['Attachments']:
print(attchmnt['InstanceId'])
attchmnt_word = attchmnt['Device'][len(attchmnt['Device'])-1:len(attchmnt['Device'])]
device = "/dev/xvd"+attchmnt_word
print(device)
ssm = boto3.client('ssm')
# sending command with instanceId, documentnName, documentVersion, comment
response = ssm.send_command(
InstanceIds=[attchmnt['InstanceId']],
DocumentName=documentName,
DocumentVersion='$LATEST',
Comment='ssm.send_command from lambda to make ebs available',
# send device to which parameter should be mounted
Parameters={
'Device': [device],
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment