简体   繁体   English

如何将 EC2 实例分配给子网内的固定 IP 地址?

[英]How do I assign an EC2 instance to a fixed IP address within a subnet?

I'm using CloudFormation to define a subnet and EC2 instance.我正在使用 CloudFormation 来定义子网和 EC2 实例。 I want to allocate a particular private IP address to an EC2 instance and have attempted to do so using the resource definitions such as those below.我想为 EC2 实例分配一个特定的私有 IP 地址,并尝试使用如下所示的资源定义来这样做。

When I try to deploy my template the creation of the NetworkInterface resource fails with the message 'Address is in subnet's reserved address range' .当我尝试部署我的模板时,NetworkInterface 资源的创建失败,并显示消息“地址在子网的保留地址范围内” I've tried to work this out but am not making progress.我试图解决这个问题,但没有取得进展。 How do I define my subnet with a range of private IP addresses which I can apply to my EC2 instances?如何使用可应用于我的 EC2 实例的私有 IP 地址范围定义我的子网?

Thanks.谢谢。

Note: The whole template contains other resources and so I've attempted to reduce it to the important components, hence the below is purposely incomplete.注意:整个模板包含其他资源,因此我尝试将其缩减为重要组件,因此以下内容故意不完整。

"SharedVPC": {
  "Type": "AWS::EC2::VPC",
  "Properties": {
      "CidrBlock": "10.10.0.0/16",
      "EnableDnsHostnames": true,
      "EnableDnsSupport": true,
      "InstanceTenancy": "default"
  }
},

"SharedVPCPrivateSubnet1": {
  "Type": "AWS::EC2::Subnet",
  "Properties": {
      "AvailabilityZone": "eu-west-1a",
      "CidrBlock": "10.10.129.0/24",
      "MapPublicIpOnLaunch": false,
      "VpcId": {
          "Ref": "SharedVPC"
      }
  }
},

"DbServerEC2Instance": {
  "Type": "AWS::EC2::Instance",
  "Properties": {
    "KeyName": "WindowsEC2",
    "InstanceType": "t2.micro",
    "AvailabilityZone": "eu-west-1a",
    "ImageId": {
      "Ref": "DbServerEC2ImageAMI"
    },
    "IamInstanceProfile": {
      "Ref": "EC2InstanceProfile"
    },
    "NetworkInterfaces": [{
      "NetworkInterfaceId": {
        "Ref": "DbServerEC2InstanceNetworkInterface"
      },
      "DeviceIndex" : "0"
    }]
  }
},

"DbServerEC2InstanceNetworkInterface": {
  "Type": "AWS::EC2::NetworkInterface",
  "Properties": {
    "Description": "eth0",
    "PrivateIpAddresses": [{
        "PrivateIpAddress": "10.10.129.2",
        "Primary": "true"
      },
      {
        "PrivateIpAddress": "10.10.129.3",
        "Primary": "false"
      }
    ],
    "SourceDestCheck": "true",
    "SubnetId": {
      "Ref": "SharedVPCPrivateSubnet1"
    }
  }
}

AWS reserves the first 4 IP addresses and the last IP of a subnets. AWS 保留子网的前 4 个 IP 地址和最后一个 IP。 These ranges within a subnet are not available.子网内的这些范围不可用。

The AWS documentation states the following: AWS 文档声明如下:

The first four IP addresses and the last IP address in each subnet CIDR block are not available for you to use, and cannot be assigned to an instance.每个子网 CIDR 块中的前四个 IP 地址和最后一个 IP 地址不可供您使用,并且不能分配给实例。 For example, in a subnet with CIDR block 10.0.0.0/24 , the following five IP addresses are reserved:例如,在 CIDR 块为10.0.0.0/24的子网中,保留以下五个 IP 地址:

  • 10.0.0.0 : Network address. 10.0.0.0 :网络地址。
  • 10.0.0.1 : Reserved by AWS for the VPC router. 10.0.0.1 :AWS 为 VPC 路由器保留。
  • 10.0.0.2 : Reserved by AWS. 10.0.0.2保留。 The IP address of the DNS server is the base of the VPC network range plus two. DNS 服务器的 IP 地址是 VPC 网络范围的基础加 2。 For VPCs with multiple CIDR blocks, the IP address of the DNS server is located in the primary CIDR.对于具有多个 CIDR 块的 VPC,DNS 服务器的 IP 地址位于主 CIDR 中。 We also reserve the base of each subnet range plus two for all CIDR blocks in the VPC.我们还为 VPC 中的所有 CIDR 块保留每个子网范围的基础加上两个。 For more information, see Amazon DNS server.有关更多信息,请参阅 Amazon DNS 服务器。
  • 10.0.0.3 : Reserved by AWS for future use. 10.0.0.3 :由 AWS 保留以备将来使用。
  • 10.0.0.255 : Network broadcast address. 10.0.0.255 :网络广播地址。 We do not support broadcast in a VPC, therefore we reserve this address.我们不支持在 VPC 中广播,因此我们保留此地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一子网范围内的 ec2 实例中分配/生成新 IP 地址? - How to allocate/generate new IP address in ec2 instance in same subnet range? 如何在私有子网中获取 EC2 实例私有 IP? - How to get EC2 instance private IP in a private subnet? 如何捕获CLI启动的EC2实例的IP地址以供后续使用? - How do I capture the IP address of a CLI-started EC2 instance for subsequent use? 如何将私有IP从其他子网附加到EC2实例? - How can I attach a private IP from a different subnet to an EC2 instance? 如何使用 v2 ruby​​ sdk 为 ec2 实例分配 IP 地址 - how to assign an ip address to an ec2 instance using the v2 ruby sdk 公共子网中的多个 EC2 实例应具有相同的 IP 地址用于传出流量 - Multiple EC2 instance in public subnet should have same IP address for outgoing traffic 没有弹性 IP 地址,ec2 实例无法访问公共子网中的互联网? - The ec2 instance can't access internet in a public subnet without a elastic ip address? 从 EC2 实例“分离弹性 IP 地址”后,将公共 IP 自动分配给 AWS EC2 实例 - Auto assign of public IP to AWS EC2 instance after "Dissociate Elastic IP address" from EC2 instance 我无法使用 ec2 实例 ip 地址加载页面 - I am unable to load a page with ec2 instance ip address 如何访问私有子网中的 EC2 实例? - How to access to EC2 Instance in private subnet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM