New Lambda streaming, can't get basic demos to work as expected, TTFB = full running time

2

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: Configuration for lambda

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

asked 9 months ago361 views
1 Answer
3
Accepted Answer

In case anyone else is stuck.

I ended up swapping regions from us-east-1 to us-east-2 with the exact same code and configuration and it ended up working.

This is contray to this announcement post: https://aws.amazon.com/about-aws/whats-new/2023/04/aws-lambda-response-payload-streaming/

answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions