Automate EBS Snapshot Deletion For Orphan Snapshots

Automate EBS Snapshot Deletion For Orphan Snapshots

Play this article

In this blog we are going to automate EBS Snapshot deletion for Old Snapshots using Python SDK for AWS.

If you are making point-in-time backups for your EBS volumes , you need to make sure you understand AWS snapshot pricing so there are no surprises on your bill.

To understand EBS Snapshot Pricing its always recommended to check latest AWS EBS snapshot pricing on AWS official page here

EBS Snapshots are a point in time copy of your data, and can be used to enable disaster recovery, migrate data across regions and accounts, and improve backup compliance. You can create and manage your EBS Snapshots through the AWS Management Console, the AWS CLI, or the AWS SDKs.

Prerequisite

An AWS Account An IAM User with:

  • AWS Management Console access to verify your EC2 instances launched,listed and terminated.
  • The IAM permissions required to perform IAM, EC2, and CloudWatch activities. IAM policy creation and AWS Application Programming Interface (API) permissions are outside this article’s scope. Always adhere to the principle of least privilege when authorizing accounts to perform actions. Administrative access to an EC2 Instance.
  • Install awscli using aws official documentation here
  • Install python and boto3
  • Configure aws cli by using official documentation here

Launch AWS EC2 Instance with Tags using python script

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the client for EC2
    client = boto3.client('ec2')
    
  3. To launch EC2 instances we have to use method "run_instances()". This method helps us launch AWS EC2 instances based on our requirement.
    response =client.run_instances(<arguments>)
    
  4. Goto link where you will find all arguments list. Based on your requirement you can put this arguments to launch your EC2 instances. This document also mentions datatype of the parameter.
    Note:- Arguments which are with "REQUIRED" tags mentioned in documentation is mandatory, if you don't specify those arguments code block to launch EC2 will not execute successfully.
    Example:- "MinCount", "MaxCount".
    Below code will launch EC2 instance based on your provided input.
    resp=client.run_instances(ImageId='<Image ID>',
                           InstanceType='t2.micro',
                           MinCount=1,
                           MaxCount=1,
                           KeyName='<Your Key Name>',
                           TagSpecifications=[
                               {
                                   'ResourceType': 'instance',
                                   'Tags': [{'Key': 'Name','Value': 'Linux Server',{'Key': 'Env','Value': 'Production'}]
                               },
                           ],
                           )
    
  5. Once above method will run it will launch EC2 and launched EC2 information will be captured in variable "resp" . It will return infomation in dictonary.

To view entire github code please click here

Code python script to Automate EBS Snapshot

  1. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  2. We will invoke the service resource for EC2.
    Note:- Make sure to explore EC2 service resources here
    ec2 = boto3.resource('ec2')
    
  3. Create a variable which we will use to store our EC2 instances tags that we have attached to our EC2 instances above.
    tagfilters=[
     {
        'Name': 'tag:Env',
        'Values':['Production']
     }
    ]
    
  4. Now we will write for loop which will capture the instance information in instance variable while traversing through EC2 resources using iterator and will fetch instances based on tags.
    for instance in ec2.instances.filter(Filters=tagfilters):
    
  5. Now we will use another for loop which will fetch EBS volume ids attached to these instances.
    for instance in ec2.instances.filter(Filters=tagfilters):
     for volume in instance.volumes.all():
    
  6. We will now create the snapshot of the EBS volumes with tags using EC2 resource method "create_snapshot()" and will store it in variable "snapshot".
    Checkout documentation for this method here
    for instance in ec2.instances.filter(Filters=tagfilters):
     for volume in instance.volumes.all():
         snapshot=volume.create_snapshot(Description='Snapshot created via script',
                                         TagSpecifications=[
                                             {
                                                 'ResourceType': 'snapshot',
                                                 'Tags': [{'Key': 'Env', 'Value': 'Production'}, ]
                                             },
                                         ],
                                         )
    
    To view entire github code please click here

Code python script to Automate EBS Snapshot Deletion

  1. The datetime module supplies classes for manipulating dates and times. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation.
    We are going to use datetime, timedelta, timezone from module datetime.
    Note:- Make sure to checkout datetime module official documentation here
    from datetime import datetime, timedelta, timezone
    
  2. Python code in one module gains access to the code in another module by the process of importing it. The import statement combines two operations it searches for the named module, then it binds the results of that search to a name in the local scope.
    import boto3
    
  3. We will invoke the service resource for EC2.
    Note:- Make sure to explore EC2 service resources here
    ec2 = boto3.resource('ec2')
    
  4. Create a variable which we will use to store our EC2 instances tags that we have attached to our EC2 instances above.
    tagfilters=[
     {
        'Name': 'tag:Env',
        'Values':['Production']
     }
    ]
    
  5. Now we will use "snapshots.filter()" method to fetch snapshot information based on our specified tag "tagfilters" and store it in variable "snapshots".
    Make sure to checkout "snapshots.filter()" method official documentation here
    snapshots=ec2.snapshots.filter(Filters=tagfilters)
    
  6. Now we will write for loop to traverse through our output stored in variable "snapshots".
    for snapshot in snapshots:
    
  7. We will now fetch the time stamp when the snapshot was initiated using inbuilt "start_time" and store it in variable "create_time" Refer documentation here
    for snapshot in snapshots:
     create_time=snapshot.start_time
    
  8. Lets perform arithmetic operation to fetch deletion time for our current snapshot and store in variable "delete_time".
    for snapshot in snapshots:
     create_time=snapshot.start_time
     delete_time=datetime.now(tz=timezone.utc) - timedelta(days=10)
    
  9. We will now write if else by using which snapshot which is 10 days old will be deleted automatically.
    for snapshot in snapshots:
     create_time=snapshot.start_time
     delete_time=datetime.now(tz=timezone.utc) - timedelta(days=10)
     if delete_time > create_time:
         print('Create time of snapshot is {} And Delete time of snapshot is {}'.
               format(create_time,delete_time))
         snapshot.delete()
         print('{} has been deleted'.format(snapshot.snapshot_id))
     else:
         print('Existing Snapshot {} is not less that 10 days old'.format(snapshot.snapshot_id))
    
    To view entire github code please click here

Conclusion

Make sure you understand AWS snapshot pricing so there are no surprises on your bill and by using this automation you can delete old snapshots based on your organization EBS Snapshot retention policy.

Stay Tuned For My Next Blogs...

So, did you find my content helpful? If you did or like my other content, feel free to buy me a coffee. Thanks.

Did you find this article valuable?

Support Dheeraj Choudhary by becoming a sponsor. Any amount is appreciated!