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'
).