AWS Lambda request does not stream provided data to the requesting client

0

I have a lambda responsible for running a simulation and returning the result to the requesting client. The result is 1 to 2 mbs in size. I read that streaming the data to the client using stream responses is the most performant way to relay that size of data, so that is what I have been attempting to setup.

My code looks like this:

const { Readable } = require('stream');
const pipeline = require("util").promisify(require('stream').pipeline);

exports.handler = awslambda.streamifyResponse(async (event, responseStream, _context) =>
{
    // generate simulation result
    matchResult = fs.readFileSync("/tmp/MatchResult.json");
    const requestStream = Readable.from(Buffer.from(matchResult));
    await pipeline(requestStream, responseStream);
    responseStream.end();
}

The await pipeline(requestStream, responseStream); does get run but I am not seeing any indication of a message being received or any errors on the lambda side indicating something is not setup correctly.

This is the yaml for the function:

  RunSinulation:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      MemorySize: 4000
      FunctionUrlConfig:
        AuthType: AWS_IAM
        InvokeMode: RESPONSE_STREAM
      Policies:
        - AmazonAPIGatewayInvokeFullAccess
        - S3CrudPolicy:
            BucketName: !Ref Bucket
      Environment:
        Variables:
          WebsocketEndpoint:
            Ref: WebsocketAPI
          BucketName:
            Ref: Bucket
    Metadata:
      Dockerfile: Dockerfile.dockerfile
      DockerContext: ./images/RunSinulation/

I am using a C# WebsocketSharp as the client.

Am I missing something on the server side, or is there something special I need to do with the client to receive these messages? I appreciate any help that can be offered.

gefragt vor 3 Monaten209 Aufrufe
1 Antwort
2
Akzeptierte Antwort

I am not sure what is your issue, however, 1-2MB you can just return from a function without streaming. You should use streaming if your response size is larger than 6 MB, or, if you want to minimize the Time To First Byte.

Also, streaming does not mean websockets. You should use a regular HTTP client to get the request, which should arrive in chunks.

profile pictureAWS
EXPERTE
Uri
beantwortet vor 3 Monaten
profile picture
EXPERTE
überprüft vor 2 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen