hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.29k stars 1.72k forks source link

google_artifact_registry_repository error when using merge in labels #17901

Open blancoobelver opened 5 months ago

blancoobelver commented 5 months ago

Community Note

Terraform Version

Terraform v1.2.9 on windows_amd64

Affected Resource(s)

google_artifact_registry_repository

Terraform Configuration


terraform {
  required_version = ">= 0.15"
  required_providers {
    google-beta = {
      source  = "hashicorp/google-beta"
      version = "5.18.0"
    }
  }
  experiments = [module_variable_optional_attrs]
}

resource "google_artifact_registry_repository" "my-repo" {
  count         =  1
  provider      = google-beta
  location      = "europe-west4"
  repository_id = "repo-test"
  description   = "example docker repository"
  format        = "DOCKER"
  project       = "example-project"
  lifecycle {
    ignore_changes = [
      labels["status"],
      labels["on_service"],
    ]
  }
  labels = merge(

    {
                    account                 = "example",
                    enterprise              = "example",
                    environment             = "example",
                    region                  = "example",
                    security_exposure_level = "example",
                    status                  = "example",
                    app                     = "example",
                    appdt9                  = "example",
                    system                  = "example",
                    provider                = "example",
                    on_service              = "example",
                    cmdb_name               = ""
                  },
    {
      "resource_name" = format(
        "%s-%s-repository%.2d-%s",
        "example",
        "example",
        1,
        "example"
      ),
      "resource_type"   = "repository",
      "deployment_date" = formatdate("DD-MM-YYYY", timestamp()),
    }
  )

}

Debug Output

https://gist.github.com/blancoobelver/0c94151dcb7f2dca887bee977895da21

Expected Behavior

Is not supposed to give an error while applying for the second time terraform apply

Actual Behavior

It´s giving an error while executing the terraform apply for the second time

Steps to reproduce

  1. terraform init
  2. terraform apply
  3. terraform apply

Important Factoids

I think the error arises because I've utilized the merge function within labels. Apart from merge, I don't see any other alternatives, given that we have both calculated and predefined tags, necessitating the use of merge. However, in Google version 4.x, this issue didn't arise.

References

No response

b/336375662

ggtisc commented 5 months ago

Confirmed issue with the terraform version and providers configuration after a 2nd terraform apply without changes in the code

alfonsozamorac commented 5 months ago

Hi,

The same error has also been detected in at least the following modules:

Forcing the ignore to be added to the lifecycle and obtaining a warning from the provider.

ignore_changes = [
      terraform_labels,
      effective_labels,
    ]

Warning: Adding an attribute name to ignore_changes tells Terraform to ignore future changes to the argument in configuration after the object has been created, retaining the value originally configured. The attribute terraform_labels is decided by the provider alone and therefore there can be no configured value to compare with. Including this attribute in ignore_changes has no effect. Remove the attribute from ignore_changes to quiet this warning.

Best regards.

Subserial commented 3 months ago

Testing various plans revealed that timestamp() is the culprit here. timestamp() changes value every second, causing the plan step and the apply step to produce inconsistent values. The Terraform team recommends against using this function to determine attributes in both the documentation and other discussions.

I have found during testing that replacing timestamp() with plantimestamp() produces a usable time such that formatdate("DD-MM-YYYY", plantimestamp()) will produce a new value each day.

alfonsozamorac commented 3 months ago

@Subserial thank you very much for the reply. Finally, we have updated the provider from 5.18.0 to 5.32.0, and we no longer have the problem with terraform_labels or effective_labels.