我正在捆绑这个云形成堆栈,但是当创建到达DBSubnetGroup时,它会失败,消息是:属性SubnetIds的值必须是字符串类型的列表。谢谢你的帮助。除了DBSubnetGroup之外,其他都很正常。
AWSTemplateFormatVersion: 2010-09-09
Description: My First CloudFormation Template
Resources:
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.1.0.0/16
EnableDnsHostnames: true
EnableDnsSupport: true
InstanceTenancy: default
Tags:
- Key: Name
Value: VPC for Activity Number 1
MySubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
CidrBlock: 10.1.1.0/24
MapPublicIpOnLaunch: true
AvailabilityZone: "us-east-1c"
Tags:
- Key: Name
Value: Public Subnet
MySubnetA:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
CidrBlock: 10.1.2.0/24
MapPublicIpOnLaunch: true
AvailabilityZone: "us-east-1a"
Tags:
- Key: Name
Value: Public Subnet A
MySubnetB:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
CidrBlock: 10.1.3.0/24
MapPublicIpOnLaunch: true
AvailabilityZone: "us-east-1b"
Tags:
- Key: Name
Value: Public Subnet B
这就是创建失败的开始:
DBSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
DependsOn:
- MyVPC
- MySubnet
- MySubnetA
- MySubnetB
Properties:
DBSubnetGroupDescription: Subnets to lauch the database
SubnetIds:
-!Ref MySubnet
-!Ref MySubnetA
-!Ref MySubnetB
您应该在-
和内在函数!Ref
之间有一个空格,以便正确解析。遗憾的是,CloudFormation只有在创建堆栈时才会检测到这一点。
DBSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
DependsOn:
- MyVPC
- MySubnet
- MySubnetA
- MySubnetB
Properties:
DBSubnetGroupDescription: Subnets to lauch the database
SubnetIds:
- !Ref MySubnet
- !Ref MySubnetA
- !Ref MySubnetB
Subnets:
Description: The array of Subnet IDs for the Subnet group
Type: List<AWS::EC2::Subnet::Id>
如果您使用Subnetid作为EC2资源的引用,它将从您在参数中作为数组传递的列表中传递值。但是在subnetid中,它只允许字符串,因此你必须更改subnetid的数据类型。
Resources:
Ec2Resourse:
Type: AWS::EC2::Instance
Properties:
SubnetId: !Join [",", [!Select [0, !Ref Subnets]]]
上面的代码将数组更改为字符串,并将部署我们从子网列表中选择的EC2。