Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(aws-ecs-patterns): Enable autoscaling with ApplicationLoadBalancedEc2Service #18868

Closed
5nafu opened this issue Feb 8, 2022 · 6 comments · Fixed by #20879
Closed

(aws-ecs-patterns): Enable autoscaling with ApplicationLoadBalancedEc2Service #18868

5nafu opened this issue Feb 8, 2022 · 6 comments · Fixed by #20879
Labels
effort/small Small work item – less than a day of effort feature/enhancement A new API to make things easier or more intuitive. A catch-all for general feature requests. feature-request A feature should be added or improved. p2

Comments

@5nafu
Copy link

5nafu commented Feb 8, 2022

General Issue

I would like to use an ApplicationLoadBalancedEc2Service together with autoscaling of EC2 instances.

The Question

I would like to use an ApplicationLoadBalancedEc2Service together with an EC2 ASG. To enable autoscaling of the ECS instances, I would need to apply the capacity_provider_strategies parameter.
The L2 @aws-cdk/aws-ecs.Ec2Service already supports this option, but the L3 construct does not.

Unfortunately the EC2Service does not allow to add this strategy later to the finished service either.

CDK CLI Version

2.10.0

Framework Version

No response

Node.js Version

16.3.0

OS

OSX

Language

Typescript

Language Version

No response

Other information

No response

@5nafu 5nafu added guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels Feb 8, 2022
@peterwoodworth
Copy link
Contributor

peterwoodworth commented Feb 8, 2022

Hey @5nafu,

While you can't directly set this property on the L3 construct, you can use escape hatches to apply this property to the underlying service.

    const l3 = new ApplicationLoadBalancedFargateService(this, 'ALBFargateService', {
      memoryLimitMiB: 1024,
      taskImageOptions: {
        image: ContainerImage.fromRegistry('test'),
        environment: {
          TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
          TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value",
        },
      },
      desiredCount: 2,
    });

    const l1 = l3.service.node.defaultChild as CfnService;

    const cpsprops = {
      base: 2,
      weight: 2,
      capacityProvider: 'capacityProvider'
    }

    l1.addPropertyOverride('CapacityProviderStrategy', cpsprops);

@peterwoodworth peterwoodworth added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Feb 8, 2022
@5nafu
Copy link
Author

5nafu commented Feb 8, 2022

Hi @peterwoodworth ,

thanks for the quick response. Unfortunately the above code results in a Specifying both a launch type and capacity provider strategy is not supported. (Adding the required []around cpsprops on the last line).

I needed to add l1.addPropertyDeletionOverride('LaunchType') to make it work.

So the complete code would be (adding as reference for future google searches):

    const l3 = new ApplicationLoadBalancedEc2Service(this, 'ALBFargateService', {
      memoryLimitMiB: 1024,
      taskImageOptions: {
        image: ContainerImage.fromRegistry('test'),
        environment: {
          TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
          TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value",
        },
      },
      desiredCount: 2,
    });

    const l1 = l3.service.node.defaultChild as CfnService;

    const cpsprops = [{
      base: 2,
      weight: 2,
      capacityProvider: 'capacityProvider'
    }];

    l1.addPropertyOveride('CapacityProviderStrategy', cpsprops);
    l1.addPropertyDeletionOverride('LaunchType');

That said, I would like to request the addition of the capacity_provider_strategies parameter to the EC2-LB pattern ApplicationLoadBalancedEc2Service as using autoscaling groups with an EC2 based ECS cluster seems to be the standard use case, and instances-autoscaling does not work without this setting (at least in our tests).

Cheers and thanks again for the hint with the escape hatches.

@peterwoodworth
Copy link
Contributor

Thanks for cleaning the code up, I only synthesized my code 🙂

I'll convert this to a feature request. Since this has a relatively easy workaround we don't have a timeline for it, but we happily accept community contributions. Check out our contributing guide if you're interested

@peterwoodworth peterwoodworth added effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. feature/enhancement A new API to make things easier or more intuitive. A catch-all for general feature requests. p2 and removed guidance Question that needs advice or information. labels Feb 8, 2022
@peterwoodworth peterwoodworth removed their assignment Feb 8, 2022
@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 9, 2022
@gshpychka
Copy link
Contributor

If I may, I would suggest that if you're running into issues like this, it's time to switch from L3 to L2 constructs. L3 are very opinionated and limit the available options by design, to make it easy for newcomers to spin up complex solutions. Once you're comfortable with it and are in need of deeper configuration, I'd go with L2 to have full control.

@peterwoodworth peterwoodworth changed the title (aws-ecs-patterns): Add capacity_provider_strategies to ApplicationLoadBalancedEc2Service (aws-ecs-patterns): Enable autoscaling with ApplicationLoadBalancedEc2Service Feb 9, 2022
@peterwoodworth
Copy link
Contributor

peterwoodworth commented Feb 9, 2022

@gshpychka thank you for the input, I tend to agree here, and directly implementing the prop just for the L2 construct wouldn't help with the abstraction the L3 is trying to offer. However, I believe there's a case for somehow abstracting the ability to autoscale the instances created 🙂

@mergify mergify bot closed this as completed in #20879 Jul 18, 2022
mergify bot pushed a commit that referenced this issue Jul 18, 2022
…ion/Network)LoadBalanced(Ec2/Fargate)Service (#20879)

Add a property `capacityProviderStrategies` to the four constructs below.

- ApplicationLoadBalancedEc2Service
- NetworkLoadBalancedEc2Service
- ApplicationLoadBalancedFargateService
- NetworkLoadBalancedFargateService

closes #18868

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

comcalvi pushed a commit to comcalvi/aws-cdk that referenced this issue Jul 25, 2022
…ion/Network)LoadBalanced(Ec2/Fargate)Service (aws#20879)

Add a property `capacityProviderStrategies` to the four constructs below.

- ApplicationLoadBalancedEc2Service
- NetworkLoadBalancedEc2Service
- ApplicationLoadBalancedFargateService
- NetworkLoadBalancedFargateService

closes aws#18868

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort/small Small work item – less than a day of effort feature/enhancement A new API to make things easier or more intuitive. A catch-all for general feature requests. feature-request A feature should be added or improved. p2
Projects
None yet
5 participants