Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reference the Default VPC in CloudFormation?

I have a stack which needs resources which need to be deployed in a certain VPC. I want to use the default VPC but I don't want to parameterize this. Is there a way to automatically obtain the default VPC value? (Like for example Fn::GetAZs: region for AZ's in a region).

like image 291
DenCowboy Avatar asked Dec 02 '19 20:12

DenCowboy


People also ask

What is the default VPC?

A default VPC is a logically isolated virtual network in the AWS cloud that is automatically created for your AWS account the first time you provision Amazon EC2 resources. When you launch an instance without specifying a subnet-ID, your instance will be launched in your default VPC.

What is a default VPC And why would you use it?

Default VPC is a Virtual network which is automatically created for customer AWS account the very 1st time EC2 resources are provisioned. On the other hand, a nondefault (also called Customer VPC) is not automatically created when EC2 resources are provisioned and customer needs to create own VPC.

Can VPC be created using a CloudFormation template?

AWS CloudFormation allows us to implement "Infrastructure as Code" in an AWS environment. A sophisticated Virtual Private Cloud (VPC) is easy to create and update in an automated way with CloudFormation. We can re-use CloudFormation templates to build various stacks of resources for various purposes.


1 Answers

The ID of the default VPC can be obtained using the following AWS CLI command:

$ aws ec2 describe-vpcs \
    --filters Name=isDefault,Values=true \
    --query 'Vpcs[*].VpcId' \
    --output text

vpc-a1b2c3d4

The above command will output something like: vpc-a1b2c3d4

You can assign this output to a variable and then pass it to your CF template like this:

$ default_vpc_id=$(aws ec2 describe-vpcs \
    --filters Name=isDefault,Values=true \
    --query 'Vpcs[*].VpcId' \
    --output text)

$ echo ${default_vpc_id}

vpc-a1b2c3d4

like image 113
Marcin Avatar answered Oct 02 '22 03:10

Marcin