aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

aws-stepfunctions-tasks: Confusion between RunLambdaTask and InvokeFunction

Open mifisignal opened this issue 7 years ago • 9 comments

There are now two similar-looking constructs in aws-stepfunctions-tasks pertaining to Lambda tasks, and it's not clear which one to use.

Users who are used to historical Step Functions patterns and want things to easily work should use InvokeFunction. However, there are some corner cases where people want special behavior (e.g., asynchronous execution) who like to use RunLambdaTask.

If you mix these up, be prepared to spend a lot of time debugging. @ccfife and I spent the afternoon scratching our heads to figure out, when we accidentally used RunLambdaTask, why the event payload being sent to our Lambda function Tasks was an empty event.

Let's do something to disambiguate these and make clear when a user should use one or the other.

mifisignal avatar Oct 31 '19 22:10 mifisignal

I'm open to ideas for good verbiage to put in the class descriptions.

rix0rrr avatar Nov 13 '19 14:11 rix0rrr

Or the README

rix0rrr avatar Nov 13 '19 14:11 rix0rrr

Since I am getting obsolete warning on InvokeFunction(this has been working fine for us), we moved to RunLambdaTask. Since then we have been receiving null ref exception in the lambda function. Seems like the payload passed to lambda function is null? Any thoughts? @rix0rrr @otterley The code with InvokeFunction looked like this: I am using C#

            var postToApiTask = new Task(scope, $"{props.UniqueFunctionSuffix}-PostAPI-Task", new TaskProps
            {
                Task = new InvokeFunction(props.PostToAPILambda), //this is the lambda function
                InputPath = "$",
                ResultPath = "$.data.lambdaResult",
                Comment = "This task invokes API endpoint",
                Timeout = Duration.Seconds(30)
            });

I just replace InvokeFunction with RunLambdaTask. I tried setting some properties as well, but no luck.

sacag avatar Apr 14 '20 04:04 sacag

@sacag can you share the code that you were using when you converted to RunLambdaTask? which properties did you set?

Did you set the IntegrationPattern property? Wondering if it's the default of FIRE_AND_FORGET that's biting you here.

In TypeScript it would look something like this:

  task: new tasks.RunLambdaTask(postToAPILambda, {
    integrationPattern: sfn.ServiceIntegrationPattern.WAIT_FOR_TASK_TOKEN,
    payload: {
      token: sfn.Context.taskToken
    }
  }),
  inputPath: '$',
  resultPath: '$.lambdaResult',
  comment: 'This task invokes API endpoint',
  timeout: Duration.seconds(30)
});

shivlaks avatar Apr 14 '20 04:04 shivlaks

@shivlaks - I did not set the IntegrationPattern property. Also, my lambda function is not accepting/returning any token or anything of that sort. Below is how my C# code looks like: var postToApiTask = new Task(scope, $"{props.UniqueFunctionSuffix}-PostAPI-Task", new TaskProps { Task = new InvokeFunction(postToAPILambda), //this is the lambda function InputPath = "$", ResultPath = "$.data.lambdaResult", Comment = "This task invokes API endpoint", Timeout = Duration.Seconds(30) });

In the above code I just replaced InvokeFunction with RunLambdaTask and it stopped working

Are you suggesting I shall try with the props you are setting in your typescript code and see? without any changes to the lambda function?

sacag avatar Apr 14 '20 14:04 sacag

@shivlaks - I tried Context.EntireContext, Context.TaskToken..still same issue

            var applyFilterTransformTask = new Task(scope, $"{props.UniqueFunctionSuffix}-FilterTransform-Task", new TaskProps
            {
                Task = new RunLambdaTask(props.FilterTransformLambda, 
                        new RunLambdaTaskProps { IntegrationPattern = ServiceIntegrationPattern.WAIT_FOR_TASK_TOKEN, 
                                                Payload = new Dictionary<string, object>() { { "input",Context.EntireContext} } }),
                Comment = $"This task applies source/destination filters to {props.UserFriendlyTopicName} Message and also Transforms it",
                InputPath = "$",
                ResultPath = "$.data.lambdaResult",
                OutputPath = "$.data.lambdaResult",
                Timeout = Duration.Seconds(15),                
            });

sacag avatar Apr 14 '20 14:04 sacag

image @shivlaks

sacag avatar Apr 14 '20 15:04 sacag

@shivlaks - finally got it to work with the below code: new RunLambdaTask(props.PostToAPILambda,new RunLambdaTaskProps { Payload = new Dictionary<string, object>() { { "input", Context.StringAt("$$.Execution.Input") } } })

sacag avatar Apr 14 '20 17:04 sacag

@shivlaks, @rix0rrr - I am seeing more issues with RunLambdaTask: below is the code which works with InvokeFunction..$.Payload has my model in json string

 Task = new InvokeFunction(lambdaFunction),                
                InputPath = "$.Payload",
                ResultPath = "$.lambdaResult",
                OutputPath = "$.lambdaResult"

The same code when using RunLambdaTask...not sure how to access $.Payload in the Payload property, so that it can be passed over to the Lambda function? This is what I have. This passes NULL to my lambda function

Task = new RunLambdaTask(lambdaFunction,
                            new RunLambdaTaskProps 
{ Payload = new Dictionary<string, object>() { { "lambdaResult", Context.StringAt("$$.Payload") } } }),
                InputPath = "$.Payload",
                ResultPath = "$.lambdaResult",
                OutputPath = "$.lambdaResult"

When I do Context.StringAt(...) in RunLambdaTask, i think it takes the execution's input and not the task input..i think, maybe there is another way to access the TaskState input?
@otterley - wondering if you were able to figure this out?

sacag avatar Apr 15 '20 16:04 sacag