Automate EBS Snapshot in Amazon EC2 with CSharp

23. November 2011 22:22

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)

Tags:

Amazon Cloud

How to Install the EC2 API Command Line Tool for Windows

17. February 2011 01:38

Getting the EC2 Command Line API Tools to work on windows was beneficial for me so I thought I would share it with others. I found it to be beneficial because I was in a situation where I wanted to change the instance size one one of my Windows instances. You can't accomplish this from the amazon management console, but you can from their api command line tool.

This was installed on Windows 7 Ultimate 64-bit


First step is to download the tools from the following URL

http://aws.amazon.com/developertools/351?_encoding=UTF8&jiveRedirect=1


Once you have the tool downloaded, extract the file to a directory of your choosing.  I chose to extract the tool into C:\EC2\

Second step is that you will need your x.509 certs from your amazon account.

To accomplish this

  • I went to my "account" page within AWS Management Console
  • Clicked on Security Credentials
  • Under access credentials sections I clicked Create new certificate and downloaded the private key and certificate when prompted too.


Note: Keep track of where you downloaded them. I just put them in my C:\EC2\ directory to keep things in one place


Third, you will need to make sure you have the Java SDK installed.  I downloaded it here


http://www.oracle.com/technetwork/java/javase/downloads/index.html


Fourth, you will need to enter Environment Variables on your Windows system.

I entered in the following environment variables

Variable: EC2_CERT
Value: Path to your cert file name you got from amazon
Example: C:\EC2\cert-XYXYXYXXYXYXY.pem


Variable: EC2_HOME
Value:  C:\EC2\

This needs to be the location where the api tools are

Variable: EC2_PRIVATE_KEY
Value: Path to the file of your private key you got from amazon
Example: C:\EC2\pk-XYXYXYXYXYXXYXY.pem

Variable: JAVA_HOME
Value: C:\Program Files\Java\jdk1.6.0_23\

This needs to be the path where you installed the Java SDK


This should do it. You should now be able to run the following command to make sure it is working properly:

ec2-describe-regions

 

To change the instance type of an instance, first stop the instance, then run the following command

ec2-modify-instance-attribute yourInstanceID -t m1.xlarge

In the command above replace yourInstanceID with your actual instance id and then change m1.xlarge to the actual size you want it to be.

Then start your instance back up and you are good to go with the new size.


Here is a link to the API reference doc.

http://docs.amazonwebservices.com/AWSEC2/latest/CommandLineReference/

Tags:

Amazon Cloud




My Random Thought

I think the OCW is a great thing to have available to those who are in school, just finished school or just want to educate themself

http://ocwconsortium.org/

John On Twitter

Discounts