AWS CDK(v2) LambdaInvoke in StepFunctions to dynamically override Payload from Context or Input

Page content

Introduction

When executing Lambda from StepFunctions with AWS CDK, it is useful to use LambdaInvoke, but I would like to note how to map the payload from the Input information. You can find it by searching for LambdaInvoke, but you need to be careful because it is different from AWSBatch’s BatchSubmitJob.

Version

  • aws-cdk: 2.20.0

How to dynamically override Payload from Context or Input in LambdaInvoke

It is recommended to use TaskInput.fromObject() for Lambda’s payload (Object passed to Lambda’s Handler). The object specified here is passed directly to the handler.

import {
  aws_stepfunctions as sfn,
  aws_stepfunctions_tasks as tasks,
} from "aws-cdk-lib";

export class MyAwesomeStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);
    // ...
    const reportTask = new tasks.LambdaInvoke(this, "ReportTask", {
    lambdaFunction: reportLambda,
    payload: sfn.TaskInput.fromObject({
        taskName: sfn.JsonPath.stringAt("$.taskName"),
        report: sfn.JsonPath.objectAt("$.report"),
    }),
  });
}

JSON example of input

{
  "taskName": "MyGreatTask",
  "report": {
    "profit": "5000兆円"
  }
}

Afterward

It was a memo.