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) }