amazon-web-services - 通过cloudformation创建VPC时的子网问题

标签 amazon-web-services networking aws-cloudformation amazon-vpc

我是网络新手。我正在通过 Cloudformation 创建 VPC。其中,我必须创建 4 个子网。当我运行包含的模板时,我看到以下错误: 模板错误:Fn::Select 无法选择索引 3 处不存在的值

但是,当我用 3 个子网创建它时,就没有问题了。

我的模板示例:

Parameters:

  VpcBlock:
    Type: String
    Default: 192.168.0.0/16
    Description: The CIDR range for the VPC. This should be a valid private (RFC 1918) CIDR range.

  Subnet01Block:
    Type: String
    Default: 192.168.0.0/14
    Description: CidrBlock for subnet 01 within the VPC

  Subnet02Block:
    Type: String
    Default: 192.168.64.0/14
    Description: CidrBlock for subnet 02 within the VPC

  Subnet03Block:
    Type: String
    Default: 192.168.128.0/14
    Description: CidrBlock for subnet 03 within the VPC

  Subnet04Block:
    Type: String
    Default: 192.168.192.0/14
    Description: CidrBlock for subnet 04 within the VPC

Resources:
  Subnet01:
    Type: AWS::EC2::Subnet
    Metadata:
      Comment: Subnet 01
    Properties:
      AvailabilityZone:
        Fn::Select:
        - '0'
        - Fn::GetAZs:
            Ref: AWS::Region
      CidrBlock:
        Ref: Subnet01Block
      VpcId:
        Ref: VPC
      Tags:
      - Key: Name
        Value: !Sub "${AWS::StackName}-Services-Subnet01"

  Subnet02:
    Type: AWS::EC2::Subnet
    Metadata:
      Comment: Subnet 02
    Properties:
      AvailabilityZone:
        Fn::Select:
        - '1'
        - Fn::GetAZs:
            Ref: AWS::Region
      CidrBlock:
        Ref: Subnet02Block
      VpcId:
        Ref: VPC
      Tags:
      - Key: Name
        Value: !Sub "${AWS::StackName}-Services-Subnet02"

  Subnet03:
    Type: AWS::EC2::Subnet
    Metadata:
      Comment: Subnet 03
    Properties:
      AvailabilityZone:
        Fn::Select:
        - '2'
        - Fn::GetAZs:
            Ref: AWS::Region
      CidrBlock:
        Ref: Subnet03Block
      VpcId:
        Ref: VPC
      Tags:
      - Key: Name
        Value: !Sub "${AWS::StackName}-Services-Subnet03"

  Subnet04:
    Type: AWS::EC2::Subnet
    Metadata:
      Comment: Subnet 04
    Properties:
      AvailabilityZone:
        Fn::Select:
        - '3'
        - Fn::GetAZs:
            Ref: AWS::Region
      CidrBlock:
        Ref: Subnet04Block
      VpcId:
        Ref: VPC
      Tags:
      - Key: Name
        Value: !Sub "${AWS::StackName}-Services-Subnet04"

我正在 us-west-2 区域部署此模板。 我在这里做错了什么吗?

最佳答案

您的问题是 AWS 中的不同区域具有不同数量的可用区 (AZ) ( docs )。

由于您位于 us-west-2 区域,因此您只有 3 个可用区。其他地区,例如 us-east-1,则有更多。可以使用以下方式找到您所在区域的可用区:

▶ aws ec2 describe-availability-zones --region us-west-2 --query 'AvailabilityZones[].ZoneName' 
[
    "us-west-2a", 
    "us-west-2b", 
    "us-west-2c"
]

同时,内在函数 Fn::GetAZs将 AZ 作为数组返回给您。您引用了该数组的第 3 个元素(即第 4 个元素),但它不存在,这就是您看到该错误消息的原因。

您可能需要在移动到不同区域、拥有不同数量的子网或让一个可用区拥有 2 个子网而其余可用区拥有 1 个子网之间做出选择。

关于amazon-web-services - 通过cloudformation创建VPC时的子网问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54625629/

相关文章:

ios - AWS 认知 : immediately signed-put after sign-in

java - 什么是套接字绑定(bind)以及如何绑定(bind)地址?

java - 为什么 HTTP 响应没有给我指定的数据范围?

azure - 1 个 VM 连接到 2 个 VNET?

node.js - AWS 无服务器函数 : Can't access methods of aws-sdk objects. 。 ."is not a function"

在 Web 浏览器中运行 RStudio 的多个实例

amazon-web-services - `eb deploy` 错误输出 "Default subnet not found in us-west-2d"

amazon-web-services - Cloud Formation 条件资源属性 AWS Backup 资源

amazon-web-services - AWS : The CIDR '10.30.1.0/24' conflicts with another subnet

c++ - AWS SNSClient 发布调用无法到达端点