Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 869
Roobie Avatar asked Nov 03 '14 05:11

Roobie


2 Answers

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.

like image 71
bsvingen Avatar answered Oct 18 '22 22:10

bsvingen


Or get vpc id from input, such as

 "VpcId" : {
      "Type" : "AWS::EC2::VPC::Id",
      "Description" : "VpcId of your existing Virtual Private Cloud (VPC)",
      "ConstraintDescription" : "must be the VPC Id of an existing Virtual Private Cloud."
    },
like image 21
whossa Avatar answered Oct 18 '22 21:10

whossa