You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.1 KiB
36 lines
1.1 KiB
import json
|
|
import boto3
|
|
|
|
VOLUME_ID = 'vol-123412341234'
|
|
TARGET_ACCOUNT = "123412341234"
|
|
|
|
print("START")
|
|
|
|
def lambda_handler(event, context):
|
|
ec2 = boto3.resource('ec2')
|
|
print("Fetching snapshots for volume " + VOLUME_ID)
|
|
volume = ec2.Volume(id=VOLUME_ID)
|
|
snapshots = volume.snapshots.all()
|
|
snapshot_ids = []
|
|
|
|
for snapshot in snapshots:
|
|
shared = snapshot.describe_attribute(Attribute='createVolumePermission')
|
|
already_shared = False
|
|
|
|
for i in range(0, len(shared['CreateVolumePermissions'])):
|
|
if 'UserId' in shared['CreateVolumePermissions'][i] and shared['CreateVolumePermissions'][i]['UserId'] == TARGET_ACCOUNT:
|
|
already_shared = True
|
|
break
|
|
|
|
if not already_shared:
|
|
print("Sharing " + snapshot.id)
|
|
snapshot_ids.append(snapshot.id)
|
|
snapshot.modify_attribute(
|
|
Attribute='createVolumePermission',
|
|
OperationType='add',
|
|
UserIds=[TARGET_ACCOUNT])
|
|
|
|
return {
|
|
'statusCode': 200,
|
|
'body': json.dumps(snapshot_ids)
|
|
}
|
|
|