我已编写代码以使用 C# SDK 创建 AMI,但无法仅使用根卷创建 AMI。默认情况下,它会创建 AMI,并将所有 EBS 卷附加到实例。例如,如果它有 4 个卷,它将创建一个带有 4 个与之关联的快照的 AMI,而我只需要包含 C:\drive(根驱动器)。我知道您必须使用 BlockDeviceMapping 来指定音量,但不确定实现任何帮助将不胜感激。
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);
}
}