Unable to use ECS Ec2Service construct when ECS cluster uses manually added EC2 capacity
See original GitHub issueThe Ec2Service class in the @aws-cdk/aws-ecs module has a check in its validate()
method to verify that the cluster has capacity. While this seems like a reasonable check in many situations it assume that the user is using AWS CDK constructs to create ECS EC2 instances. However, if one where to manually create an ECS instance in a cluster that doesn’t use EC2 capacity this check would prevent the user from using the Ec2Service
construct and there is no way around it (except for the ugly workaround mentioned below).
An example use-case is an ECS cluster that is 100% Fargate (i.e. no EC2 capacity required). Then a need arises when you need a very specific use-case that Fargate does not support and thus require an EC2 ECS instance. Further, the EC2 instance is heavily customized in a way that is not supported from cluster.addCapacity()
and requires the ECS instance to be created outside of addCapacty()
. In this situation, the ECS cluster construct does not know the capacity you’ve manually added.
My recommendation is to remove this hasCapacity
check in Ec2Service as I don’t think it really services a useful purpose. At best it can catch a situation where someone accidentally forgot to add capacity but it cannot actually usefully tell you if you have enough capacity to actually run your service. What if your service requires 4xCPU but you only have enough capacity for 2xCPU. There is a myriad of cases where you can have enough capacity but ECS still won’t be able to run your service (another example would be placement constraints).
Reproduction Steps
- Create an ECS Cluster with a single Fargate service.
- Create an ECS instance manually (not using
addCapacity()
) - Create an ECS service using the
Ec2Service
construct
Error Log
Error: Validation failed with the following errors: [path/to/myservice] Cluster for this service needs Ec2 capacity. Call addXxxCapacity() on the cluster.
Environment
- CLI Version :
- Framework Version:
- OS :
- Language :
Other
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:10 (3 by maintainers)
Top GitHub Comments
I have another use case for bypassing the capacity check of the Ec2Service construct (and by extension the ECS EC2 patterns). We’re using Spot Fleet and Application AutoScaling Policies to handle instance capacity, so we’re not using AutoScaling groups. It doesn’t appear that addCapacity() supports this method of capacity management, so I’m unable to use our cluster with the Ec2Service construct. I get the same error that @cbeattie-tl mentioned in his issue:
Cluster for this service needs Ec2 capacity. Call addXxxCapacity() on the cluster.
I had a similar issue. In my case the EC2 instances were actually created via CDK, but since they are not in an ASG that can be added as capacity, I would get the same error. Found a workaround though: Just created the capacity 🙂
Not beautiful, but it worked for me. CDK passed and the service runs on the registered EC2 instances.