New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Streamed responses giving 502 Bad Gateway when payload exceeds threshold (25kb+) #1137
Comments
Middy v5 does support Nodejs14 runtime (Only 18/20 support with this version). streamifyResponse was added in nodejs18.x runtime (and I believe back ported to nodejs16.x). Can you try updating your runtime. |
Sorry, we are actually using node 16.x. Also, response streaming appears to work for smaller payloads, but errors on the larger size. This issue may not even necessarily be on Middy's end, as the actual Lambda does not error; perhaps something with AWS? |
You need to set a setting on your lambda to allow it to work |
What do you mean? We have Invoke mode set to RESPONSE_STREAM and can stream smaller chunks of text, but errors on this larger JSON. |
Yes, the invoke mode is what was referring too. Can you test on nodejs18.x? I wonder if the back post was built differently. Is there a full example you can share? |
Nodejs 18.x does not appear to work either. Also, it seems that the function provides the correct response when using the "Test" feature in the Lambda console, meaning the issue is most likely on AWS's Function URLs side. const middy = require('@middy/core');
const handler = async (event) => {
const response = {
statusCode: 200,
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(['*'.repeat(20000)])
};
return response;
};
module.exports.handler = middy(handler, {
streamifyResponse: true
}); This code snippet errors for me. |
Sounds like you may need to open a support ticket. Note: another thing worth trying; use ESM? v5 deprecated CJS support (but is in v4) |
I believe I figured out the issue. const pipeline = require("util").promisify(require("stream").pipeline);
const { Readable } = require('stream');
exports.handler = awslambda.streamifyResponse(async (event, responseStream, _context) => {
// As an example, convert event to a readable stream.
const response = {
statusCode: 201,
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(['*'.repeat(20000)])
};
responseStream = awslambda.HttpResponseStream.from(responseStream, response);
const requestStream = Readable.from(Buffer.from(JSON.stringify(['*'.repeat(20000)])));
await pipeline(requestStream, responseStream);
}); This above snippet causes the function URL to give 502 Bad Request, but when Lines 58 to 62 in b2d3276
In this line in the middy source code, the whole response is passed back from middy without doing any processing, so body is inadvertently being sent as well. Not entirely sure why this is causing the function URL to error, but calling |
[![Mend Renovate logo banner](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [@middy/core](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2fcore/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2fcore/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2fcore/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2fcore/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2fcore/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@middy/error-logger](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2ferror-logger/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2ferror-logger/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2ferror-logger/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2ferror-logger/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2ferror-logger/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@middy/http-error-handler](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2fhttp-error-handler/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2fhttp-error-handler/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2fhttp-error-handler/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2fhttp-error-handler/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2fhttp-error-handler/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@middy/http-header-normalizer](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2fhttp-header-normalizer/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2fhttp-header-normalizer/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2fhttp-header-normalizer/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2fhttp-header-normalizer/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2fhttp-header-normalizer/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@middy/http-response-serializer](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2fhttp-response-serializer/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2fhttp-response-serializer/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2fhttp-response-serializer/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2fhttp-response-serializer/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2fhttp-response-serializer/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>middyjs/middy (@​middy/core)</summary> ### [`v5.0.3`](https://togithub.com/middyjs/middy/releases/tag/5.0.3) [Compare Source](https://togithub.com/middyjs/middy/compare/5.0.2...5.0.3) ##### What's Changed - update typescript and testing documentation by [@​anthony-nhs](https://togithub.com/anthony-nhs) in [middyjs/middy#1140 - Streamed responses giving 502 Bad Gateway when payload exceeds threshold (25kb+) [@​jjason685](https://togithub.com/jjason685) [middyjs/middy#1137 - http-response-serializer documentation incorrect [@​karmaniverous](https://togithub.com/karmaniverous) [middyjs/middy#1141 ##### New Contributors - [@​anthony-nhs](https://togithub.com/anthony-nhs) made their first contribution in [middyjs/middy#1140 **Full Changelog**: middyjs/middy@5.0.2...5.0.3 </details> --- ### Configuration 📅 **Schedule**: Branch creation - "before 5am on sunday" (UTC), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about these updates again. --- - [ ] If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/SvenKirschbaum/aws-utils).
[![Mend Renovate logo banner](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [@middy/core](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2fcore/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2fcore/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2fcore/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2fcore/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2fcore/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@middy/error-logger](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2ferror-logger/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2ferror-logger/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2ferror-logger/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2ferror-logger/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2ferror-logger/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@middy/http-content-negotiation](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2fhttp-content-negotiation/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2fhttp-content-negotiation/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2fhttp-content-negotiation/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2fhttp-content-negotiation/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2fhttp-content-negotiation/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@middy/http-error-handler](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2fhttp-error-handler/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2fhttp-error-handler/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2fhttp-error-handler/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2fhttp-error-handler/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2fhttp-error-handler/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@middy/http-header-normalizer](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2fhttp-header-normalizer/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2fhttp-header-normalizer/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2fhttp-header-normalizer/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2fhttp-header-normalizer/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2fhttp-header-normalizer/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@middy/http-json-body-parser](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2fhttp-json-body-parser/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2fhttp-json-body-parser/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2fhttp-json-body-parser/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2fhttp-json-body-parser/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2fhttp-json-body-parser/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@middy/http-response-serializer](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2fhttp-response-serializer/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2fhttp-response-serializer/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2fhttp-response-serializer/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2fhttp-response-serializer/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2fhttp-response-serializer/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@middy/validator](https://middy.js.org) ([source](https://togithub.com/middyjs/middy)) | [`5.0.2` -> `5.0.3`](https://renovatebot.com/diffs/npm/@middy%2fvalidator/5.0.2/5.0.3) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@middy%2fvalidator/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@middy%2fvalidator/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@middy%2fvalidator/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@middy%2fvalidator/5.0.2/5.0.3?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>middyjs/middy (@​middy/core)</summary> ### [`v5.0.3`](https://togithub.com/middyjs/middy/releases/tag/5.0.3) [Compare Source](https://togithub.com/middyjs/middy/compare/5.0.2...5.0.3) ##### What's Changed - update typescript and testing documentation by [@​anthony-nhs](https://togithub.com/anthony-nhs) in [middyjs/middy#1140 - Streamed responses giving 502 Bad Gateway when payload exceeds threshold (25kb+) [@​jjason685](https://togithub.com/jjason685) [middyjs/middy#1137 - http-response-serializer documentation incorrect [@​karmaniverous](https://togithub.com/karmaniverous) [middyjs/middy#1141 ##### New Contributors - [@​anthony-nhs](https://togithub.com/anthony-nhs) made their first contribution in [middyjs/middy#1140 **Full Changelog**: middyjs/middy@5.0.2...5.0.3 </details> --- ### Configuration 📅 **Schedule**: Branch creation - "before 5am on sunday" (UTC), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about these updates again. --- - [ ] If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/SvenKirschbaum/share.kirschbaum.cloud).
Describe the bug
I'm hosting an endpoint using streamifyResponse: true and AWS Lambda Function URLs. When the payload exceeds 20-25kb+ (I'm using a JSON string, but any data will error), the entire function URL 502s, yet there are no errors in the CloudWatch logs. However, when using streamifyResponse: false, the response is returned fine.
To Reproduce
How to reproduce the behaviour:
Expected behaviour
Returns response as normal
Environment (please complete the following information):
Using latest version of middy, Lambda environment is node 14
The text was updated successfully, but these errors were encountered: