AWS Cloudformation Value of property SubnetIds must be of type List of String

Viewed 588

I am tying this cloudformation stack but when creation reaches DBSubnetGroup it fails with the message: Value of property SubnetIds must be of type List of String. I appreciate your help. All seems working fine except the 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      

That's where creation failure starts:

  DBSubnetGroup:
    Type: AWS::RDS::DBSubnetGroup
    DependsOn:
      - MyVPC
      - MySubnet
      - MySubnetA
      - MySubnetB
    Properties:
      DBSubnetGroupDescription: Subnets to lauch the database
      SubnetIds: 
        -!Ref MySubnet
        -!Ref MySubnetA  
        -!Ref MySubnetB    
        
      

2 Answers

You should have a space between - and the intrinsic function !Ref in order to be parsed correctly. Sadly, CloudFormation detects this only when the stack is created.

 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> 

if you are use the Subnetid as Reference for EC2 resources, it will pass the value from the list that you are passed in parameters as an array.but in subnetid it will only allow strings for that you have to change the datatype of your subnetid.

Resources:
  Ec2Resourse:
    Type: AWS::EC2::Instance
    Properties:
      SubnetId: !Join [",", [!Select [0, !Ref Subnets]]]  

the above code will change the array to string, and will deploy the EC2 which we have selected from the subnet drop list.

Related