简体   繁体   中英

How can we get a VPC's default route table ID using Get::Attr in AWS CloudFormation?

I have created a VPC using CloudFormation template, along with the VPC a default route table and NACL are created automatically.

Now I want to add some routes to the default route table that is created with VPC in the same CloudFormation template. Is there any way to get the VPC's default route table ID in the same CloudFormation template?

One possible way could be using Get::Attr, but I can't find any attribute like defaultroutetable with Get::Attr.

The default Route table for VPC is created along with the VPC. If you create a VPC using cloud-formation then it automatically creates a route table and attach it with the same VPC. Get::Attr function is used for fetching the attributes of a specific AWS resource, but it depends on that AWS resource, if it returns the attribute which you are looking for. In this case, you might be looking for default Route table Id but Get::Attr function does not return RouteTableID. (It may be available in future but not as of now.)

I would recommend to create route tables for the each subnet and in case of different routes. If you want to do it using scripts then you should do it by creating a shell script using AWS CLI commands and find the ID of default route table of VPC, then add routes to it.

Find the route table ID: aws ec2 describe-route-tables --filters "Name=vpc-id,Values=$vpcs" --query "RouteTables[].RouteTableId[]"

Create routes for this route table: (Example) aws ec2 create-route --route-table-id --destination-cidr-block 0.0.0.0/0 --gateway-id igw-c0a643a9

Well, the short answer is You can't directly get the main Route Table ID from a VPC only by AWS CloudFormation, because CloudFormation does not provide a reference between them.

However, if you really want to do it with CloudFormation, you can still do it with the help of calling an AWS Lambda function within the CloudFormation template. The Lambda function can run commands to attach the route rules to the main Route Table. Here is an answer from AWS in 2019, https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-route-table-vpc/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM