代码之家  ›  专栏  ›  技术社区  ›  Hleb

如何在cloudformation模板中同时使用sub和getatt函数?

  •  10
  • Hleb  · 技术社区  · 8 年前

    我创建了cloudformation yaml模板,需要使用 !GetAtt "TestLambda.Arn" 作为一部分 !Sub “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"
    

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

    4 回复  |  直到 8 年前
        1
  •  24
  •   Yves M.    7 年前

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

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

    这在 docs :

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

        2
  •  5
  •   Premraj    7 年前

    aws cloudformation提供了几个内置函数,帮助您管理堆栈。使用模板中的内部函数将值分配给运行时才可用的属性。

    Source from AWS

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

    宣言

    • 杰森
      • { "Fn::GetAtt" : [ "logicalNameOfResource", "attributeName" ] }
    • 钇铝石榴石
      • 完整函数名的语法:
      • Fn::GetAtt: [ logicalNameOfResource, attributeName ]
    • 缩写形式的语法:
      • !GetAtt logicalNameOfResource.attributeName

    注: 不应与双冒号混淆: FN::盖特 就像 Fn_GetAtt

        3
  •  3
  •   Cristian Marin    7 年前

    我知道这是一个老问题,但仍然是谷歌的第一个结果:

    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}
    
        4
  •  0
  •   Hleb    8 年前

    我们可以使用 Fn:: 如果使用 ! 先是简短的形式。所以

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