简体   繁体   中英

Launching EC2 instance into vpc and with a public IP

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
};

This does not launch an instance into the vpc with a public ip address. What is wrong with it? I want to launch an instance into a vpc that also assigns it a public ip address.

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
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM