2
InstanceNetworkInterfaceSpecification vpc = new InstanceNetworkInterfaceSpecification()
{
SubnetId = "subnet-sadfsadf",
    AssociatePublicIpAddress = true,
    DeleteOnTermination = false

};
List<InstanceNetworkInterfaceSpecification> temp= new List<InstanceNetworkInterfaceSpecification>();
            temp.Add(vpc);

//Create and initialize a RunInstanceRequest
RunInstancesRequest newInstanceRequest = new RunInstancesRequest()
{
   ImageId = appdbAMI,
   InstanceType = appdbType,
   MinCount = 1,
   MaxCount = appdbQuantity,
   KeyName = ec2Key,
   NetworkInsterfaces = temp,
   BlockDeviceMappings = resp.Images[0].BlockDeviceMappings
   //DisableApiTermination = true
};

这不会将实例启动到具有公共 IP 地址的 vpc 中。它有什么问题?我想将一个实例启动到一个 vpc 中,并为其分配一个公共 IP 地址。

4

1 回答 1

3

CORRECT FORMAT: I forgot to add the device index in the instance network interface specification. I create the string list for the security groups. I then create the createnetworkinterfacerequest object and add the subnet id and security groups. Then create the createnetworkinterfaceresponse object and create the interface. Next I create the instancenetworkinterfacespecification item and add it to the list. Last I run the runinstancerequest and bam, it worked.

List<string> secgrps = new List<string>(new string[] { "sg-2143", "sg-1234" });
CreateNetworkInterfaceRequest cnireq = new CreateNetworkInterfaceRequest()
{ 
SubnetId = "subnet-1234", 
Groups = secgrps 
};
CreateNetworkInterfaceResponse cniresp = ec2Client.CreateNetworkInterface(cnireq);
InstanceNetworkInterfaceSpecification inis = new InstanceNetworkInterfaceSpecification()
{ 
SubnetId = cniresp.NetworkInterface.SubnetId, 
Groups = secgrps, 
AssociatePublicIpAddress = true, 
DeviceIndex=0 
};
List<InstanceNetworkInterfaceSpecification> inisList = new List<InstanceNetworkInterfaceSpecification>();
inisList.Add(inis);

//Create and initialize a RunInstanceRequest
RunInstancesRequest newInstanceRequest = new RunInstancesRequest()
{
    ImageId = appdbAMI,
    InstanceType = appdbType,
    MinCount = 1,
    MaxCount = appdbQuantity,
    KeyName = ec2Key,
    //SecurityGroups = appdbSecurity,
    NetworkInterfaces = inisList,
    BlockDeviceMappings = resp.Images[0].BlockDeviceMappings
    //DisableApiTermination = true
};
于 2014-04-05T00:34:59.873 回答