10

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?

Roobie
  • 1,346
  • 4
  • 14
  • 24

3 Answers3

9

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.

bsvingen
  • 2,699
  • 14
  • 18
  • basically right - but the "Type" can't be simply string for a VPC, it must be "AWS::EC2::VPC::Id", like here documented: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html – jebbie Jun 07 '17 at 22:02
  • String should work fine (or at least did at the time when I wrote this code), but using a more specific type is better. Thanks. – bsvingen Jun 08 '17 at 22:15
  • at least in the latest awscli i'm using right now it will fail with a validation error on a template not using this specific type ;) – jebbie Jun 12 '17 at 17:25
  • So if you do this and then delete this stack will it delete that VPC? That would be bad since it wasn't created by this stack... – Dasmowenator Jun 20 '17 at 00:25
  • If you delete the stack creating the VPC, the VPC will be deleted. If you delete a stack just referencing the VPC it will not. – bsvingen Jun 21 '17 at 12:05
5

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."
    },
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
whossa
  • 191
  • 1
  • 7
-2

Reference it by name ie. "VpcId" : { "Ref" : "myVPC" }, In something like:

    {
   "Type" : "AWS::EC2::Subnet",
   "Properties" : {
      "AvailabilityZone" : String,
      "CidrBlock" : String,
      "Tags" : [ Resource Tag, ... ],
      "VpcId" : { "Ref" : String }
      }
    }  

Documentation here: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html

bennie j
  • 729
  • 4
  • 8
  • It looks like I need to have a Parameter or a Mapping and then hard-code the VPC Id and then reference it in the subnet script unless the VPC and Subnet all are created in the same script for me to be able to reference the VPC Id using "VpcId" : { "Ref" : "myVPC" }. – Roobie Nov 04 '14 at 22:12
  • 1
    If you already have a VPC it will have an Id simply put that in the ref. ie "VpcId" : {"Ref": "vpc-123456"} – bennie j Nov 06 '14 at 11:02
  • 1
    Doesn't work: Template validation error: Template format error: Unresolved resource dependencies ... – Ashesh Sep 04 '15 at 17:40
  • @benniej 's suggestion also does not work for me. Will have to try the param, and mapping option – Sam Hammamy Mar 24 '16 at 19:34