如何引用现有子网和现有安全组参数并在 YAML cloudformation 模板中引用它们

How do I refer to an existing subnet and existing securitygroup parametres and refer to them within a YAML cloudformation template

如何引用现有子网和现有安全组参数并在 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