在 CloudFormation 模板中指定 VPC 参数后查找 IGW。

huangapple go评论64阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年1月9日 03:36:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定