- Newest
- Most votes
- Most comments
In order to use EC2 Capacity Provider with a Task or Service, you must specify a Capacity Provider Strategy when creating the service or running the task. This strategy is used instead of a Launch Type. The Capacity Provider Strategy must include the EC2 Capacity Provider along with a weight of at least 1.
For example:
{
"weight": 1,
"capacityProvider": "myEC2CapacityProvider",
"base": 0
}
You can also configure this in the ECS Console when launching a Service.
Per answer from @Michael_F, for this to work, you must ensure that the Capacity Provider Strategy is attached to the ECS Service. In my case, building this with a CloudFormation stack, it was not, which is interesting so posting the solution here.
TL;DR: to go from not working to working, here's the CF template diff:
EcsService:
Type: AWS::ECS::Service
Properties:
Cluster: !Ref EcsCluster
DesiredCount: !FindInMap [ ServerState, !Ref ServerState, DesiredCapacity ]
ServiceName: !Sub "${AWS::StackName}-ecs-service"
TaskDefinition: !Ref EcsTask
+ CapacityProviderStrategy:
+ - CapacityProvider: !Ref ECSCapacityProvider
+ Weight: 1
+ Base: 0
DeploymentConfiguration:
MaximumPercent: 100
MinimumHealthyPercent: 0
Worth noting that ECSCapacityProvider
referenced here is already defined - it is also already associated with the ECS Cluster with an association as the default strategy provider:
EcsCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: !Sub "${AWS::StackName}-cluster"
ECSCapacityProvider:
Type: AWS::ECS::CapacityProvider
Properties:
AutoScalingGroupProvider:
AutoScalingGroupArn: !Ref AutoScalingGroup
ManagedScaling:
MaximumScalingStepSize: 1
MinimumScalingStepSize: 1
Status: ENABLED
TargetCapacity: 100
ManagedTerminationProtection: ENABLED
EcsClusterCapacityProviderAssociation:
Type: AWS::ECS::ClusterCapacityProviderAssociations
Properties:
Cluster: !Ref EcsCluster
CapacityProviders:
- !Ref ECSCapacityProvider
DefaultCapacityProviderStrategy:
- CapacityProvider: !Ref ECSCapacityProvider
Weight: 1
Now I thought this should be enough - the Capacity Provider and Strategy are registered with the same ECS Cluster where my ECS Service is defined, and according to https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-capacity-providers.html and other docs, if you attach a default capacity provider strategy to a cluster, then create an ECS Service without one, it should adopt the default.
This did not happen for me in my CloudFormation template. The only thing I can image happened is that without an explicit DependsOn
, my ECS Service is being defined asynchronously, or in-parallel to the capacity provider strategy on the cluster, so the ECS Service is being created before the capacity provider strategy is built and assigned to the cluster. The ECS Service therefore picks up a launch strategy. Can't really prove this yet.
So the solution would be to either add a DependsOn
, or what I did above and explicitly assign the (already cluster default) capacity assignment strategy to the ECS Service.
With this, I can now use a Desired Task Count 1 -> 0 -> 1 to drive the ASG instance count to 1 -> (15 minutes) 0 -> (minute or two) 1.
Relevant content
- asked a month ago
- Accepted Answerasked 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
Great answer @Michael_F - I did get this to work via a CloudFormation and I can guess at the root cause when it wasn't working, so I will add an answer with those details.