Amazon web services 如何在CloudFormation模板中同时使用Sub和GetAtt函数?

Amazon web services 如何在CloudFormation模板中同时使用Sub和GetAtt函数?,amazon-web-services,yaml,amazon-cloudformation,Amazon Web Services,Yaml,Amazon Cloudformation,我创建了CloudFormation yaml模板,我需要使用!GetAtt“TestLambda.Arn”作为的一部分!“AWS::ApiGateway::Method”集成Uri中的子函数 ... Type: "AWS::ApiGateway::Method" Properties: RestApiId: Ref: "RestApi" ResourceId: Ref: "TestResource" HttpMethod: "GET"

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

...
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"

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

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

Uri: !Sub arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/${TestLambda.Arn}/invocations
下文对此进行了解释:

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


如果使用
,我们可以对嵌套的内在函数使用
Fn::
首先是简短形式。所以

!Sub "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/$(Fn::GetAtt:[TestLambda, Arn])/invocations"

AWS CloudFormation提供了几个内置函数,可帮助您管理堆栈。在模板中使用内部函数为运行时才可用的属性指定值

Fn::GetAtt
内在函数从模板中的资源返回属性值

声明

  • JSON
    • {“Fn::GetAtt”:[“logicalNameOfResource”,“attributeName”]}
  • YAML
    • 完整函数名的语法:
    • Fn::GetAtt:[logicalNameOfResource,attributeName]
  • 缩写形式的语法:
    • !GetAtt logicalNameOfResource.attributeName

注意:不应与双冒号混淆:
Fn::GetAtt
就像
Fn\u GetAtt
我知道这是一个老问题,但仍然是谷歌的第一个结果:

Parameters:
  path:
    Type: String
    Default: something/script.sh
Resources:
  Bucket:
    Type: AWS::S3::Bucket
Outputs:
  ScriptUrl:
    Description: Script Url
    Value:
      Fn::Sub:
        - ${url}${the_path}
        - {url: !GetAtt Bucket.WebsiteURL, the_path: !Ref path}

事实上,这并没有回答任何寻找JSON等价物的人的问题,然后看看上面答案中链接的文档,并使用映射:
{“Fn::Sub”:[“www.${Domain},{“Domain”:{“Ref”:“RootDomainName”}]}
我不得不说,这是解决方案不仅存在的神奇时刻之一,但这比预期的容易。呜!