【问题标题】:How to get arn of AWS Api gateway when deploying with cloudformation使用 cloudformation 部署时如何获取 AWS Api 网关的 arn
【发布时间】:2022-01-17 03:47:29
【问题描述】:

我正在尝试使用 cloudformation 部署功能和 aws api 网关。在LambdaPermission 资源中有一个属性是SourceArn,它需要调用该函数的资源的 ARN,在这种情况下它将是 api 网关。现在ApiGateway 资源不提供arn 的输出值。所以我的问题是我们如何访问它?

这是我需要将值放入 sourcearn 的 Lambda Permission 资源。

LambdaPermission:
    Type: "AWS::Lambda::Permission"
    Properties:
        Action: "lambda:InvokeFunction"
        FunctionName: !GetAtt LambdaFunction.Arn
        Principal: "apigateway.amazonaws.com"
        SourceArn: "How to get this value"

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-cloudformation aws-api-gateway


    【解决方案1】:

    格式:

    SourceArn:
      Fn::Join:
      - ''
      - - 'arn:'
        - !Ref AWS::Partition
        - ":execute-api:"
        - !Ref AWS::Region
        - ":"
        - !Ref AWS::AccountId
        - ":"
        - !Ref "Logical ID of resource of type AWS::ApiGateway::RestApi"
        - "/"
        - !Ref "Logical ID of resource of type AWS::ApiGateway::Stage"
        - "/GET or POST or other HTTP Methods/your/resource/path/here"
    

    一个例子:

    SourceArn:
      Fn::Join:
      - ''
      - - 'arn:'
        - !Ref AWS::Partition
        - ":execute-api:"
        - !Ref AWS::Region
        - ":"
        - !Ref AWS::AccountId
        - ":"
        - !Ref ApiGatewayRestApiResource
        - "/"
        - !Ref ApiGatewayStageResource
        - "/GET/example"
    

    【讨论】:

    • 感谢@Kaustubh,这是 95% 正确的,你只需要使用!在参考之前。这是我尝试过的,它奏效了。 SourceArn: !Join [ ":", ["arn:aws:execute-api", !Ref AWS::Region, !Ref AWS::AccountId, !Ref ApiGatewayRestApi, "/*/POST/" ] ]
    • 在 YAML 中,Ref: logicalName!Ref logicalName 都应该可以工作。但是我编辑了我的答案以使用!Ref,因为它对你来说是正确的。
    • 也可以使用!Sub函数来获得更简洁的语法
    猜你喜欢
    • 2022-08-15
    • 2020-03-13
    • 2016-12-13
    • 2022-08-11
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 2023-03-06
    • 2021-04-24
    相关资源
    最近更新 更多