I get an error when I use the AWS Command Line Interface (AWS CLI) or API to delete a capacity provider. The capacity provider is for my Amazon Elastic Container Service (Amazon ECS) cluster.
Short description
If you use either the AWS CLI delete-capacity-provider command or DeleteCapacityProvider API to delete a capacity provider, then you might receive the following error:
"updateStatus": "DELETE_FAILED"
"updateStatusReason": "The capacity provider cannot be deleted because it is associated with cluster: <Cluster Name>. Remove the capacity provider from the cluster and try again."
When you use the AWS Management Console to delete a capacity provider that's in use by an Amazon ECS service, you get this error message:
"There was an error deleting capacity provider <Capacity Provider Name>. The specified capacity provider is in use and cannot be removed"
You might receive these errors for the following reasons:
- An Amazon ECS service is using the capacity provider that you want to delete.
- The default capacity provider strategy is using the capacity provider that you want to delete. You can delete only the capacity providers that aren't associated with a cluster.
Note: If you receive errors when you run AWS CLI commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.
Resolution
Prerequisites:
- Set the cluster and capacity provider parameters to your values.
- Set your AWS CLI credentials to your AWS Region.
- Install jq from the jq website.
Check if your capacity provider is in use
To check if your capacity provider is in use by an Amazon ECS service, complete the following steps:
- To verify what services use the capacity provider, run the following script:
#!/bin/bash
cluster=<Cluster Name>
capacityprovider=<Capacity Provider Name>
services=$(aws ecs list-services --cluster ${cluster} | jq --raw-output '.serviceArns[]')
aws ecs describe-services \
--cluster ${cluster} \
--services ${services} \
| jq -r --arg capacityprovider "${capacityprovider}" \
'.services[] | select(.capacityProviderStrategy[]?.capacityProvider == $capacityprovider) | .serviceName'
Note: If the script returns a blank output, then none of the services in the cluster are using the capacity provider. Skip to the Check if your capacity provider is set in the default capacity provider strategy for the cluster section.
- Before you can delete the old capacity provider, update the ECS service to use a new capacity provider. To remove a capacity provider from a service's capacity provider strategy, use the UpdateService API or the update service workflow in the Amazon ECS console. When you update a service, use forceNewDeployment to transition tasks that use Amazon EC2 instance capacity from the old provider to the new provider.
Important: You can't use a capacity provider to update a service to use a launch type. You must update the service with a different capacity provider.
- Delete the old capacity provider.
Check if your capacity provider is set in the default capacity provider strategy for the cluster
To check if your capacity provider is set in the default capacity provider strategy for the cluster, complete the following steps:
-
To find the default capacity provider for your cluster, run the following command:
$ aws ecs describe-clusters --cluster mycluster | jq '.clusters[].defaultCapacityProviderStrategy'
[
{
"capacityProvider": "oldCP",
"weight": 0,
"base": 0
}
]
-
Modify the default capacity provider strategy for your cluster.
You can use the Amazon ECS console or the AWS CLI to modify the default capacity provider strategy for your cluster.
Amazon ECS console
- Open the Amazon ECS console.
- In the navigation pane, choose Clusters, and then choose your cluster.
- Choose Update Cluster
- In the Cluster Configuration section, remove the old capacity provider from Default capacity provider strategy.
- Choose Update.
AWS CLI
-
To remove all capacity providers from a cluster, run the following command:
aws ecs put-cluster-capacity-providers \
--cluster <Cluster Name> \
--capacity-providers [] \
--default-capacity-provider-strategy []
-
To replace the old capacity provider with a new capacity provider, run the following command before you delete the old capacity provider:
$ aws ecs put-cluster-capacity-providers \
--cluster <Cluster Name> \
--capacity-providers <New Capacity Provider> \
--default-capacity-provider-strategy capacityProvider=<New Capacity Provider> \
--region <Region>
$ aws ecs delete-capacity-provider --capacity-provider <Old Capacity Provider ARN>
$ aws ecs describe-capacity-providers --capacity-provider <Old Capacity Provider ARN>
Note: Any existing capacity providers associated with a cluster that are omitted from the PutClusterCapacityProviders API call are disassociated from the cluster. The same rules apply to the cluster's default capacity provider strategy.