Skip to content

Conversation

@go-to-k
Copy link
Contributor

@go-to-k go-to-k commented Sep 11, 2025

Issue # (if applicable)

Closes #20013 #30171 #7967

Reason for this change

The capacity provider strategy can't be set on EcsRunTask with EcsFargateLaunchTargetOptions and EcsEc2LaunchTargetOptions.

capacityProviderStrategy

The capacity provider strategy to use for the task.

If a capacityProviderStrategy is specified, the launchType parameter must be omitted. If no capacityProviderStrategy or launchType is specified, the defaultCapacityProviderStrategy for the cluster is used.

When you use cluster auto scaling, you must specify capacityProviderStrategy and not launchType.

A capacity provider strategy can contain a maximum of 20 capacity providers.

https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_RunTask.html

Description of changes

This PR adds a new property capacityProviderOptions (CapacityProviderOptions class with static factory methods as an union-like class) in the target options.

The CapacityProviderOptions has following sub types:

  • NoCapacityProviderOptions
  • CustomCapacityProviderOptions
  • DefaultCapacityProviderOptions

The NoCapacityProviderOptions creates the original settings with LaunchType (EC2 or FARGATE). It is a default value for the capacityProviderOptions.

The CustomCapacityProviderOptions allows users to set the capacity provider strategy without LaunchType.

The DefaultCapacityProviderOptions allows users to use the cluster's default capacity provider strategy. The default strategy can be set by specifying no options (LaunchType and CapacityProviderStrategy) in CFn.

Describe any new or updated permissions being added

Description of how you validated changes

Both unit tests and integ tests.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team September 11, 2025 08:27
@github-actions github-actions bot added effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p2 distinguished-contributor [Pilot] contributed 50+ PRs to the CDK labels Sep 11, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@go-to-k go-to-k marked this pull request as ready for review September 11, 2025 12:35
@aws-cdk-automation aws-cdk-automation dismissed their stale review September 11, 2025 12:36

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@@ -1,3 +1,3 @@
FROM public.ecr.aws/docker/library/python:3.12
FROM --platform=linux/amd64 public.ecr.aws/docker/library/python:3.12
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because amd64 is used for cpu architecture in integ tests.

If this option is not specified, running the tests on Mac will result in an error during state machine execution.

@go-to-k go-to-k changed the title feat(stepfunctions-tasks): allow EcsRunTask on Fargate and EC2 to set capacity provider strategy feat(stepfunctions-tasks): allow EcsRunTask on fargate and ec2 to set capacity provider strategy Sep 11, 2025
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Sep 11, 2025
Copy link
Contributor

@badmintoncryer badmintoncryer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! I've added some minor comments.

