如何引用现有子网和现有安全组参数并在 YAML cloudformation 模板中引用它们?
我尝试将子网和安全组的参数从硬编码 ID 更改为变量,但收到错误“属性 SubnetId 的值必须为字符串类型”。
Parametres:
PublicSecurityGroup:
Description: WebSecurityGroup
#Type: String
Type: AWS::EC2::SecurityGroup::Id
#Default: sg-081d3059c58edb3b6
PublicSubnet:
Description: Web/PublicSecurityGroup
#Type : String
#Default: subnet-0b3ea12c33b327f0a
Type: 'List<AWS::EC2::Subnet::Id>'
Resources:
WebInstance:
Type: AWS::EC2::Instance
Properties:
KeyName:
Ref: KeyName
InstanceType:
!FindInMap [
EnvironmentToInstanceType,
!Ref EnvironmentInstanceType,
InstanceType,
]
ImageId: !Ref ImageId
# AvailabilityZone: !Ref AvailabilityZone
#SubnetId: !Ref PublicSubnet
SubnetId:
- Ref: PublicSubnet
SecurityGroupIds:
- Ref: PublicSecurityGroup
最佳答案
AWS::EC2::Instance
只能位于单个子网中,不能位于多个子网中。因此,您必须指定一个子网,而不是子网列表。
Parametres:
PublicSecurityGroup:
Description: WebSecurityGroup
#Type: String
Type: AWS::EC2::SecurityGroup::Id
#Default: sg-081d3059c58edb3b6
PublicSubnet:
Description: Web/PublicSecurityGroup
#Type : String
#Default: subnet-0b3ea12c33b327f0a
Type: 'AWS::EC2::Subnet::Id'
Resources:
WebInstance:
Type: AWS::EC2::Instance
Properties:
KeyName:
Ref: KeyName
InstanceType:
!FindInMap [
EnvironmentToInstanceType,
!Ref EnvironmentInstanceType,
InstanceType,
]
ImageId: !Ref ImageId
# AvailabilityZone: !Ref AvailabilityZone
#SubnetId: !Ref PublicSubnet
SubnetId: !Ref PublicSubnet
SecurityGroupIds:
- Ref: PublicSecurityGroup
关于amazon-web-services - 如何引用现有子网和现有安全组参数并在 YAML cloudformation 模板中引用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71953888/