2014-04-03 140 views
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中,该vpc也分配一个公共ip地址。将EC2实例启动到vpc中并使用公共IP

+0

它是做什么的?你能提供一些日志吗? – Rico

+0

谢谢!我总是忘记检查我的日志!我会发布我现在只是为了将来参考。 – swiftie

+1

您可以将您的解决方案添加为答案吗? – Rico

回答

3

正确格式: 我忘了在实例网络接口规范中添加设备索引。 我为安全组创建了字符串列表。 然后我创建createnetworkinterfacerequest对象并添加子网ID和安全组。 然后创建createnetworkinterfaceresponse对象并创建接口。 接下来,我创建instancenetworkinterfacespecification项并将其添加到列表中。 最后我运行runinstancerequest和bam,它工作。

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

为什么你创建“cnireq”? SubnetId = cniresp.NetworkInterface.SubnetId应该是您用于cni的指定最终子网ID,在这里:“subnet-1234” –

相关问题