Parameter referencing in AWS

Asked by At

In my code below I'm trying to reference a parameter which I created for creating subnets\cidr block for EC2 instance.

I guess, since I created CFCidrVPC1 as string and the actual IP address is not a string.

Something I am missing here.

Can someone guide me here. The reason I'm trying to do this is reuse the code in the future. I also tried to use Ref function within Fn::Cidr funtion but that didn't work either. so replace it with cidr block.

The error I'm getting is "Value of property SubnetId must be of type String".

Please find my full script:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Creating template to deploy 3 ec2 instances in 3 different AZs",
  "Parameters": {
    "CFCidrVPC1": {
      "Type": "String",
      "Default": "10.10.0.0/16"
    },
    "CFCidrVPC2": {
      "Type": "String",
      "Default": "10.20.0.0/16"
    },
    "CFEC2Instancetype": {
      "Type": "String",
      "Default": "t2.micro",
      "Description": " Only t2.micro is available in the free tier account hence no any other options"
    }
  },
  "Resources": {
    "VPC1": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": {
          "Ref": "CFCidrVPC1"
        },
        "EnableDnsHostnames": "False",
        "EnableDnsSupport": "False",
        "InstanceTenancy": "default"
      }
    },
    "VPC2": {
      "Type": "AWS::EC2::VPC",
      "Properties": {
        "CidrBlock": {
          "Ref": "CFCidrVPC2"
        },
        "EnableDnsHostnames": "False",
        "EnableDnsSupport": "False",
        "InstanceTenancy": "default"
      }
    },
    "CFsubnet1": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "AvailabilityZone": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": ""
            }
          ]
        },
        "CidrBlock": {
          "Fn::Select": [
            "0",
            {
              "Fn::Cidr": [
                "10.10.0.0/16",
                "2",
                "8"
              ]
            }
          ]
        },
        "VpcId": {
          "Ref": "CFCidrVPC1"
        }
      }
    },
    "CFsubnet2": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "AvailabilityZone": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": ""
            }
          ]
        },
        "CidrBlock": {
          "Fn::Select": [
            "1",
            {
              "Fn::Cidr": [
                "10.10.0.0/16",
                "2",
                "8"
              ]
            }
          ]
        },
        "VpcId": {
          "Ref": "CFCidrVPC1"
        }
      }
    },
    "CFsubnet3": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "AvailabilityZone": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": ""
            }
          ]
        },
        "CidrBlock": {
          "Fn::Select": [
            "0",
            {
              "Fn::Cidr": [
                "10.20.0.0/16",
                "2",
                "8"
              ]
            }
          ]
        },
        "VpcId": {
          "Ref": "CFCidrVPC2"
        }
      }
    },
    "CFsubnet4": {
      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "AvailabilityZone": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": ""
            }
          ]
        },
        "CidrBlock": {
          "Fn::Select": [
            "1",
            {
              "Fn::Cidr": [
                "10.10.0.0/16",
                "2",
                "8"
              ]
            }
          ]
        },
        "VpcId": {
          "Ref": "CFCidrVPC2"
        }
      }
    },
    "FirstECSinVPC1AZ1": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "AvailabilityZone": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": ""
            }
          ]
        },
        "ImageId": "ami-005bdb005fb00e791",
        "InstanceType": {
          "Ref": "CFEC2Instancetype"
        },
        "SubnetId": {
          "ref": "CFsubnet1"
        }
      }
    },
    "FirstECSinVPC1AZ2": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "AvailabilityZone": {
          "Fn::Select": [
            "1",
            {
              "Fn::GetAZs": ""
            }
          ]
        },
        "ImageId": "ami-005bdb005fb00e791",
        "InstanceType": {
          "Ref": "CFEC2Instancetype"
        },
        "SubnetId": {
          "ref": "CFsubnet2"
        }
      }
    },
    "FirstECSinVPC2AZ1": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "AvailabilityZone": {
          "Fn::Select": [
            "0",
            {
              "Fn::GetAZs": ""
            }
          ]
        },
        "ImageId": "ami-005bdb005fb00e791",
        "InstanceType": {
          "Ref": "CFEC2Instancetype"
        },
        "SubnetId": {
          "ref": "CFsubnet3"
        }
      }
    },
    "FirstECSinVPC2AZ2": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "AvailabilityZone": {
          "Fn::Select": [
            "1",
            {
              "Fn::GetAZs": ""
            }
          ]
        },
        "ImageId": "ami-005bdb005fb00e791",
        "InstanceType": {
          "Ref": "CFEC2Instancetype"
        },
        "SubnetId": {
          "ref": "CFsubnet4"
        }
      }
    }
  }
}

1 Answers

1
cpt_guy On Best Solutions

Everything in cloudformation is case sensitive.

You have:

"SubnetId": {
   "ref": "CFsubnet2"
    }

Try:

"SubnetId": {
   "Ref": "CFsubnet2"
    }

For all occurrences .