Trying to get the new lambda response streaming example setup.
Link: https://aws.amazon.com/about-aws/whats-new/2023/04/aws-lambda-response-payload-streaming/
Link: https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html
I've set my Lambda configuration to be:
The following is my lambda code
Lambda code taken from: https://www.pulumi.com/blog/aws-lambda-response-streaming/
export const handler = awslambda.streamifyResponse(async (payload, responseStream, context) => {
try {
responseStream.setContentType("text/plain");
const sentences = [
"1 sentence....",
"2 sentence....",
"3 sentence....",
"4 sentence....",
"5 sentence....",
"6 sentence....",
"7 sentence....",
"8 sentence....",
"9 sentence....",
"10 sentence....",
"11 sentence....",
]
const timer = ms => new Promise(res => setTimeout(res, ms));
for (let i = 0; i < sentences.length; i ++) {
responseStream.write(`${sentences[i]}\n`);
console.log(`${sentences[i]}\n`);
await timer(1000);
}
responseStream.end();
} catch(e){
console.error(`Unknown error ${e.name} ${e.message}`);
}
});
I then ran the following command to test out response streaming.
Command run: curl -N -w "Connect: %{time_connect} TTFB: %{time_starttransfer} Total Time: %{time_total} \n" https://iu42edsafsbs7smopwrau3qfky0ixwmi.lambda-url.us-east-1.on.aws/
Taken from: https://www.pulumi.com/blog/aws-lambda-response-streaming/
Results:
curl -N -w "Connect: %{time_connect} TTFB: %{time_starttransfer} Total Time: %{time_total} \n" https://iu42edsafsbs7smopwrau3qfky0ixwmi.lambda-url.us-east-1.on.aws/
1 sentence....
2 sentence....
3 sentence....
4 sentence....
5 sentence....
6 sentence....
7 sentence....
8 sentence....
9 sentence....
10 sentence....
11 sentence....
Connect: 0.050423 TTFB: 11.559064 Total Time: 11.940257
I expect the results of TTFB to be ~1.5 seconds and total time to be ~12 seconds, but instead it gives it as a buffered response which is when it is completed.
When viewing cloudwatch logs, the console.log() is logging after a second delay. It appears as if the responseStream.write() is not working as intended
I had the same issue. I swapped my region from US-EAST-1 to US-EAST-2 and the same code worked like charm! Either this feature is broken in US-EAST-1 or it may not be available yet (contrary to the announcements). I have some others user reporting similar issues on a reddit thread and have confirmed changing the region from US-EAST-1 to US-EAST-2 works (haven't confirmed other regions). https://www.reddit.com/r/aws/comments/13om387/comment/jlb6it3/?utm_source=share&utm_medium=web2x&context=3
Thanks! Same issue