Open JulianCBC opened 5 years ago
Same issue with security groups.
If you have a security group which is used by some other resources, then taint it, terraform apply
waits forever for the security group to be deleted.
If you attempt to delete it from the AWS management console, it prompts you to remove it from the network interfaces which are using it first.
Same issue with ACM certificates.
If you have an ACM certificate used by a load balancer, then you cannot re-create the certificate as it's used by that load balancer and the AWS provider isn't smart enough to determine a work-around.
Is there any progress on this topic?
Hey @JulianCBC 👋 Thank you for taking the time to file this issue! Given that there's been a number of AWS provider releases since you initially filed it, can you confirm whether you're still experiencing this behavior?
@justinretzolk our Terraform configuration has had "hacks" added to it to work around this issue, so it's not causing us problems anymore, however those hacks are inelegant and not easy to discover for novice users, so the issue is still relevant.
I just removed one of those hacks and checked this behaviour on version 3.69 of the AWS provider and the issue is still present.
The relevant part of our configuration looks like this:
resource "aws_security_group" "test" {
name_prefix = "test"
vpc_id = aws_vpc.test.id
lifecycle {
create_before_destroy = true
}
}
resource "aws_instance" "test" {
instance_type = "t2.micro"
ami = data.aws_ami.crm_ubuntu.id
key_name = "test"
vpc_security_group_ids = [
aws_security_group.test.id,
]
subnet_id = aws_subnet.test_a.id
}
And I tested this by:
terraform apply
to clear out any pending changes and ensure that the resources are present in AWScreate_before_destroy
ruleterraform apply
It got stuck destroying the security group as AWS won't let you do that while the EC2 instance references it.@JulianCBC Care to share your "hacks" here? A very similar issue also plagues the vcd provider.
@JouHouFin literally just the create_before_destroy
lifecycle rule, it's easy to work around, but non-obvious and requires knowledge of which things could potentially have these sorts of reverse dependencies. Arguably the provider should hold that knowledge as these "rules" are static properties of the service being used, not something that is context- or configuration-dependent.
Documentation is here: https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle
Oh, and this is why it was "hacks" in quotation marks: I view these lifecycle rules as a way to work around stuff that is weird or complicated, not fundamental properties of a service - a "proper" configuration shouldn't need them, hence this bug.
Thanks, I'll check if that's something I could use. Although I already tried that exact lifecycle rule, but maybe my understanding of it was limited so maybe worth revisiting it. 👍
AWS generally doesn't let people remove resources if they're referenced by another resource by ARN or ID.
For example, a load balancer listener rule might reference a target group. This means that the target group isn't removable until that listener rule no-longer references it.
The AWS provider doesn't seem to understand these reverse dependencies, causing errors when otherwise easily resolvable changes take place, requiring the operator to manually change things in the AWS Management Console before applying the changes. (Or hack the configuration)
Community Note
Terraform Version
Terraform v0.12.8
Affected Resource(s)
This seems to be an issue across many different resources, I've hit it most while working with load balancers, so the affected load balancer resources are listed here.
Terraform Configuration Files
Example configuration: (Modified from a real configuration,
var.*
values will need to be filled out before applying.)Expected Behavior
Terraform creates a plan which changes the
aws_lb_target_group
andaws_lb_listener_rule
resources in an order that prevents it from removing theaws_lb_target_group
while it's referenced by theaws_lb_listener_rule
.E.g.
Or alternatively:
(We can't edit the name of a target group, so we can't create the new one and then edit it to have the correct name after everything's been updated.)
Actual Behavior
Terraform attempts to perform these operations in this order:
This fails at step 1 as the target group cannot be removed while the listener rule references it.
Steps to Reproduce
aws_lb_target_group
resource so that it will be re-created. E.g. changingtarget_type
.Notes
This particular scenario could be hit in the wild by changing a load-balanced server from a EC2 instance to a container - doing this would require changing the target group it's part of from a "instance" type to an "ip" type.
The AWS provider or Terraform itself needs to be made smarter so that it understands when referenced data can be left "dangling" and when it can't.