user3343396
user3343396

Reputation: 765

AWS Lambda next.js - Failed to post 2MB file to api route

I deployed my next.js app to aws usingserverless-next.js package. When I'm trying to post a big file (~1MB) to the api route, I'm getting a 503 response code. The full response content:

<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>503 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: X
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>

The CloudWatch log shows:

{
    "errorType": "Error",
    "errorMessage": "Cannot call write after a stream was destroyed",
    "code": "ERR_STREAM_DESTROYED",
    "stack": [
        "Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed",
        "    at WriteStream._write (internal/fs/streams.js:379:33)",
        "    at WriteStream.<anonymous> (internal/fs/streams.js:375:12)",
        "    at Object.onceWrapper (events.js:422:26)",
        "    at WriteStream.emit (events.js:327:22)",
        "    at WriteStream.EventEmitter.emit (domain.js:483:12)",
        "    at internal/fs/streams.js:366:10",
        "    at FSReqCallback.oncomplete (fs.js:156:23)"
    ]
}

I'm using formidable to parse the body of the request. It working with smaller file sizes.

What can I do? 1MB does not count as big file size.

In development everything works even with 10MB files, but not in AWS lambda@edge

Upvotes: 1

Views: 723

Answers (1)

Gabouchet
Gabouchet

Reputation: 191

It's written here: https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html

You can't have a request + response larger than 256 KB if you upload in asynchronous mode.

Because serverless is only here to make your routes in lambda functions. If you want to upload this file, you can use S3 bucket with pre-signed url. With that you doesn't need to upload the file to the lambda function.

Upvotes: 1

Related Questions