Open krowlandson opened 2 years ago
The diff above is showing the difference between what is stored in the state and the remote state of the resource.
In your first apply, due to how Terraoform works, it evaluate the final state to be stored based on the planned new state (see the resource instance change lifecycle for details). In this case, the planned new state is affected by the TF configuration in effect. Specificially, for those list typed properties that is missing in the config, they'll end up with a null
in the state (i.e. in your case the private_ip_ranges
and dns_servers
).
This can be fixed by a follow-up terraform apply -refresh-only
. In this case, terraform
don't care about the configuration, but only refresh what is recorded in the state to match its current remote state. This will honestly refresh everything regardless the TF configuration. So those list typed properties will become either empty list []
or list with elements, but not a null
.
This Objects have changed outside of Terraform
issue exists for lots of resources. E.g. an empty resource group with no tags
defined will also cause this issue complaining that the tags
will be changed from null
to {}
in a refresh. IMO, this is something need to fix in the hashicorp/terraform-plugin-sdk
, rather than the provider side. This might be fixed once hashicorp/terraform-plugin-sdk#261 is addressed, or we've migrated to the terraform-plugin-framework someday.
This can be fixed by a follow-up
terraform apply -refresh-only
. In this case,terraform
don't care about the configuration, but only refresh what is recorded in the state to match its current remote state. This will honestly refresh everything regardless the TF configuration.
So is the fix to always run a "terraform apply -refresh-only" after every "terraform apply" is run? And is this normal/safe?
cf my similar issue here https://github.com/hashicorp/terraform-provider-azurerm/issues/15499
@magodo If there any working solution to this issue with null -> []
changes on apply?
We use lots of Idempotent tests, and they fail, of course, due to planned changes.
Is there any known way it can be solved with configuration or provider code on the resource level?
@dkuzmenok Unfortunately, the only workaround I know is to do a refresh.
Community Note
Terraform (and AzureRM Provider) Version
Affected Resource(s)
azurerm_firewall
Terraform Configuration Files
This is being observed as part of a test deployment located within the
caf-enterprise-scale
module, but can be reproduced outside the module.See the following lines of code where we configure a deployment of the module where we observe this issue: https://github.com/krowlandson/terraform-azurerm-caf-enterprise-scale/blob/89d31d4dddeb5d4bd5c9f948871111b66c9e3aeb/tests/modules/test_003_add_mgmt_conn/main.tf#L117-L146
Debug Output
Panic Output
Expected Behaviour
When creating an
azurerm_firewall
resource using Terraform, the correct state should be stored upon resource creation.Actual Behaviour
When running
terraform plan
orterraform apply
after initial creation of anazurerm_firewall
resource, Terraform reports thatObjects have changed outside of Terraform
:As can be seen from the above, the following attributes appear to be affected:
dns_servers
private_ip_ranges
Attempting to set these values to
[]
instead ofnull
(which is the module default) to ensure the code matches the configuration, results in the following errors:Steps to Reproduce
./tests/modules/test_003_add_mgmt_conn
sub folderterraform.tf
terraform init
terraform apply
and confirm deploymentterraform apply
a second time and observe the changesImportant Factoids
References
none found