AWS CDK(v2) BatchSubmitJob in StepFunctions to dynamically overwrite commands from Context and Input

Page content

Introduction

When executing AWSBatch from StepFunctions in AWS CDK, it is convenient to use BatchSubmitJob, but I could not find out how to overwrite containerOverrides.command, so I have written it down.

Version

  • aws-cdk: 2.20.0

How to dynamically overwrite command from Context or Input with BatchSubmitJob

There is a aws_stepfunctions.JsonPath, and if string[] is an expected parameter, you can use JsonPath.listAt(JSON_PATH). There are also JsonPath.numberAt(JSON_PATH) and stringAt().

Thus, for example, you can write as followings.

Note that if the specified JSON_PATH is not found in the input JSON, an error will occur.

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 batchTask = new tasks.BatchSubmitJob(this, `SubmitJobTask`, {
    jobDefinitionArn: JOB_DEF_ARN,
    jobName: "SubmitJob",
    jobQueueArn: JOB_QUEUE_ARN,
    containerOverrides: {
        command: sfn.JsonPath.listAt("$.commands"),
    },
    attempts: sfn.JsonPath.numberAt("$.attempts"),
    });
    // ...
  }

JSON example of input

{
  "commands": ["echo", "hello"],
  "attempts": 3
}

Afterward

I have a hard time pinpointing this kind of information in a search.