【问题标题】:Launching EC2 instance into vpc and with a public IP将 EC2 实例启动到 vpc 并使用公共 IP
【发布时间】:2014-05-15 18:19:43
【问题描述】:
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 地址。

【问题讨论】:

  • 它有什么作用?你能提供一些日志吗?
  • 谢谢!我总是忘记检查我的日志!我将发布我现在拥有的内容以供将来参考。
  • 您可以添加您的解决方案作为答案吗?

标签: c# amazon-web-services amazon-ec2 ip amazon-vpc


【解决方案1】:

正确的格式: 我忘记在实例网络接口规范中添加设备索引。 我为安全组创建字符串列表。 然后我创建 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
};

【讨论】:

  • 为什么要创建“cnireq”? SubnetId = cniresp.NetworkInterface.SubnetId 应该是您用于 cni 的指定最终子网 ID,此处:“subnet-1234”
猜你喜欢
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 2013-10-02
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多