Home > other >  How do I refer to an existing subnet and existing securitygroup parametres and refer to them within
How do I refer to an existing subnet and existing securitygroup parametres and refer to them within

Time:04-23

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

I tried changing parameters for subnet and security group from hardcoded ID to variable, but get the error "Value of property SubnetId must be of type String".

 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

CodePudding user response:

AWS::EC2::Instance can be only in a single subnet, not in multiple ones. So you have to specify exactly one subnet, not a list of subnets.

 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
  • Related