Comment on lines 12 to 13
stack.node.setContext(EC2_RESTRICT_DEFAULT_SECURITY_GROUP, false);
stack.node.setContext(STEPFUNCTIONS_TASKS_FIX_RUN_ECS_TASK_POLICY, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these feature flags are needed?

Comment on lines 13 to 16
postCliContext: {
'@aws-cdk/aws-ecs:enableImdsBlockingDeprecatedFeature': false,
'@aws-cdk/aws-ecs:disableEcsImdsBlocking': false,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there feature flags needed?

Comment on lines 19 to 20
stack.node.setContext(EC2_RESTRICT_DEFAULT_SECURITY_GROUP, false);
stack.node.setContext(STEPFUNCTIONS_TASKS_FIX_RUN_ECS_TASK_POLICY, false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to unify the method for setting feature flags either through postCliContext or setContext. (If these flags are needed!)

The `capacityProviderOptions` property allows you to configure the capacity provider
strategy for both EC2 and Fargate launch targets.

- When `CapacityProviderOptionsBase.none()` is used, the task uses the launch type (EC2 or FARGATE) without a capacity provider strategy.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's true that CapacityProviderOptionsBase is an abstract base class, but I don't think users will be aware of the concrete classes that inherit from it when specifying their settings.
I thought it would be more user-friendly to name it simply CapacityProviderOptions. What do you think?

/**
* No capacity provider strategy options
*/
export class NoCapacityProviderOptions extends CapacityProviderOptionsBase {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this concrete class be exported?
I think user uses not a NoCapacityProviderOptions but a abstract class.

],
launchTarget: new tasks.EcsFargateLaunchTarget({
platformVersion: ecs.FargatePlatformVersion.VERSION1_4,
capacityProviderOptions: tasks.NoCapacityProviderOptions.none(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better to use a abstract class?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relates to #35465 (comment)

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Dec 12, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 17, 2025

⚠️ Experimental Feature: This security report is currently in experimental phase. Results may include false positives and the rules are being actively refined.
Please try merge from main to avoid findings unrelated to the PR.


TestsPassed ☑️SkippedFailed ❌️
Security Guardian Results100 ran98 passed2 failed
TestResult
Security Guardian Results
packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/ecs/integ.ec2-run-task-capacity-provider.js.snapshot/aws-sfn-tasks-ecs-ec2-run-task-capacity-provider.template.json
iam-no-overly-permissive-passrole.guard❌ failure
packages/@aws-cdk-testing/framework-integ/test/aws-stepfunctions-tasks/test/ecs/integ.fargate-run-task-capacity-provider.js.snapshot/aws-sfn-tasks-ecs-fargate-run-task-capacity-provider.template.json
iam-no-overly-permissive-passrole.guard❌ failure

@github-actions
Copy link
Contributor

github-actions bot commented Dec 17, 2025

⚠️ Experimental Feature: This security report is currently in experimental phase. Results may include false positives and the rules are being actively refined.
Please try merge from main to avoid findings unrelated to the PR.


TestsPassed ✅SkippedFailed
Security Guardian Results with resolved templates100 ran100 passed
TestResult
No test annotations available

@go-to-k
Copy link
Contributor Author

go-to-k commented Dec 17, 2025

@badmintoncryer Thanks for your review! I'll check and apply your points later!

@go-to-k
Copy link
Contributor Author

go-to-k commented Dec 17, 2025

@badmintoncryer

I have applied the all comments!

Copy link
Contributor

@badmintoncryer badmintoncryer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!!

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Dec 17, 2025
Copy link
Member

@ozelalisen ozelalisen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have some comments, and thanks for raising this PR, this will be really valuable!

}

interface CapacityProviderParams {
launchType?: ecs.LaunchType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The launchType in CapacityProviderParams feels unnecessary here. The launch type is already determined by the task context (Fargate vs EC2) — it's not something users should configure via capacityProviderOptions. The current _bind(launchType) pattern passes it in just to return it back out.

Could we simplify by just checking whether capacityProviderStrategy is provided instead of threading launchType through this abstraction?

/**
* No capacity provider strategy is used.
*/
public static none(): CapacityProviderOptions {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure CapacityProviderOptions.none() adds value here. The absence of the capacityProviderOptions property already means "no capacity provider strategy" — having an explicit .none() method creates two ways to express the same thing (undefined vs .none()), which can be confusing for users.

What's the use case for explicitly calling .none() vs just not setting the property?

* @default - 'FARGATE' LaunchType running tasks on AWS Fargate On-Demand
* infrastructure is used without the capacity provider strategy.
*/
readonly capacityProviderOptions?: CapacityProviderOptions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a simpler API without the abstract class hierarchy would be better, something like:

  capacityProviderStrategy?: ecs.CapacityProviderStrategy[];
  useClusterDefaultCapacityProvider?: boolean;

Then the logic becomes:

  • capacityProviderStrategy provided → use custom strategy (omit LaunchType)
  • useClusterDefaultCapacityProvider: true → omit both (uses cluster default)
  • Neither → use LaunchType (current behavior)

This would remove the need for CapacityProviderOptions, NoCapacityProviderOptions, CustomCapacityProviderOptions, and DefaultCapacityProviderOptions classes entirely, making the API more intuitive and easier to maintain.

* @default - 'EC2' LaunchType running tasks on Amazon EC2 instances registered to
* your cluster is used without the capacity provider strategy.
*/
readonly capacityProviderOptions?: CapacityProviderOptions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above

@ozelalisen ozelalisen self-assigned this Dec 24, 2025
@ozelalisen ozelalisen added p1 and removed p2 labels Dec 24, 2025
@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Dec 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

distinguished-contributor [Pilot] contributed 50+ PRs to the CDK effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. p1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(stepfunctions-tasks): Allow specifying CapacityProviderStrategy in EcsRunTask

4 participants