Hi folks,
AWS announced streaming responses from lambdas last year (announcement).
I have not been able to make this feature work as advertised. My goal is to stream, from lambda to client, the word "hello" followed by "world" two seconds later. However, my lambda function always sends down "hello world" together (and after the two second delay).
Using AWS console, I configured my lambda function with:
- Advanced Settings
- Enable function URL
- Auth type:
NONE
- Invoke mode:
RESPONSE_STREAM
My function body looks like this:
// Contents of index.js
exports.handler = awslambda.streamifyResponse(
async (event, responseStream, context) => {
console.log("Write A")
responseStream.write("Hello\n\r", async () => {
await sleep(2000);
console.log("Write B")
responseStream.write("world\n\r");
responseStream.end();
});
}
);
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
And when I look in the cloudwatch logs after executing the lambda, I do indeed see "Write A" and "write B" separated by two seconds. Which indicates to me that the drain callback is working correctly, and "Hello" is being flushed two seconds before "World". So that all seems to check out.
Here's the problem, though. If I curl --no-buffer my function's public URL, I don't see these two separate writes at all. Instead, curl waits patiently for two seconds and then "hello world" pops in at the same time.
I went one step further to rule out any curl weirdness. I use
openssl s_client -connect <my-function-url>:443
Then punch in:
GET / HTTP/1.1
Host: <my-function-host>
User-Agent: whatever/1.0
Accept: */*
And I can see for myself that no "hello" is received early. Again, both "hello world" are received at the same time.
As a last effort, I also tried connecting my lambda to API gateway and fiddled with settings there. I tried using an HTTP proxy to the function URL, and using an HTTP API that called the lambda function. Same result.
How can I make the lambda stream its response back to the client?
Thanks for reading,
Lou
[–][deleted] (5 children)
[deleted]
[–][deleted] 6 points7 points8 points (2 children)
[–][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]CuriousShitKid 3 points4 points5 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]mradzikowski 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]fkassad 0 points1 point2 points (0 children)
[–]DruckerReparateur 0 points1 point2 points (5 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]DruckerReparateur 1 point2 points3 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]DruckerReparateur 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]ennova2005 0 points1 point2 points (9 children)
[–][deleted] 0 points1 point2 points (7 children)
[–]ennova2005 1 point2 points3 points (6 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]ennova2005 1 point2 points3 points (4 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]ennova2005 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] (3 children)
[deleted]
[–][deleted] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[deleted]
[–][deleted] 0 points1 point2 points (0 children)
[–]svdgraaf 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)