Is it possible to get a VPC Id using AWS Cloud Formation?

LeandroCR picture LeandroCR · Nov 6, 2014 · Viewed 11.2k times · Source

Is it possible to obtain a VPC Id using Cloud Formation JSON?

Something like:

{ "Fn::GetAtt" : [ "MyVPCName", "VPCId" ] }

Answer

Bradley T. Hughes picture Bradley T. Hughes · Nov 7, 2014

You can use the "Ref" built-in function to get the VPC's resource ID iff you created the VPC in the same template, see the Return Values section of http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc.html

If you are wanting to reference an existing VPC (like your default VPC), then the above won't work. You could pass the VPC's Id as a parameter to the template instead and use the "Ref" built-in to use it where you need it.