Guide for Lambda as Activities
nitin02 opened this issue · 4 comments
I'm currently in the process of creating a SWF with multiple Lambda functions as activities. Can you provide an example/documentation on how to achieve this. I have searched a lot for how Lambda functions can be scheduled using deciders but couldn't find any.
Basically, In my case, The input to the Workflow (from the WorkFlowStarter) determines which Lambda function to be executed first. Thereafter, the result of a Lambda function determines which Lambda function to be executed next. An example (in Java) would be helpful.
I agree, and will add such a topic.
For now, you can look at AWS Lambda Tasks in the SWF developer guide. It's not language-specific, but it does show the data that you'd need to pass to ScheduleLambdaFunctionDecisionAttributes and provides other information that you'll need, such as setting up the necessary IAM role.
Here are the basics using the Java SDK:
In your workflow worker, you respond to a decision task by returning a list of Decisions in your RespondDecisionTaskCompletedRequest object:
ScheduleActivityTaskDecisionAttributes attrs =
new ScheduleActivityTaskDecisionAttributes()
.withActivityType(new ActivityType()
.withName(HelloTypes.ACTIVITY)
.withVersion(HelloTypes.ACTIVITY_VERSION))
.withActivityId(UUID.randomUUID().toString())
.withInput(workflow_input);
decisions.add(
new Decision()
.withDecisionType(DecisionType.ScheduleActivityTask)
.withScheduleActivityTaskDecisionAttributes(attrs));If you're scheduling an Activity, the decisions specify a DecisionType.ScheduleActivityTask with the necessary data to identify the activity to run (and input data, etc.) in ScheduleActivityTaskDecisionAttributes.
If you want to run a Lambda function instead of an Activity, you respond with DecisionType.ScheduleLambdaFunction, and an associated ScheduleLambdaFunctionDecisionAttributes.
The fields that are required are name (the Lambda function name) and id (An identifier of your choice that SWF uses to identify the task).
Note that the Lambda function must be in the same region as your workflow, and the workflow must be registered with an IAM role that gives SWF access to call Lambda functions, by using the .withDefaultLambdaRole() method in the RegisterWorkflowTypeRequest.
Creating this IAM role is described in the Lambda Tasks article.
Thank you for your succinct response.
Would you suggest registering these Lambda functions as individual Activities (The Lambda function will be called inside the Activity wrapper as a registered ActivityType) or would you suggest scheduling them directly from the decider (WorkFlowWorker). Is there any specific advantage?
Currently, I'm under the impression that calling a Lambda function inside a ActivityType would be better in a logging perspective i.e I'd know which Lambda function failed from the Activity log rather than the event log.
Also, there might be a way to know to monitor the progress of a workflow execution by understanding what Activity is being presently executed.
While you can do this (call Lambda from inside an activity), the SWF API is designed around using Lambda functions as top-level tasks, just like classic SWF activities are. Lambda tasks report on your workflow event history, and you also get standard Lambda function history + run transcripts reported by CloudWatch.
Within a Lambda task, you can log information simply by using System.out/System.err.println(). There's a lot more information about this in the Java Programming Model section in the Lambda Guide.
Update: The new Amazon SWF Lambda tasks topic has been added. Closing this issue!