question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(aws_ecs): Add support for `DefaultCapacityProviderStrategy` to the `Cluster` L2 construct

See original GitHub issue

Step Function state language does not support provider parameter for running ECS task https://docs.aws.amazon.com/step-functions/latest/dg/connect-ecs.html. I think default provider is the only option there. To use it conveniently from CDK, there is an open ticket with a described workaround https://github.com/aws/aws-cdk/issues/7967#issuecomment-651583630.

Proposed Solution

The L2 construct a2s_ecs.Cluster currently doesn’t support a default provider, could you please add it? https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ecs/Cluster.html#aws_cdk.aws_ecs.Cluster.add_asg_capacity_provider

I tried to use L1 constructs and ran into several issues. CfnCluster.default_capacity_provider_strategy (via CapacityProviderStrategyItemProperty) and CfnClusterCapacityProviderAssociations expect provider as a string instead of specific type. This is probably not a problem of CDK as the AWS::ECS::ClusterCapacityProviderAssociations in CloudFormation docs have a similar problem. Examples in the docs are fine. Could you please forward the problem to the correct place? https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ecs.CfnCluster.CapacityProviderStrategyItemProperty.html https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ecs.CfnClusterCapacityProviderAssociations.html https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-clustercapacityproviderassociations.html

I tried to create the association using a generic CfnResource, i came up with this:

ecs_cluster = ecs.Cluster(...)
provider = ecs.AsgCapacityProvider(...)

association = CfnResource(self, "capacity_association2",
                type="AWS::ECS::ClusterCapacityProviderAssociations",
                properties= {
                    "CapacityProviders": [
                        provider.node.children[0].ref
                ],
                    "Cluster": ecs_cluster.node.default_child.ref,
                    "DefaultCapacityProviderStrategy": [
                        {
                            "CapacityProvider": provider.node.children[0].ref
                        }
                    ]
                }
            )

In Python CDK, ecs.AsgCapacityProvider did not have a default_child, so to access its reference, I had to use provider.node.children[0].ref

Environment

  • CDK CLI Version : 1.108.1
  • **Framework Version: ** 1.108.1
  • Node.js Version: v10.19.0
  • OS : Ubuntu
  • Language (Version): 3.8.5

Other

To run an ECS task from step function using a custom provider from CDK, there is currently an open ticket with a described workaround https://github.com/aws/aws-cdk/issues/7967#issuecomment-651583630.


This is a 🚀 Feature Request

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:13
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
piotrromanowskicommented, Aug 4, 2022

Was able to get around this issue using Aspects

class SetDefaultCapacityProviderStrategy implements cdk.IAspect {
  constructor(
    private readonly defaultCapacityProvider: ecs.AsgCapacityProvider
  ) {}

  visit(node: IConstruct): void {
    if (node instanceof ecs.CfnClusterCapacityProviderAssociations) {
      node.defaultCapacityProviderStrategy = [
        {
          capacityProvider: this.defaultCapacityProvider.capacityProviderName,
        },
      ];
    }
  }
}

And then in the construct that’s creating the cluster

    cdk.Aspects.of(this).add(
      new SetDefaultCapacityProviderStrategy(defaultCapacityProvider)
    );
    ```
0reactions
piotrromanowskicommented, Aug 11, 2022

I’ve got a branch that I’ll create a PR for once I’ve added tests as suggested by docs https://github.com/aws/aws-cdk/compare/main...piotrromanowski:aws-cdk:Add_cluster_service_capacity_provider_support

Edit: FWIW we’ve been using this in production for a while now and it’s been working out well

Read more comments on GitHub >

github_iconTop Results From Across the Web

class CfnClusterCapacityProviderAssociations (construct)
The AWS::ECS::ClusterCapacityProviderAssociations resource associates one or more capacity providers and a default capacity provider strategy with a cluster ...
Read more >
Using CDK to define ECS Fargate cluster with Service ...
ECS Fargate with service discovery but without Load Balancing ... can also create a L2 FargateService construct using the aws-ecs library.
Read more >
aws ecs put-cluster-capacity-providers - Fig
When creating a service or running a task on a cluster, if no capacity ... It is recommended to define a default capacity...
Read more >
@aws-cdk/aws-ecs | Yarn - Package Manager
The CDK Construct Library for AWS::ECS ... Amazon ECS Construct Library ... package contains constructs for working with Amazon Elastic Container Service ......
Read more >
put-cluster-capacity-providers - Amazon AWS
When creating a service or running a task on a cluster, if no capacity provider or launch type is specified then the default...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found