How to fix Error: Error creating ECS Cluster: ClusterContainsServicesException in Terraform

TerraformINTERMEDIATEHIGHUpdated Dec 31, 2025ยท 97 views

This error occurs when attempting to delete or recreate an AWS ECS cluster that still has active services. You must delete or deregister all services before the cluster can be deleted.

Quick fix

Fixes Error: Error creating ECS Cluster: ClusterContainsServicesException

aws ecs list-services --cluster <cluster-name> --region <region>
aws ecs describe-services --cluster <cluster-name> --services <service-name> --region <region>
  1. Verify existing services in the cluster
  2. Delete ECS services first
  3. Add explicit depends_on relationships
  4. Update service desired count to zero
terraform โ€” error
aws ecs list-services --cluster <cluster-name> --region <region>
aws ecs describe-services --cluster <cluster-name> --services <service-name> --region <region>
Error: Error creating ECS Cluster: ClusterContainsServicesException

What this error means

AWS ECS prevents deletion of clusters that contain running or active services. When Terraform attempts to destroy or update an ECS cluster that has services deployed, AWS returns the ClusterContainsServicesException error. This is a safety mechanism to prevent accidental data loss and ensures proper resource cleanup order.

Typical symptoms

  • Terraform destroy fails with ClusterContainsServicesException error
  • Cluster update or recreation attempts fail partway through
  • Cluster appears locked or unable to be modified
  • Error mentions 'Services are active' preventing cluster deletion

Common causes

  1. ECS services are still running in the cluster and have not been deleted
  2. Terraform attempted to delete the cluster before deleting associated services due to incorrect dependency ordering
  3. Services are in DRAINING state and have not fully shut down
  4. Container instances or capacity providers are still registered with the cluster
  5. AWS eventual consistency delay - Terraform deleted the service but cluster deletion happened before deletion propagated

How to fix "Error: Error creating ECS Cluster: ClusterContainsServicesException"

1Verify existing services in the cluster

Before attempting deletion, check what services are currently deployed:

bash
aws ecs list-services --cluster <cluster-name> --region <region>
aws ecs describe-services --cluster <cluster-name> --services <service-name> --region <region>

This shows all active services and their status (ACTIVE, DRAINING, INACTIVE).

2Delete ECS services first

Delete the ECS services before attempting to delete the cluster. In Terraform, ensure aws_ecs_service resources are defined and will be destroyed first:

hcl
resource "aws_ecs_service" "example" {
  name            = "example-service"
  cluster         = aws_ecs_cluster.example.id
  task_definition = aws_ecs_task_definition.example.arn
  desired_count   = 1
}

resource "aws_ecs_cluster" "example" {
  name = "example-cluster"
}

Terraform will automatically destroy services before the cluster due to implicit dependencies.

3Add explicit depends_on relationships

If you have complex dependencies, add explicit depends_on to ensure proper destruction order:

hcl
resource "aws_ecs_cluster" "example" {
  name = "example-cluster"

  depends_on = [
    aws_ecs_service.example
  ]
}

This ensures services and their IAM policies are cleaned up before the cluster.

4Update service desired count to zero

If services are stuck in DRAINING state, scale down the desired task count:

bash
aws ecs update-service --cluster <cluster-name> --service <service-name> --desired-count 0 --region <region>

Wait for tasks to fully drain before proceeding with cluster deletion.

5Deregister container instances

If using EC2 launch type, deregister all container instances from the cluster:

bash
aws ecs list-container-instances --cluster <cluster-name> --region <region>
aws ecs deregister-container-instance --cluster <cluster-name> --container-instance <instance-arn> --force --region <region>

Remove all capacity provider associations before deletion.

6Retry Terraform destroy

After all services and instances are removed, retry the Terraform destroy:

bash
terraform destroy

If the destroy still hangs, you may need to wait a few minutes for AWS to fully propagate the service deletions (eventual consistency), then run destroy again.

Advanced notes

This error is caused by AWS eventual consistency - when you delete an ECS service, it takes a moment for that deletion to propagate across all AWS systems. If Terraform attempts to delete the cluster immediately after deleting the service, AWS may not have registered the service deletion yet. Known issue in terraform-provider-aws#4852. For Fargate services, use capacity_providers carefully as they can introduce unexpected dependency chains. If using CloudFormation stacks via Terraform's aws_cloudformation_stack resource, ensure proper resource ordering in your template. Consider using depends_on to explicitly control resource destruction order and avoid race conditions during service teardown.

Frequently asked questions

What does "Error: Error creating ECS Cluster: ClusterContainsServicesException" mean?
AWS ECS prevents deletion of clusters that contain running or active services. When Terraform attempts to destroy or update an ECS cluster that has services deployed, AWS returns the ClusterContainsServicesException error. This is a safety mechanism to prevent accidental data loss and ensures proper resource cleanup order.
How do I fix "Error: Error creating ECS Cluster: ClusterContainsServicesException"?
This error occurs when attempting to delete or recreate an AWS ECS cluster that still has active services. You must delete or deregister all services before the cluster can be deleted.
What causes "Error: Error creating ECS Cluster: ClusterContainsServicesException"?
ECS services are still running in the cluster and have not been deleted Terraform attempted to delete the cluster before deleting associated services due to incorrect dependency ordering Services are in DRAINING state and have not fully shut down
Is the "Error: Error creating ECS Cluster: ClusterContainsServicesException" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading