Ok, so I've had this code for a long time now (over a year) and have never had a chance (or forgot) to post this here. This is basically a solution I came up with to help automate my EBS snapshots in my Amazon EC2 environment. The code does the following:
- Create snapshots for volume names specified in the code
- Delete snapshots that are 2 days old for the volume names specified in the code
I have ripped out code from my overall solution. I have in my code, additional functionality for logging and more volumes, but this sample should get you started and on your way.
You will need the following for this to work:
This code is created as a console application. You can leave it like that or switch it to a windows service, so you don't have to worry about it as much. Also, this code could be optimized so feel free to do so, and post your comment here if you'd like.
The zipped filed contains all the code you will need. I'll give a short explanation of each file
EC2\BaseEC2.cs -> This is the base class that contains the property that creates your EC2 client based off of your keys
EC2\Snapshot.cs -> This contains the following methods
public void MaintainSnapshots()
/// <summary>
/// Returns information regarding the snapshot ID
/// </summary>
/// <param name="snapshotId"></param>
/// <returns></returns>
private DescribeSnapshotsResult DescribeSnapshot(string snapshotId)
/// <summary>
/// Return the snapshot ID based off the snapshot description filter
/// </summary>
/// <param name="snapDescription"></param>
/// <returns></returns>
private string GetSnapshotIDByDesc(string snapDescription)
/// <summary>
/// Returns a collection of snapshot results based off a list of snapshot ID's
/// </summary>
/// <param name="lstSnapshotIDs"></param>
/// <returns></returns>
private DescribeSnapshotsResult DescribeSnapshot(List<string> lstSnapshotIDs)
/// <summary>
/// Creates a single snapshot
/// </summary>
/// <param name="name"></param>
/// <param name="volumeID"></param>
/// <returns></returns>
private CreateSnapshotResult CreateSnapshot(string description, string volumeID)
/// <summary>
/// Deletes a single snapshot
/// </summary>
/// <param name="snapshotId"></param>
/// <returns></returns>
private bool DeleteSnapshot(string snapshotId)
EC2\Volume.cs -> contains and enum for the volume names and 2 methods
/// <summary>
/// Returns a single volume ID givien the volume name provided
/// </summary>
/// <param name="volumeName"></param>
/// <returns></returns>
public static string GetVolumeID(XDevVolume volumeName)
/// <summary>
/// Returns a list of volumes Id given the specified volume names in the list provided
/// </summary>
/// <param name="volumeNames"></param>
/// <returns></returns>
public static List<string> GetVolumeID(List<string> volumeNames)
App.config -> where you enter in your keys
Program.cs -> Main method that just calls the MaintainSnapshots method from the object Snapshot
And that's it. If you guys have a different way of doing so please let me know. I'm sure this is not the best way, but gets the job done for a simple task like this.
As always use this code at your own risk, test and modify as needed to fit your solution
Download Solution Below
XDev.AWS.rar (9.77 kb)