我是网络菜鸟。我正在通过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个AZ。其他地区,如us-east-1
,有更多。您所在地区的AZ可以使用以下方法找到:
▶ 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个子网的AZ,其余为1。