Day 20 – Creating and Deleting EBS Snapshots using Boto3
Welcome to Day 20 of 101 Days of DevOps. The topic for today is Creating and deleting EBS snapshots using Boto3.
This is another common use case we use to encounter daily DevOps jobs where we need to create and clean up snapshots per company policy(e.g., every 30 or 60 days). One of the primary reasons to create a snapshot is for backup, and cleanup is to save costs.
To write this code, we need to follow the series of steps
Step1: Importing the standard library boto3
Step2: Create the ec2 resource.
ec2 = boto3.resource("ec2")
Step3: Iterate over all the ec2 volumes to get the volume id and stored it in the volume variable.
for vol in ec2.volumes.all(): vol_id = vol.id volume = ec2.Volume(vol.id)
Step4: To create a snapshot, we need to use the create_snapshot() method.
Deleting snapshots is similar to how we perform cleaning up AMI on Day 19(obviously, the method we use to delete snapshots is different)https://www.101daysofdevops.com/courses/101-days-of-devops/lessons/day-19/. I already explain the process in the below video, but please mention it in the comment section if you need any help.
If you like to dig deeper into the AWS concept, please feel free to check my book
- Website: https://101daysofdevops.com/
- Linkedin: https://www.linkedin.com/in/prashant-lakhera-696119b/
- Twitter: @100daysofdevops OR @lakhera2015
- Facebook: https://www.facebook.com/groups/795382630808645/
- Medium: https://medium.com/@devopslearning
- GitHub: https://github.com/100daysofdevops/100daysofdevops
- YouTube Channel: https://www.youtube.com/user/laprashant/videos
- Slack: https://join.slack.com/t/100daysofdevops/shared_invite/zt-au03logz-YfDUp_FJF4rAUeDEbgWmsg
- Reddit: r/101DaysofDevops
- Meetup: https://www.meetup.com/100daysofdevops/