AWS lambda send partial response

亡梦爱人 提交于 2021-02-19 05:55:10

问题


I have a lambda function which does a series of actions. I have a react application which triggers the lambda function. Is there a way I can send a partial response from the lambda function after each action is complete.

const testFunction = (event, context, callback) => {
    let partialResponse1 = await action1(event);
    // send partial response to client
    let partialResponse2 = await action2(partialResponse1);
    // send partial response to client  
    let partialResponse3 = await action3(partialResponse2);
    // send partial response to client
    let response = await action4(partialResponse3); 
    // send final response
}

Is this possible in lambda functions? If so, how we can do this. Any ref docs or sample code would be do a great help. Thanks.

Note: This is fairly a simple case of showing a loader with % on the client-side. I don't want to overcomplicate things SQS or step functions.

I am still looking for an answer for this.


回答1:


From what I understand you're using API Gateway + Lambda and are looking to show the progress of the Lambda via UI.

Since each step must finish before the next step begin I see no reason not to call the lambda 4 times, or split the lambda to 4 separate lambdas.

E.g.:

// Not real syntax!

try {
  res1 = await ajax.post(/process, {stage: 1, data: ... });
  out(stage 1 complete);
  res2 = await ajax.post(/process, {stage: 2, data: res1});
  out(stage 2 complete);
  res3 = await ajax.post(/process, {stage: 3, data: res2});
  out(stage 3 complete);
  res4 = await ajax.post(/process, {stage: 4, data: res3});
  out(stage 4 complete);
  out(process finished);
catch(err) {
  out(stage {$err.stage-number} failed to complete);
}

If you still want all 4 calls to be executed during the same lambda execution you may do the following (this especially true if the process is expected to be very long) (and because it's usually not good practice to execute "long hanging" http transaction).

You may implement it by saving the "progress" in a database, and when the process is complete save the results to the database as well.

All you need to do is query the status every X seconds.

// Not real syntax

Gateway-API --> lambda1 - startProcess(): returns ID {
  uuid = randomUUID();
  write to dynamoDB { status: starting }.
  send sqs-message-to-start-process(data, uuid);
  return response { uuid: uuid };
}

SQS --> lambda2 - execute(): returns void {
    try {
      let partialResponse1 = await action1(event);
      write to dynamoDB { status: action 1 complete }.
      // send partial response to client
      let partialResponse2 = await action2(partialResponse1);
      write to dynamoDB { status: action 2 complete }.
      // send partial response to client  
      let partialResponse3 = await action3(partialResponse2);
      write to dynamoDB { status: action 3 complete }.
      // send partial response to client
      let response = await action4(partialResponse3); 
      write to dynamoDB { status: action 4 complete, response: response }.
    } catch(err) {
      write to dynamoDB { status: failed, error: err }.
    }
}

Gateway-API --> lambda3 -> getStatus(uuid): returns status {
  return status from dynamoDB (uuid);
}

Your UI Code:

res = ajax.get(/startProcess);
uuid = res.uuid;

in interval every X (e.g. 3) seconds:
  status = ajax.get(/getStatus?uuid=uuid);
  show(status);
  if (status.error) {
    handle(status.error) and break;
  }
  if (status.response) {
    handle(status.response) and break;
  }
}

Just remember that lambda's cannot exceed 15 minutes execution. Therefore, you need to be 100% certain that whatever the process does, it never exceeds this hard limit.




回答2:


What you are looking for is to have response expose as a stream where you can write to the stream and flush it

Unfortunately its not there in Node.js

How to stream AWS Lambda response in node?

https://docs.aws.amazon.com/lambda/latest/dg/programming-model.html

But you can still do the streaming if you use Java

https://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-stream.html

package example;

import java.io.InputStream;
import java.io.OutputStream;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.amazonaws.services.lambda.runtime.Context; 

public class Hello implements RequestStreamHandler{
    public void handler(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        int letter;
        while((letter = inputStream.read()) != -1)
        {
            outputStream.write(Character.toUpperCase(letter));
        }
    }
}



回答3:


Aman, You can push the partial outputs into SQS and read the SQS messages to process those message. This is a simple and scalable architecture. AWS provides SQS SDKs in different languages, for example, JavaScript, Java, Python, etc.

Reading and writing into SQS is very easy using SDK and that too can be implemented in serverside or in your UI layer (with proper IAM).




回答4:


I found AWS step function may be what you need:

AWS Step Functions lets you coordinate multiple AWS services into serverless workflows so you can build and update apps quickly.

Check this link for more detail:

In our example, you are a developer who has been asked to create a serverless application to automate handling of support tickets in a call center. While you could have one Lambda function call the other, you worry that managing all of those connections will become challenging as the call center application becomes more sophisticated. Plus, any change in the flow of the application will require changes in multiple places, and you could end up writing the same code over and over again.



来源:https://stackoverflow.com/questions/57052588/aws-lambda-send-partial-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!