-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ResourceInUse: Service contains registered instances; delete the instances before deleting the service #4853
Comments
Still an issue, in my case it's a bit of a pain to add depends_on as this is abstracted away in a module... so |
This does seem to be a nasty bug, because terraform should be able to handle deleting resources in the correct order, but it doesn't seem to be in this case. I'm also having this issue with terraform destroy and aws_service_discovery. Currently manual deletion of AWS resources is required when this error happens. |
+1 |
any workaround to this issue? |
I'm having the same issue pradeepbhadani cited. After a "terraform destroy" on an ECS fargate environment, I end up with orphaned DNS records in the service discovery namespace that cannot be deleted manually as they are managed by the service discovery service. Then, because those records are still there, the service discovery namespace cannot be deleted. |
This issue is happening to me while running:
This is requiring AWS Support personnel to go in and delete the orphaned DNS records manually before the Service Discovery namespace can be deleted using AWS CLI. |
I've run into this again in another scenario where the namespace wasn't being deleted. A service was being destroyed as part of updating it. I have ended up with an orphaned service discovery operation. An instance was attempted to be registered, but, the underlying ECS service was already destroyed. I'm again left with requiring AWS Support to go in and fix things behind the scenes. |
I'm experiencing this, though with custom service instances (not ECS). Some kind of force_delete attribute on the service might help so that terraform can cycle through and deregister any instances left in the service before attempting to delete the service. |
I was able to resolve this by running
then selecting my service's ID from the list and running.
|
In my case there was a modification in service discovery resource and terraform was unable to destroy the old resource. So I have to do it manually.
Solution: Based on the service id, first i have to find the attached instance-id,
Once i have attached instance-id, i have to deregister it before i delete the service.
It looks like terraform needs to fix this bug :) |
This is true if the associated The issue also occurs more fundamentally when Terraform needs to remove or replace just the
Until the Terraform AWS provider removes existing service discovery instance records, our options seem limited to manual removal or a destroy time The latter really doesn't sit well with me as it introduces risk, dependencies on the host machine running Terraform having AWS CLI and appropriate privileges - however in a heavily automated CI/CD environment it's perhaps a better interim workaround than random failures and manual intervention. resource "aws_service_discovery_service" "core" {
[..]
/**
* Workaround to https://github.com/terraform-providers/terraform-provider-aws/issues/4853
* Terraform does not deregister existing service discovery instance records prior to removing
* the `aws_service_discovery_service` resource, causing AWS to error with:
* ResourceInUse: Service contains registered instances; delete the instances before deleting the service
*/
provisioner "local-exec" {
when = "destroy"
command = <<EOF_COMMAND
SERVICE_ID=$(aws servicediscovery list-services --filters '[{"Name":"NAMESPACE_ID","Values":["${var.service_discovery_namespace_id}"]}]' --region ${var.aws_region} \
--query 'Services[?Name == `${var.service_discovery_name}`].Id' --output text) && \
aws servicediscovery discover-instances --namespace-name ${var.service_discovery_domain} --service-name ${var.service_discovery_name} \
--query 'Instances[*].InstanceId | join(`"\n"`, @)' --output text \
| xargs -I {INSTANCE_ID} aws servicediscovery deregister-instance --service-id $SERVICE_ID --instance-id {INSTANCE_ID} && sleep 5
EOF_COMMAND
}
} |
In my scenario, I have a direct dependency between the ECS service and service-discovery-service (the service references the service discovery service ARN). In my case, a seemingly simple change to the DNS TTL value in the service discovery caused me to encounter this problem. |
Same happens here. |
We are hitting the same scenario on services which are already running in production. |
+1 |
This is a bad bug as it basically makes the provider feature incomplete and broken. IMO the original feature should have never been released if it doesn't handle this scenario. |
+1 |
3 similar comments
+1 |
+1 |
+1 |
just stop the task first before delete the service |
that fixed it for me |
+1 |
Is there a workaround that actually works around? |
This has been working great for me: Add to # Remove after https://github.com/terraform-providers/terraform-provider-aws/issues/4853 is resolved
provisioner "local-exec" {
when = destroy
command = "${path.module}/servicediscovery-drain.sh ${self.id}"
}
#!/bin/bash
[ $# -ne 1 ] && echo "Usage: $0 <service-id>" && exit 1
serviceId="--service-id=$1"
echo "Draining servicediscovery instances from $1 ..."
ids="$(aws servicediscovery list-instances $serviceId --query 'Instances[].Id' --output text | tr '\t' ' ')"
found=
for id in $ids; do
if [ -n "$id" ]; then
echo "Deregistering $1 / $id ..."
aws servicediscovery deregister-instance $serviceId --instance-id "$id"
found=1
fi
done
# Yes, I'm being lazy here...
[ -n "$found" ] && sleep 5 || true |
Having the same issue right now. |
The same problem :( |
Any progress on this one. Hitting it with TF 0.14.4 and AWS Provider 3.23.0 |
Experiencing the same issue on latest TF and AWS:
|
I'm struggling with this while attempting @japgolly solution using a local-exec provisioner and calling a shell script to deregister the service.
I want to pass in TF vars of profile and region to the local-exec provisioner - however tf has restricted this whilst when=destroy. Any way around this? |
This functionality has been released in v3.57.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you! |
Experienced this issue during terraform destroy of ECS services running on EC2 instances.
|
Experienced the same issue : Terraform v0.15.3
|
Ran into it today using TF v1.1.7 |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
This issue was originally opened by @tuaris as hashicorp/terraform#18264. It was migrated here as a result of the provider split. The original body of the issue is below.
Terraform Version
Terraform Configuration Files
Debug Output
Expected Behavior
Removing the resource
aws_service_discovery_service.worker
should first stop the serviceaws_ecs_service.worker
, then proceed to delete the resource.Actual Behavior
Process fails with
ResourceInUse: Service contains registered instances; delete the instances before deleting the service
Steps to Reproduce
To reproduce the issue, for example:
terraform apply
aws_service_discovery_service.worker
terraform apply
The text was updated successfully, but these errors were encountered: