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.

Unable to use ECS Ec2Service construct when ECS cluster uses manually added EC2 capacity

See original GitHub issue

The 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

  1. Create an ECS Cluster with a single Fargate service.
  2. Create an ECS instance manually (not using addCapacity())
  3. 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:open
  • Created 4 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
akuma12commented, Sep 22, 2020

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.

1reaction
RodrigoAS28commented, Apr 20, 2022

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 🙂

  const cluster = new ecs.Cluster(this, 'cluster', {
    clusterName: 'cluster',
    vpc: vpc,
    containerInsights: true,
    capacity: {
      autoScalingGroupName: "dummy",
      instanceType: new ec2.InstanceType("t2.nano"),
      maxCapacity: 0,
      minCapacity: 0,
    }
  })

Not beautiful, but it worked for me. CDK passed and the service runs on the registered EC2 instances.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot why your ECS or EC2 instance can't join the ...
I am unable to register my Amazon Elastic Compute Cloud (Amazon EC2) instance with an Amazon Elastic Container Service (Amazon ECS) Cluster.
Read more >
ECS EC2 service with capacity provider binpack doesnt ...
I know I could go to ECS Cluster, then in the ECS instances tab select the extra EC2 instances and drain them. However,...
Read more >
@aws-cdk/aws-ecs - npm
The following example creates an Amazon ECS cluster, adds capacity to it, and runs a service on it: declare const vpc: ec2.Vpc; //...
Read more >
Deploy an EC2 Spot Capacity Provider
Enable EC2 Spot capacity on the cluster. Navigate back to the repo where we create and manage the platform. cd ~/environment/ecsdemo-platform/cdk.
Read more >
aws/lambda_ecs/node_modules/@aws-cdk/aws-ecs ... - Rdrr.io
import * as ecs from '@aws-cdk/aws-ecs'; // Create an ECS cluster const cluster = new ecs.Cluster(this, 'Cluster', { vpc, }); // Add capacity...
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