首页 > 解决方案 > CloudFormation模板中如何同时使用Sub和GetAtt函数?

问题描述

我创建了 CloudFormation yaml 模板,我需要在“AWS::ApiGateway::Method”集成 Uri 中!GetAtt "TestLambda.Arn"用作函数的一部分:!Sub

Type: "AWS::ApiGateway::Method"
  Properties:
    RestApiId:
      Ref: "RestApi"
    ResourceId:
      Ref: "TestResource"
    HttpMethod: "GET"
    AuthorizationType: "NONE"
    Integration:
      Type: "AWS_PROXY"
      IntegrationHttpMethod: "POST"
      Uri: !Sub "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/[[place where I want to use !GetAtt "TestLambda.Arn"]]/invocations"

结果我想得到一个类似的值:

"arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/my-endpoint-lambda/invocations"

如何将这些功能一起使用并获得所需的结果?

标签: amazon-web-servicesyamlamazon-cloudformation

解决方案


您不需要在!GetAtt这里使用,!Sub如果您将它们放在占位符中,它将自动为您解包值:

Uri: !Sub arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${TestLambda.Arn}/invocations

这在文档中进行了解释:

如果您指定模板参数名称或资源逻辑 ID,例如${InstanceTypeParameter},AWS CloudFormation 将返回与您使用 Ref 内在函数相同的值。如果您指定资源属性,例如,AWS CloudFormation 将返回与您使用内部函数${MyInstance.PublicIp}相同的值。Fn::GetAtt


推荐阅读