AWS ApiGateway集成S3、SQS、SNS、DynamoDB等服务,如何构建AWS CloudFormation集成URI?

How to construct AWS CloudFormation integration URI for AWS ApiGateway integration with S3, SQS, SNS, DynamoDB and other services?

是否有一些地方有关于如何通过 ApiGateway 集成进行一系列操作的示例?查看如何将对象上传到 S3、将项目推送到 SQS 和 SNS 队列、进行 DynamoDB 调用和许多其他事情,试图找到有关如何构建这些路径的文档。

我正在使用 CloudFormation 模板,它使用集成 URI 来设置此 AWS ApiGateway 与 AWS 服务的集成。

找不到说明如何为各种服务创建这些 URI 路径的文档。

使用另一个 AWS 服务操作设置集成请求时,集成请求 URI 也是一个 ARN。

例如,对于与Amazon S3的GetBucket操作的集成,集成请求URI是如下格式的ARN:

arn:aws:api网关:api-区域:s3:路径

查看更多:https://docs.aws.amazon.com/apigateway/latest/developerguide/integration-request-basic-setup.html

发电机组: 比 S3 复杂一点: https://aws.amazon.com/blogs/compute/using-amazon-api-gateway-as-a-proxy-for-dynamodb/

社交网络: https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-proxy-integrate-service/

对于 SQS,我找到了 cloudformation 设置:

PostMethod:
    Type: "AWS::ApiGateway::Method"
    Properties:
      AuthorizationType: "NONE"
      ApiKeyRequired: "true"
      HttpMethod: "POST"
      ResourceId: !Ref "SomeResource"
      RestApiId: !Ref "RestApi"
      MethodResponses:
      - StatusCode: 200
      Integration:
        Credentials: !GetAtt "RestApiRole.Arn"
        IntegrationHttpMethod: "POST"
        IntegrationResponses:
        - StatusCode: 200
        Type: "AWS"
        Uri: !Sub "arn:aws:apigateway:${AWS::Region}:sqs:action/SendMessage"
        RequestParameters:
          integration.request.querystring.QueueUrl: !Sub "'${SomeQueue}'"
          integration.request.querystring.MessageBody: "method.request.body"

这里是 RestApiRole 的代码:

RestApiRole:
    Type: "AWS::IAM::Role"
    Properties:
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
        - Action:
          - "sts:AssumeRole"
          Principal:
            Service:
            - "apigateway.amazonaws.com"
          Effect: "Allow"
      Policies:
      - PolicyName: "InvokeLambda"
        PolicyDocument:
          Version: "2012-10-17"
          Statement:
          - Action:
            - "lambda:InvokeFunction"
            Resource: !GetAtt "LambdaFunction.Arn"
            Effect: "Allow"

来自 Uri 属性 documentation:

If you specify AWS for the Type property, specify an AWS service that follows this form: arn:aws:apigateway:region:subdomain.service|service:path|action/service_api. For example, a Lambda function URI follows this form: arn:aws:apigateway:region:lambda:path/path. The path is usually in the form /2015-03-31/functions/LambdaFunctionARN/invocations. For more information, see the uri property of the Integration resource in the Amazon API Gateway REST API Reference.

来自另一个 AWS 的更多描述和示例 documentation:

根据这些文档示例和描述,似乎有 2 种类型的 APIs - 基于操作和基于路径。

使用基于操作的 API

我认为大多数人(如果不是所有人)都支持这一点。虽然这些操作在 IAM 设置和所有 API 文档中可用,而所有 AWS 服务都是 web 服务,也就是它们具有 API 接口并且这些接口使用操作。如果某些服务有误,请纠正我,但我认为遵循此结构应该可以对与 API 网关服务集成的任何服务进行任何调用。

有时需要使用路径API

正在尝试使用 PutObject 将文件上传到 S3,但出现错误:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>MethodNotAllowed</Code>
    <Message>The specified method is not allowed against this resource.</Message>
    <Method>PUT</Method>
    <ResourceType>SERVICE</ResourceType>
    <RequestId>....</RequestId>
    <HostId>....=</HostId>
</Error>

替换为路径 API 格式并成功。所以这里的学习是我将继续尝试首先使用操作 APIs,如果不能针对该特定操作 - 切换到路径 API,而我觉得操作 API 更具声明性。

示例结构操作 API:

arn:aws:apigateway:us-east-1:SERVICE_NAME:action/ACTION_NAME&Var1=Value1&Var2=Value2

对 S3 服务的示例调用。操作名称 - GetObject。此 API 操作的文档说有 2 个必需属性 - Bucket(存储桶名称)和 Key。如此完整的示例 URI:

arn:aws:apigateway:us-east-1:s3:action/GetObject&Bucket=myDemoBucket1&Key=some/path/to/file

路径相同 API:

arn:aws:apigateway:us-east-1:s3:path/myDemoBucket1/some/path/to/file

我找到了一种获取样本的方法。使用控制台 UI,创建端点,部署到某个阶段并转到阶段,select 导出选项卡,并以 Yaml 格式导出为 Swagger + API 网关扩展。当我将 Yaml 与 cloudformation 一起使用时。 Yaml 里面有你需要的一切。如果没有“舞台”,请转到“资源”并从下拉菜单中 select 在对话框中部署和创建舞台。


以下是我为主要服务找到的一些不同样本:

调用 Lambda docs:

arn:aws:apigateway:api-region:lambda:path//2015-03-31/functions/arn:aws:lambda:lambda-region:account-id:function:lambda-function-name/invocations

路径部分似乎映射到 API docs 中的 API 操作:

DynamoDB blog post

需要通过APIAction文档+Api Action名称+IntegrationRequest模板使用HTTP方法来调用DynamoDB。

查询操作的示例 URI:

arn:aws:apigateway:us-east-1:dynamodb:action/Query

社交网络blog post

示例 URI:arn:aws:apigateway:region:sns:action/Publish 地区:arn:aws:apigateway:us-east-1:sns:action/Publish

需要通过URLQuery String参数传入TopicArn和Message等参数。有关于该主题的好帖子:

https://docs.aws.amazon.com/sns/latest/api/API_Publish.html#API_Publish_Examples