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

3

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?

Acupuncture answered 3/11, 2014 at 5:24 Comment(0)
E
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.

Eades answered 14/1, 2015 at 1:37 Comment(5)
basically right - but the "Type" can't be simply string for a VPC, it must be "AWS::EC2::VPC::Id", like here documented: docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…Ummersen
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.Eades
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 ;)Ummersen
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...Correction
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.Eades
P
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."
    },
Prissie answered 21/12, 2016 at 6:25 Comment(0)
P
-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

Proximity answered 3/11, 2014 at 9:56 Comment(4)
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" }.Acupuncture
If you already have a VPC it will have an Id simply put that in the ref. ie "VpcId" : {"Ref": "vpc-123456"}Proximity
Doesn't work: Template validation error: Template format error: Unresolved resource dependencies ...Raffinate
@benniej 's suggestion also does not work for me. Will have to try the param, and mapping optionLedoux

© 2022 - 2024 — McMap. All rights reserved.