Closed infradmin closed 2 years ago
A work-around is proposed to add
lifecycle {
create_before_destroy = true
}
to the address resource, and in some cases it works. But is there a solution?
@infradmin,
Providers cannot change the order of operations, nor does Terraform ever change ordering based on the provider. The order of operations is always done exactly as specified in the configuration, based on the relationships between resources. If the ordering you require is that created by create_before_destroy=true
, then that is the configuration you need. The create_before_destroy
option isn't a workaround, is how you declare when resources must be created, updated, and destroyed in the inverse order from the default.
We use GitHub issues for tracking bugs and enhancements, rather than for questions. While we can sometimes help with certain simple problems here, it's better to use the community forum where there are more people ready to help.
Thanks!
@jbardin Thank you, surely you are right
Providers cannot change the order of operations, nor does Terraform ever change ordering based on the provider. The order of operations is always done exactly as specified in the configuration, based on the relationships between resources. If the ordering you require is that created by
create_before_destroy=true
, then that is the configuration you need. Thecreate_before_destroy
option isn't a workaround, is how you declare when resources must be created, updated, and destroyed in the inverse order from the default.
But in my case I expect relationships to be created when I reference address resources creating members of the address group:
resource "fortios_firewall_addrgrp" "addgrp" {
for_each = { for i in local.subnets : i.group => i... }
name = each.key
dynamic "member" {
for_each = each.value
content {
name = fortios_firewall_address.addr["${member.value["name"]}"].name
}
}
}
I think in this case terraform should understand that address group must be changed before any of its members will be destroyed.
I work mostly with azurerm provider and there it works this way. Also it very logical and I believe this is the way terraform creators want us to use.
I think in this case terraform should understand that address group must be changed before any of its members will be destroyed.
This unfortunately is not possible without some extra information. The default ordering is to destroy first, then create or update -- if you need something different from the default that extra information must be added to the configuration. It's possible a future version of terraform may allow providers to communicate some lifecycle options, but that can have major ramifications throughout a configuration as the order of all descendent resources needs to take this into account, many of which may not properly accommodate it. It's definitely something we are going to reevaluate once the time comes, but for now the explicit option in the config makes for a simple and robust solution which is well understood.
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. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Hello.
I got the issue with fortios provider but its developers say that it is terraform bug not a provider's one (link to the message).
Currently using Terraform v1.1.3 and provider registry.terraform.io/fortinetdev/fortios v1.13.2.
Here is my code example (sorry, masking access info):
The problem is order of operations. I have few address objects and one address group object, addresses are members of address group. Creation of these objects goes successfully.
But if I need to remove one of address objects, there is an issue. Terraform plan shows correctly that one item should be changed (address group — remove a member) and one item destroyed (address).
Expected Behavior
The proper order should be: first to change address group and then destroy address.
Actual Behavior
But terraform apply tries to destroy address first, resulting in obvious error because this address object is still used by address group.
Steps to Reproduce
terraform init
terraform apply
terraform apply