Skip to content

Commit

Permalink
set the default for payload to a Lambda to use the state input
Browse files Browse the repository at this point in the history
  • Loading branch information
shivlaks committed Apr 23, 2020
1 parent 1029f30 commit a99b6c5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface RunLambdaTaskProps {
/**
* The JSON that you want to provide to your Lambda function as input.
*
* @default - No payload
* @default - The state input (JSON path '$')
*/
readonly payload?: sfn.TaskInput;

Expand Down Expand Up @@ -92,7 +92,7 @@ export class RunLambdaTask implements sfn.IStepFunctionsTask {
metricDimensions: { LambdaFunctionArn: this.lambdaFunction.functionArn },
parameters: {
FunctionName: this.lambdaFunction.functionName,
Payload: this.props.payload?.value,
Payload: this.props.payload ? this.props.payload.value : sfn.TaskInput.fromDataAt('$').value,
InvocationType: this.props.invocationType,
ClientContext: this.props.clientContext,
Qualifier: this.props.qualifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "exports.handler = async function(event, context) {\n return {\n status: event.statusCode === '200' ? 'SUCCEEDED' : 'FAILED';\n };\n };"
"ZipFile": "exports.handler = async function(event, context) {\n return {\n status: event.statusCode === '200' ? 'SUCCEEDED' : 'FAILED'\n };\n };"
},
"Handler": "index.handler",
"Role": {
Expand Down Expand Up @@ -175,15 +175,15 @@
{
"Ref": "submitJobLambdaEFB00F3C"
},
"\"},\"OutputPath\":\"$.Payload\",\"Type\":\"Task\",\"Resource\":\"arn:",
"\",\"Payload.$\":\"$\"},\"OutputPath\":\"$.Payload\",\"Type\":\"Task\",\"Resource\":\"arn:",
{
"Ref": "AWS::Partition"
},
":states:::lambda:invoke\"},\"Check the job state\":{\"Next\":\"Job Complete?\",\"Parameters\":{\"FunctionName\":\"",
{
"Ref": "checkJobStateLambda4618B7B7"
},
"\",\"Payload.$\":\"$.Payload\"},\"OutputPath\":\"$.Payload\",\"Type\":\"Task\",\"Resource\":\"arn:",
"\",\"Payload.$\":\"$\"},\"OutputPath\":\"$.Payload\",\"Type\":\"Task\",\"Resource\":\"arn:",
{
"Ref": "AWS::Partition"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import * as tasks from '../../lib';

/*
* Stack verification steps:
* The generated State Machine can be executed from the Step Functions console (or CLI)
* and run with an execution status of `Succeeded`.
* The generated State Machine can be executed from the CLI (or Step Functions console)
* and runs with an execution status of `Succeeded`.
*
* -- aws stepfunctions start-execution --state-machine-arn <deployed state machine arn> provides execution arn
* -- aws stepfunctions describe-execution --execution-arn <from previous command> returns a status of `Succeeded`
Expand All @@ -33,17 +33,15 @@ const submitJob = new sfn.Task(stack, 'Invoke Handler', {
const checkJobStateLambda = new Function(stack, 'checkJobStateLambda', {
code: Code.fromInline(`exports.handler = async function(event, context) {
return {
status: event.statusCode === '200' ? 'SUCCEEDED' : 'FAILED';
status: event.statusCode === '200' ? 'SUCCEEDED' : 'FAILED'
};
};`),
runtime: Runtime.NODEJS_10_X,
handler: 'index.handler',
});

const checkJobState = new sfn.Task(stack, 'Check the job state', {
task: new tasks.RunLambdaTask(checkJobStateLambda, {
payload: sfn.TaskInput.fromDataAt('$.Payload'),
}),
task: new tasks.RunLambdaTask(checkJobStateLambda),
outputPath: '$.Payload',
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,39 @@ test('Lambda function can be used in a Task with Task Token', () => {
});
});

test('Lambda function can be provided with the task input as the payload', () => {
test('Lambda function is invoked with the state input as payload by default', () => {
const task = new sfn.Task(stack, 'Task', {
task: new tasks.RunLambdaTask(fn),
});
new sfn.StateMachine(stack, 'SM', {
definition: task,
});

expect(stack.resolve(task.toStateJson())).toEqual({
Type: 'Task',
Resource: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':states:::lambda:invoke',
],
],
},
End: true,
Parameters: {
'FunctionName': {
Ref: 'Fn9270CBC0',
},
'Payload.$': '$',
},
});
});

test('Lambda function can be provided with the state input as the payload', () => {
const task = new sfn.Task(stack, 'Task', {
task: new tasks.RunLambdaTask(fn, {
payload: sfn.TaskInput.fromDataAt('$'),
Expand Down
16 changes: 7 additions & 9 deletions packages/@aws-cdk/aws-stepfunctions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,14 @@ similar to (for example) `inputPath`.
[Invoke](https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html) a Lambda function.

You can specify the input to your Lambda function through the `payload` attribute.
By default, no payload is specified so Step Functions invokes Lambda with the empty
object `{ }` as input.
By default, Step Functions invokes Lambda function with the state input (JSON path '$')
as the input.

The following snippet invokes a Lambda Function with the state input as the payload
by referencing the `$` path.

```ts
new sfn.Task(this, 'Invoke with task context', {
task: new tasks.RunLambdaTask(myLambda, {
payload: sfn.TaskInput.fromDataAt('$'),
}),
});
new sfn.Task(this, 'Invoke with state input');
```

When a function is invoked, the Lambda service sends [these response
Expand All @@ -186,8 +182,10 @@ The following snippet invokes a Lambda Function by referencing the `$.Payload` p
to reference the output of a Lambda executed before it.

```ts
new sfn.Task(this, 'Invoke with empty payload', {
task: new tasks.RunLambdaTask(myLambda),
new sfn.Task(this, 'Invoke with empty object as payload', {
task: new tasks.RunLambdaTask(myLambda, {
payload: sfn.TaskInput.fromObject({})
}),
});

new sfn.Task(this, 'Invoke with payload field in the state input', {
Expand Down

0 comments on commit a99b6c5

Please sign in to comment.