Reference an existing AWS VPC Id in CloudFormation script when creating subnets

Roobie picture Roobie · Nov 3, 2014 · Viewed 14k times · Source

How do you reference the VPC Id of an existing VPC (which has been created before in a separate CloudFormation script) in CloudFormation script in order to create subnets in the VPC?

Answer

bsvingen picture bsvingen · Jan 14, 2015

In the template defining the VPC, include the VPC ID in the outputs section:

"Outputs" : {
    "VPC" : {
        "Value" : {"Ref":"VPC"},
        "Description" : "VPC ID"
    },
    ...
}

In the template for the stack using the VPC, define a parameter for the VPC ID:

"Parameters" : {
    "VPC" : {
        "Type" : "String",
    },
    ...
}

When creating this stack, call describe-stack on the VPC-defining stack to get the ID from outputs, and pass it as the VPC parameter to create-stack.