Skip to content
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

Closed
jjason685 opened this issue Nov 17, 2023 · 8 comments
Labels

Comments

@jjason685
Copy link

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:

  1. Create an AWS Lambda with function URL/streaming enabled
  2. Create a handler returning '*'.repeat(25000) and enable streamifyResponse on the middy
  3. Access the lambda function URL, and see that it returns 502 Bad Gateway with the response body "Internal Server Error"

Expected behaviour
Returns response as normal

Environment (please complete the following information):
Using latest version of middy, Lambda environment is node 14

@jjason685 jjason685 added the bug label Nov 17, 2023
@willfarrell
Copy link
Member

willfarrell commented Nov 17, 2023

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.

@jjason685
Copy link
Author

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?

@willfarrell
Copy link
Member

You need to set a setting on your lambda to allow it to work

@jjason685
Copy link
Author

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.

@willfarrell
Copy link
Member

willfarrell commented Nov 17, 2023

Function URLs: If receiving no content and non-200 status code are being converted to 200. Be sure to set Invoke Mode to RESPONSE_STREAM over BUFFERED.
https://middy.js.org/docs/intro/streamify-response

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?

@jjason685
Copy link
Author

Function URLs: If receiving no content and non-200 status code are being converted to 200. Be sure to set Invoke Mode to RESPONSE_STREAM over BUFFERED.
https://middy.js.org/docs/intro/streamify-response

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.

@willfarrell
Copy link
Member

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.

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)

@jjason685
Copy link
Author

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.

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 body is removed as a key from the response object, the function URL returns the response successfully.

handlerBody = handlerResponse.body ?? ''
responseStream = awslambda.HttpResponseStream.from(
responseStream,
handlerResponse
)

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 delete handlerResponse.body; before calling awslambda.HttpResponseStream.from should fix it.

mergify bot pushed a commit to SvenKirschbaum/aws-utils that referenced this issue Nov 26, 2023
[![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 (@&#8203;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 [@&#8203;anthony-nhs](https://togithub.com/anthony-nhs) in [middyjs/middy#1140
-   Streamed responses giving 502 Bad Gateway when payload exceeds threshold (25kb+) [@&#8203;jjason685](https://togithub.com/jjason685) [middyjs/middy#1137
-   http-response-serializer documentation incorrect [@&#8203;karmaniverous](https://togithub.com/karmaniverous) [middyjs/middy#1141

##### New Contributors

-   [@&#8203;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).
mergify bot pushed a commit to SvenKirschbaum/share.kirschbaum.cloud that referenced this issue Nov 26, 2023
[![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 (@&#8203;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 [@&#8203;anthony-nhs](https://togithub.com/anthony-nhs) in [middyjs/middy#1140
-   Streamed responses giving 502 Bad Gateway when payload exceeds threshold (25kb+) [@&#8203;jjason685](https://togithub.com/jjason685) [middyjs/middy#1137
-   http-response-serializer documentation incorrect [@&#8203;karmaniverous](https://togithub.com/karmaniverous) [middyjs/middy#1141

##### New Contributors

-   [@&#8203;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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants