是否有发送到 Lambda 的 AWS Gateway 事件的官方文档?

Chr*_*son 7 documentation amazon-web-services aws-lambda aws-api-gateway

我一直在寻找某种官方文档,了解从 AWS API Gateway 等集成发送到 AWS Lambda 的数据类型。我已经能够在 API Gateway 文档中找到一些“示例”,例如此处此处。创建一个仅将输入事件回显为输出并检查输出的 Lambda 也相对容易。例如(使用带有LAMBDA_PROXY集成的 REST 类型 API),您会得到类似以下内容的信息:

{
    "resource": "/another/{parameter}",
    "path": "/another/some-parameter",
    "httpMethod": "GET",
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate, br",
        ...
    },
    "multiValueHeaders": {
        "Accept": [
            "*/*"
        ],
        "Accept-Encoding": [
            "gzip, deflate, br"
        ],
        ...
    },
    "queryStringParameters": null,
    "multiValueQueryStringParameters": null,
    "pathParameters": {
        "parameter": "some-parameter"
    },
    "stageVariables": null,
    "requestContext": {
        "resourceId": "some-id",
        "resourcePath": "/another/{parameter}",
        "httpMethod": "GET",
        ...
    },
    "body": null,
    "isBase64Encoded": false
}
Run Code Online (Sandbox Code Playgroud)

但这并没有告诉我我总是可以或只是有时期望在有效负载或字段类型中包含哪些字段。

我最接近的字段类型是来自 Definely Typed 项目的aws-lambdaTypeScript 类型。

对于 API Gateway 和其他 AWS 服务集成的 Lambda 事件负载的结构和类型,是否有任何更详细、或更一般的官方来源可供我访问?

Chr*_*son 4

到目前为止,这是我最接近的答案。如果有人有更详细(或一般信息),我很乐意接受。在Lambda 文档中,您可以读到(我的重点):

运行时将三个参数传递给处理程序方法。第一个参数是事件对象,其中包含来自调用者的信息。调用程序在调用 Invoke 时将此信息作为 JSON 格式的字符串传递,运行时将其转换为对象。当 AWS 服务调用您的函数时,事件结构因服务而异。

在 lambda 文档中的“使用其他服务”下,我们只找到问题中提供的示例。相反,我们可以查看相关服务的文档,例如API Gateway。对于 API 网关,在“使用 [HTTP/REST] API”下,您将找到RESTHTTP类型 API 的事件字段的说明。

尽管如此,提供的文档仍然仅将描述的有效负载结构称为“示例”。它只为您提供字段的名称和一些基本结构。数据类型必须从示例中推断出来,请参阅下面的代码片段。

对于您可能想要与 Lambda 集成的其他服务,这个故事似乎类似:

可以找到的只是有效负载的示例,其中一些位于服务自己的开发人员指南中,另一些位于 Lambda 的开发人员指南中。

HTTP v2.0

以下是使用 HTTP 类型 API 的 2.0 版本的有效负载格式示例。对于版本 1,请参阅HTTP 类型集成文档

{
      version: '2.0',
      routeKey: '$default',
      rawPath: '/my/path',
      rawQueryString: 'parameter1=value1&parameter1=value2&parameter2=value',
      cookies: [ 'cookie1', 'cookie2' ],
      headers: {
        'Header1': 'value1',
        'Header2': 'value2'
      },
      queryStringParameters: { parameter1: 'value1,value2', parameter2: 'value' },
      requestContext: {
        accountId: '123456789012',
        apiId: 'api-id',
        authorizer: { jwt: {
            claims: {'claim1': 'value1', 'claim2': 'value2'},
            scopes: ['scope1', 'scope2']
            }
        },
        domainName: 'id.execute-api.us-east-1.amazonaws.com',
        domainPrefix: 'id',
        http: {
          method: 'POST',
          path: '/my/path',
          protocol: 'HTTP/1.1',
          sourceIp: 'IP',
          userAgent: 'agent'
        },
        requestId: 'id',
        routeKey: '$default',
        stage: '$default',
        time: '12/Mar/2020:19:03:58 +0000',
        timeEpoch: 1583348638390
      },
      body: 'Hello from Lambda',
      pathParameters: {'parameter1': 'value1'},
      isBase64Encoded: false,
      stageVariables: {'stageVariable1': 'value1', 'stageVariable2': 'value2'}
    }
Run Code Online (Sandbox Code Playgroud)

休息v3.0

以下是使用 REST 类型 API 的 3.0 版本的有效负载格式示例。对于版本 2.0,请参阅REST 类型集成文档

{
   "openapi": "3.0.0",
   "info": {
      "version": "2016-09-12T17:50:37Z",
      "title": "ProxyIntegrationWithLambda"
   },
   "paths": {
      "/{proxy+}": {
         "x-amazon-apigateway-any-method": {
            "parameters": [
               {
                  "name": "proxy",
                  "in": "path",
                  "required": true,
                  "schema": {
                     "type": "string"
                  }
               }
            ],
            "responses": {},
            "x-amazon-apigateway-integration": {
               "responses": {
                  "default": {
                     "statusCode": "200"
                  }
               },
               "uri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:SimpleLambda4ProxyResource/invocations",
               "passthroughBehavior": "when_no_match",
               "httpMethod": "POST",
               "cacheNamespace": "roq9wj",
               "cacheKeyParameters": [
                  "method.request.path.proxy"
               ],
               "type": "aws_proxy"
            }
         }
      }
   },
   "servers": [
      {
         "url": "https://gy415nuibc.execute-api.us-east-1.amazonaws.com/{basePath}",
         "variables": {
            "basePath": {
              "default": "/testStage"
            }
         }
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)