Skip to content

Schedule lambda function

Gurmit Teotia edited this page Oct 24, 2018 · 1 revision

You can orchestrate the executions of AWS Lambda functions in a workflow as shown in following example:

[WorkflowDescription("1.0", DefaultLambdaRole= "lambda role to invoke lambda function")]
public class BookHolidaysWorkflow : Workflow
{
  public BookHolidaysWorkflow()
  {
     ScheduleLambda("BookHotel");
     ScheduleLambda("AddDinner").After("BookHotel");
  }
}

Lambda role

Create a role, as described in this article, and provide it either in WorkflowDescriptionAttribute or when starting the workflow. This role is needed by AWS SWF to invoke the lambda functions.

Lambda input:

By default workflow's input is passed as input to the lambda function however you have can customize it. Following examples shows various ways to pass input to lambda function:

[WorkflowDescription("1.0")]
public class BookHolidaysWorkflow : Workflow
{
  public BookHolidaysWorkflow()
  {
     ScheduleLambda("BookHotel").WithInput(_=> new {CustomerName = Input.CustomerName});
     ScheduleLambda("AddDinner")
             .WithInput(l=>new {BookingId = l.ParentLambda().Result().BookingId})
            .After("BookHotel");
  }
}

Error handling:

By default workflow is failed when lambda functions is failed/timedout however you can configure it take custom action as shown in below example:

[WorkflowDescription("1.0", DefaultLambdaRole= "lambda role to invoke lambda function")]
public class BookHolidaysWorkflow : Workflow
{
 public BookHolidaysWorkflow()
 {
    //BookHotel lambda function will be rescheduled on failure. It will retry it for 2 times after which it will fail the workflow.
    ScheduleLambda("BookHotel").OnFailure(e=>Reschedule(e).UpTo(times:2));
    
    //AddDinner lambda will be rescheduled on timedout every time.     
    ScheduleLambda("AddDinner").After("BookHotel")
                              .OnTimedout(Reschedule);
 }
}

Scheduling same lambda function multiple times

ScheduleLambda API accept an optional argument "positionalName", which you can use to schedule same lambda function at multiple places in same workflow:

[WorkflowDescription("1.0", DefaultLambdaRole= "lambda role to invoke lambda function")]
public class BookHolidaysWorkflow : Workflow
{
 public BookHolidaysWorkflow()
 {
    ScheduleLambda("TranscodeVideo", "WAVFormat").OnFailure(Reschedule);
    
    ScheduleLambda("TranscodeVideo", "MP4Format").AfterLambda("TranscodeVideo", "WAVFormat")
                              .OnTimedout(Reschedule);
 }
}
Clone this wiki locally