Question

I have written the code to create an AMI using the C# SDK but haven't been able to create the AMI with only the Root Volume. By default it creates the AMI with all the EBS volumes attached to the instance. E.g. If it has 4 volumes it would create an AMI with 4 snapshots associated to it whereas I only need the C:\drive (Root Drive) included. I know that you have to use BlockDeviceMapping to specify the volume but not sure about the implementation any help would be appreciated.

public static void CreateAMI(string InstanceID, string AMIName, string AMIDescription)
{
    try
    {

        Console.WriteLine("Creating AMI for InstanceID" + InstanceID);

        AmazonEC2Config config = new AmazonEC2Config();
        config.ProxyHost = ConfigurationManager.AppSettings["PROXYHOST"];
        config.ProxyPort = Convert.ToInt32(ConfigurationManager.AppSettings["PROXYPORT"])
        config.ServiceURL = "https://ec2." + Program.options.Region + ".amazonaws.com";

        AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client(Program.options.AccessKey, Program.options.SecretKey, config);

        CreateImageRequest rq = new CreateImageRequest();

        rq.InstanceId = InstanceID;
        rq.Name = AMIName;
        rq.Description = AMIDescription;
        rq.NoReboot = true;

        /* BlockDeviceMapping BMapping = new BlockDeviceMapping();
BMapping.DeviceName = "/dev/sda1";
rq.BlockDeviceMapping.Add(BMapping); */

        CreateImageResponse rs = ec2.CreateImage(rq);
        string AMIid = rs.CreateImageResult.ImageId;
        Console.WriteLine("AMI Created with AMIid: " + AMIid);
        Console.ReadLine();
    }
    catch (Exception err)
    {
        Console.WriteLine(err.Message + "/n" + err.StackTrace);
    }

}
Was it helpful?

Solution

I think you need to specif your other device(s) EBS property to Null and NoDevice to something to make them not apart of your AMI. Your code should looks something like this.

//Create Drive Mapping List
var blockDeviceMappingList = new List<BlockDeviceMapping>();

//Create Mappings
var blockDeviceMapping = new BlockDeviceMapping();
var blockDeviceMapping2 = new BlockDeviceMapping();

//Specif a mount point of the drive you want (root)
blockDeviceMapping.DeviceName = "/dev/sda1";
var ebsBlockDevice = new EbsBlockDevice();

//Set something other than null constructor or u get an error about EBS not set. Likely has to do with how they build the request to send to the server
ebsBlockDevice.VolumeType = VolumeType.Standard; 
blockDeviceMapping.Ebs = ebsBlockDevice;

//Specif a mount point of the unwanted drive and set EBS to null and NoDevice
blockDeviceMapping2.DeviceName = "/dev/sdf";
blockDeviceMapping2.Ebs = null;
blockDeviceMapping2.NoDevice = string.Empty;

//Add the mappings to the list
blockDeviceMappingList.Add(blockDeviceMapping);
blockDeviceMappingList.Add(blockDeviceMapping2);

//Setup Request
createImageRequest.BlockDeviceMappings = blockDeviceMappingList;

=)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top