Search code examples
amazon-web-servicesaws-lambdaaws-api-gatewaydocumentation

Is there any official documentation of AWS Gateway events sent to Lambda?


I've been searching for some sort of official documentation for the data types sent to AWS Lambda from integrations such as, AWS API Gateway. I've been able to find some "Examples" in the API Gateway documentation like here and here. It is also relatively easy to create a Lambda that just echoes the input event as output and check the output. For example (using a REST type API with LAMBDA_PROXY integration) you get something like:

{
    "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
}

But this doesn't tell me what fields I can always, or just sometimes, expect to be in the payload or the types of the field.

The closest I have to a bit of typing of the fields is the aws-lambda TypeScript typings from the Definitely Typed project.

Is there anything more detailed, or perhaps more general, and official source I can go to for the structure and type of Lambda event payloads for API Gateway and other AWS service integrations?


Solution

  • This is the closest I've come to an answer, so far. If anyone has more detailed (or general information) I'd gladly take it. In the Lambda documentation you can read that (my emphasis):

    The runtime passes three arguments to the handler method. The first argument is the event object, which contains information from the invoker. The invoker passes this information as a JSON-formatted string when it calls Invoke, and the runtime converts it to an object. When an AWS service invokes your function, the event structure varies by service.

    Under "Working with other services" in the lambda documentation we only find the examples presented in the question. Instead we can look in the documentation for the service in question, e.g. API Gateway. For API Gateway, under "Working with [HTTP/REST] APIs", you will find descriptions of the event fields for REST and HTTP type APIs.

    With that said, the documentation provided still only refer to the described payload structure as "examples". It only provides you with the names of the fields and some basic structure. Data types would have to be inferred from the examples, see the snippets below.

    The story seems to be similar for other services you might want to integrate with Lambda like:

    All that is to be found is examples of payloads, some on the services' own developer's guide other under Lambda's developer's guide.

    HTTP v2.0

    The following is an example of the payload format for version 2.0 using HTTP type API. For version 1 see the HTTP type integration docs.

    {
          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'}
        }
    

    REST v3.0

    The following is an example of the payload format for version 3.0 using REST type API. For version 2.0 see the REST type integration docs.

    {
       "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"
                }
             }
          }
       ]
    }