简体   繁体   English

我们如何使用 AWS CloudFormation 中的 Get::Attr 获取 VPC 的默认路由表 ID?

[英]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.我已经使用 CloudFormation 模板创建了一个 VPC,同时自动创建了一个默认路由表和 NACL。

Now I want to add some routes to the default route table that is created with VPC in the same CloudFormation template.现在,我想向在同一 CloudFormation 模板中使用 VPC 创建的默认路由表添加一些路由。 Is there any way to get the VPC's default route table ID in the same CloudFormation template?有什么办法可以在同一个 CloudFormation 模板中获取 VPC 的默认路由表 ID?

One possible way could be using Get::Attr, but I can't find any attribute like defaultroutetable with Get::Attr.一种可能的方法是使用 Get::Attr,但我找不到任何属性,例如带有 Get::Attr 的 defaultroutetable。

The default Route table for VPC is created along with the VPC. VPC 的默认路由表与 VPC 一起创建。 If you create a VPC using cloud-formation then it automatically creates a route table and attach it with the same VPC.如果您使用 cloud-formation 创建 VPC,那么它会自动创建一个路由表并将其附加到相同的 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. Get::Attr 函数用于获取特定 AWS 资源的属性,但它取决于该 AWS 资源,如果它返回您要查找的属性。 In this case, you might be looking for default Route table Id but Get::Attr function does not return RouteTableID.在这种情况下,您可能正在寻找默认路由表 ID,但 Get::Attr 函数不返回 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.如果你想使用脚本来完成,那么你应该通过使用 AWS CLI 命令创建一个 shell 脚本来完成,并找到 VPC 的默认路由表的 ID,然后向它添加路由。

Find the route table ID: aws ec2 describe-route-tables --filters "Name=vpc-id,Values=$vpcs" --query "RouteTables[].RouteTableId[]"查找路由表 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为此路由表创建路由:(示例)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.嗯,简短的回答是您不能仅通过 AWS CloudFormation 从 VPC 直接获取主路由表 ID,因为 CloudFormation 不提供它们之间的参考。

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.但是,如果您真的想使用 CloudFormation 来实现,您仍然可以借助在 CloudFormation 模板中调用 AWS Lambda 函数来实现。 The Lambda function can run commands to attach the route rules to the main Route Table. Lambda 函数可以运行命令将路由规则附加到主路由表。 Here is an answer from AWS in 2019, https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-route-table-vpc/ .这是 AWS 在 2019 年的回答, https://aws.amazon.com/premiumsupport/knowledge-center/cloudformation-route-table-vpc/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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