Search code examples
amazon-web-servicesexpressaws-lambdagraphqlserverless

Unexpected LAMBDA_RUNTIME Failed to post handler success response. Http response code: 413 error


So I have a serverless express application running in a Lambda. One request (response size around 800KB) keeps returning a LAMBDA_RUNTIME Failed to post handler success response. Http response code: 413. error. I thought it could be due to some internal logic timing out, and added logs, and all the fetch and processing takes maximum 6 seconds, but the lamdba still returns this error.

These are the response headers

x-amz-cf-pop: YTO50-C3
x-amzn-errortype: InternalServerErrorException
x-amzn-requestid: f291230-342-4324-324-cb7df188944c
x-cache: Error from cloudfront

The response size is definitely not too big, I am returning a response with right data, no errors are being thrown in the logs. Any idea why this could be happening? Also any suggestions on how I can debug this issue? Everything of course works in local, but is there a way for me debug the actual lambda? The logs I added indicate that the full process completes, yet somehow there is an error being returned.

Updated my serverless.yml config

service: my-service
variablesResolutionMode: 20210326
useDotenv: true

custom:
  serverless-offline:
    useChildProcesses: true
  webpack:
    webpackConfig: ./webpack.config.js
    packager: "yarn"
    includeModules: true
  prune:
    automatic: true
    includeLayers: true
    number: 3
  customDomain:
    domainName: "abc.com"
    basePath: "val"
    stage: ${someval}
    createRoute53Record: true

plugins:
  - serverless-domain-manager
  - serverless-webpack
  - serverless-prune-plugin
  - serverless-webpack-prisma
  - serverless-offline

provider:
  lambdaHashingVersion: "20201221"
  name: aws
  runtime: nodejs14.x
  region: us-east-1
  timeout: 30
  apiGateway:
    minimumCompressionSize: 1024 
  iamRoleStatements:
    - Effect: Allow
      Action: ssm:Get*
      Resource:
        - "abc/${opt:stage}/backend/*"
        - "abc/${opt:stage}/services/*"
    - Effect: Allow
      Action: kms:Decrypt
      Resource: "*"
    - Effect: "Allow"
      Action: s3:PutObject
      Resource: "abc/*"
    - Effect: "Allow"
      Action:
        - sns:Publish
      Resource: "*"

  environment:
    - myvars: 'abc'

functions:
  graphql:
    handler: src/index.graphqlHandler
    events:
      - http:
          path: /graphql
          method: options
      - http:
          path: /graphql
          method: get
      - http:
          path: /graphql
          method: post

Solution

  • This error is usually due to hitting the Lambda limit for response payload.

    Currently, AWS Lambdas have a hard limit for invocation payloads of:

    • 6MB for Synchronous requests and
    • 256KB for Asynchronous requests,

    According to the documentation: enter image description here https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html

    Keep in mind that this is a hard limit and cannot be increased.

    Currently (2022), if you need more than this limit, you'll have two options that I can think of

    1. use another stack, for example ECS
    2. you can compress (gzip, brotli, etc) your response inside the lambda and uncompress in the consumer. If you're using API Gateway to expose your lambda, you'll have to set the appropriate header.

    Personally, I've experienced similar issues even before reaching this threshold, so it can be hard to calculate exactly the size of your payload.