Created
February 25, 2019 20:11
-
-
Save jewzaam/1a347da464a7353b790aff88373991d5 to your computer and use it in GitHub Desktop.
Snapshot all OCP 4.0 EBS volumes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
ec = boto3.client('ec2') | |
def lambda_handler(event, context): | |
# https://serverlesscode.com/post/lambda-schedule-ebs-snapshot-backups/ | |
reservations = ec.describe_instances( | |
Filters=[ | |
{'Name': 'tag-key', 'Values': ['openshiftClusterID']}, | |
] | |
)['Reservations'] | |
instances = sum( | |
[ | |
[i for i in r['Instances']] | |
for r in reservations | |
], []) | |
print "Found this many instances: %d" % (len(instances)) | |
for instance in instances: | |
for dev in instance['BlockDeviceMappings']: | |
if dev.get('Ebs', None) is None: | |
# skip non-EBS volumes | |
continue | |
vol_id = dev['Ebs']['VolumeId'] | |
print "Found EBS volume %s on instance %s" % ( | |
vol_id, instance['InstanceId']) | |
ec.create_snapshot( | |
VolumeId=vol_id, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment