简体   繁体   中英

AWS CDK Step Function EcsRunTask Use Input Variable For Task Definition

I am trying to convert a Step Function that I have in JSON to CDK (Typescript), but am not sure how to reference input variables from the previous step.

My existing Step Function Task:

      "Start Task": {
        "Type": "Task",
        "Resource": "arn:aws:states:::ecs:runTask.sync",
        "Parameters": {
          "LaunchType": "EC2",
          "Cluster": "my cluster arn",
          "TaskDefinition.$": "$.task"
        },
        "Next": "Task Success",
        "Catch": [
          {
            "ErrorEquals": [
              "States.ALL"
            ],
            "Next": "Task Failure"
          }
        ]
      }

How would I do "TaskDefinition.$": "$.task" in CDK? Or if I use CDK do I have to specify the task definition to run, instead of letting the input decide?

Any help would be greatly appreciated. Thanks!

The CDK aws_stepfunctions_tasks module has several ways to add an ECS task to a Step Function:

EcsRunTask : adds a ECS RunTask Optimised Integration , but does not accept an imported ARN as the Task Definition. There is an open feature request that explains why not. Supports .sync ( RUN_JOB ).

CallAwsService : the CDK equivalent of Step Functions AWS SDK Integrations . You can pass $.task as the Task Definition ARN, but can't use the .sync pattern.

To use an input variable for the Task Definition AND wait for a response with .sync , you will need to get your hands dirty and subclass the TaskStateBase construct. You must handle IAM permissions yourself. Here's a minimal implementation:

export class RunEcsTaskAtPath extends sfn.TaskStateBase {
  protected readonly taskMetrics?: sfn.TaskMetricsConfig;
  protected readonly taskPolicies?: iam.PolicyStatement[];

  constructor(scope: Construct, id: string, private readonly props: TaskStateBaseProps) {
    super(scope, id, props);
   
    this.taskPolicies = [
      // add the required policies:
      //    https://docs.aws.amazon.com/step-functions/latest/dg/ecs-iam.html
      // see also the `EcsRunTaskBase` `makePolicyStatements` method_
      //    https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/aws-stepfunctions-tasks/lib/ecs/run-ecs-task-base.ts
    ];
  }

  protected _renderTask(): any {
    return sfn.FieldUtils.renderObject({
      Resource: 'arn:aws:states:::ecs:runTask.sync',
      Parameters: {
        LaunchType: 'EC2',
        Cluster: 'my cluster arn',
        TaskDefinition: sfn.JsonPath.stringAt('$.task'),
        // other config: https://docs.aws.amazon.com/step-functions/latest/dg/connect-ecs.html
      },
    });
  }
}

Note the use of the CDK JsonPath helpers . This is optional. You can also just hardcode the state machine syntax ( 'TaskDefinition.$': '$.task' ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM