Home > Mobile >  How to filter subnets according to a VPC in CloudFormation's parameters section?
How to filter subnets according to a VPC in CloudFormation's parameters section?

Time:10-30

I want to list out the subnets only from specific VPC in my cloudformation parameters section

VPC:
    Description: VPC Id
    Type: AWS::EC2::VPC::Id 

Subnets:
    Description: Select Subnets (Minimum 2)
    Type: List<AWS::EC2::Subnet::Id>

The above displays all subnets (from other subnets also) but I want to show only subnets from selected VPC.

Is it possible? What is the workaround for the same?

CodePudding user response:

Is it possible?

No.

What is the workaround for the same?

There is none, unless you develop your own frotnend for deployment of templates.

CodePudding user response:

You can't list them that way, but you can prevent deployment if the chosen subnets are not in the VPC by using a CloudFormation rule:

{
  "Rules": {
    "IsSubnetInsideVPC": {
      "Assertions": [
        {
          "Assert": {
            "Fn::EachMemberEquals" : [
              {
                "Fn::ValueOfAll": [
                  "AWS::EC2::Subnet::Id",
                  "VpcId"
                ]
              },
              {
                "Ref": "VPC"
              }
            ]
          },
          "AssertDescription": "One or more subnets you selected are not in the VPC"
        }
      ]
    }
  }
}
  • Related