Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 229
kvnam Avatar asked Nov 19 '25 07:11

kvnam


2 Answers

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.

like image 120
Tanato Avatar answered Nov 21 '25 21:11

Tanato


You can use streamed response for larger payloads. The default limit for streams is 20MB that can be increased. source

I had an 8MB file that didn't fit to the normal limit. I changed it to stream and it worked fine.

Streaming requires some implementation changes on the Lambda side, but my curl/node client requests work without any changes.

I used Lambda URL and astuyve/lambda-stream library in my project.

like image 38
tonisives Avatar answered Nov 21 '25 22:11

tonisives



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!