英文:
Lookup IGW after specifying VPC parameter in CloudFormation template
问题
在下面的代码段中,我是否可以使用基于VpcId参数的函数(或其他方式)进行IGW的查找,而不需要将其作为参数传递?
Parameters:
VpcId:
Type: AWS::EC2::VPC::Id
Description: 输入现有着陆区VPC的ID。
InternetGateway:
Type: String
Description: 输入现有着陆区IGW。
Resources:
DefaultRoute:
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
RouteTableId: !Ref RouteTable
谢谢。
我还没有尝试过任何方法,因为我找不到任何使我认为这会起作用的东西,只是不确定是否有遗漏的内容。
英文:
In the below snippet, can I do a lookup for the IGW using a function (or something) based on the VpcId parameter instead of needing it as a parameter?
Parameters:
VpcId:
Type: AWS::EC2::VPC::Id
Description: Enter the Existing Landing Zone VPC ID.
InternetGateway:
Type: String
Description: Enter the Existing Landing Zone IGW.
Resources:
DefaultRoute:
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
RouteTableId: !Ref RouteTable
Thanks.
I have not tried anything because I can't find anything that makes me thinks this would work, just not sure if I am missing something.
答案1
得分: 1
不,这在CloudFormation中是不可能的。
唯一的方法是使用CloudFormation宏来调用执行自定义代码的Lambda函数,以这种方式动态查找资源。例如,在Python中,您可以使用boto3的describe_internet_gateways API,它返回一个互联网网关列表(您可以使用VPC ID 进行过滤)。
更多关于CloudFormation宏的信息:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html
宏示例:https://stackoverflow.com/a/70475459/3390419
英文:
No, that is not possible in CloudFormation.
The only way you can dynamically look up resources in such a way is if you leverage a CloudFormation macro to invoke a lambda function which executes custom code. For example, in Python, you could use the boto3 describe_internet_gateways API which returns a list of internet gateways (you could use a filter for the VPC ID).
More on CloudFormation macros:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html
Macro example: https://stackoverflow.com/a/70475459/3390419
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论