The size limit of 6MB on Lambda was always a blocker for many people. That was the BUFFERED way. But since AWS announced the new response streaming, we can now stream data via function URL or AWS SDK .
In this tutorial we will learn how to take advantage of RESPONSE_STREAM to send response asynchronously instead of the BUFFERED invocation mode on AWS Lambda.
NOTE: As of 2023, Lambda response stream is only supported in NodeJS and Python runtimes, so .NET and Java runtime users, please sit tight.
Create Lambda function with stream

Enable Response Stream
- Expand the Advanced settings by clicking on it.
- Enable function URL by clicking the checkbox.
- Select the Auth type to NONE.
- Choose RESPONSE_STREAM in the Invoke mode at the bottom.
Once you’re done, simply click on Create Function button at the bottom of the page.
You’ll be greeted with your new Lambda and a successful message.
Update code to use stream
We now have Lambda which supports streaming response but it’s not ready yet.
To make use of streams we need to wrap our function in awslambda.streamifyResponse.
So now our code is:
export const handler = awslambda.streamifyResponse(
async (event, responseStream, _context) => {
// rest of the code
}
)
We also receive a new parameter as responseStream which we can utilise to store the stream.
Simple example which writes from 0 to 100
We can now use a for loop which will write to the response stream after waiting for 20 milliseconds.
Your Lambda will timeout if you try to make it wait for longer than the timeout which is set on the Lambda by default.
export const handler = awslambda.streamifyResponse(
async (event, responseStream, _context) => {
// Metadata is a JSON serializable JS object. Its shape is not defined here.
const metadata = {
statusCode: 200,
headers: {
"Content-Type": "text/plain"
}
};
// Assign to the responseStream parameter to prevent accidental reuse of the non-wrapped stream.
responseStream = awslambda.HttpResponseStream.from(responseStream, metadata);
for (var i = 0; i <= 100; i++) {
await new Promise(r => setTimeout(r, 20));
responseStream.write(`Hello ${i} \n`);
}
// finish the
responseStream.end();
await responseStream.finished();
}
);
Test response streaming with curl
- Copy your Lambda function URL which looks like
xxx.us-east-1.on.awsfrom the Lambda console.
Browsers might not show you the text until it’s finished loading, so we use curl to see the real magic of response streaming.
Open your terminal and make a GET request with curl:
curl `xxx.us-east-1.on.aws`
Here is the screen recording of my terminal which will blow your mind:
Send Images & Binary Files with Stream pipeline
Sending images or any binary file from AWS Lambda was always a trouble. But with streams it’s just a piece of cake.
Let’s see how we can send an image from Lambda
Cloud9 IDE doesn’t give us the ability to upload files directly so we will have to zip the image with the code.
So your zip should look like:
stream-demo:
- image.jpeg
- index.mjs
Code for Sending Image in the stream
It’s just 3 lines of code which will do the magic for us. Here is what’s changed from the previous code:
- Import
fsfromnode:fsandpipelinefromnode:stream/promises - Specify the
"Content-Type": "image/jpeg"in the metadata. - Create file stream with
fs.createReadStream("./persian-cat.jpg") - Put them in the pipeline
await pipeline(imgStream, responseStream);
import fs from "node:fs";
import { pipeline } from "node:stream/promises";
export const handler = awslambda.streamifyResponse(
async (event, responseStream, _context) => {
// Metadata is a JSON serializable JS object. Its shape is not defined here.
const metadata = {
statusCode: 200,
headers: {
"Content-Type": "image/jpeg",
},
};
// Assign to the responseStream parameter to prevent accidental reuse of the non-wrapped stream.
responseStream = awslambda.HttpResponseStream.from(
responseStream,
metadata
);
const imgStream = fs.createReadStream("./persian-cat.jpg");
await pipeline(imgStream, responseStream);
responseStream.end();
await responseStream.finished();
}
);
Now go to your Lambda and upload the zip file
- In the Code tab go to top right corner where it shows Upload from button and click on .zip file
- Click on Upload button and select the your zipped Lambda.
- Click on Save
Here are the screenshots to help you out:

Test the Image stream
Open the function URL in your browser and you’ll see the image loading slowly on the page. We can throttle the network speed to 3G to see it load.
Even the favicon got updated with Persian cat.

Here is a video demo of showing image being loaded in Slow 3G:
With the Pro, comes the Conman.
You might be thinking:
Oh, I can now use this Lambda stream feature with Amazon API Gateway as well!
Wrong. AWS is yet to integrate streams with API Gateway or Cloudfront. Feel free to open support ticket asking AWS to support streams with API Gateway.
Limitations of AWS Lambda with response streaming
- Only supports NodeJS runtime
- Can only be invoked via Function URL or AWS API
- Can’t be used with CDN to cache response
- No TypeScript typings
I hope you learnt something new, if you did make sure to comment your ideas in the comments.