terraform-google-modules / terraform-google-slo

Creates SLOs on Google Cloud from custom Stackdriver metrics capability to export SLOs to Google Cloud services and other systems
https://registry.terraform.io/modules/terraform-google-modules/slo/google
Apache License 2.0
63 stars 29 forks source link

Terraform wants to change resources when "local files" are recreated #57

Closed svenmueller closed 3 years ago

svenmueller commented 3 years ago

Terraform wants to change resources when "local files" (generated from templates in /templates) are recreated (e.g. when TF is run on another environment which has no local files).

How to reproduce:

-> TF will show changes for e.g. for resources like local_file (create), google_cloudfunctions_function (updated in-place), google_storage_bucket_object (replaced).

Not sure yet why, but seems like a new archive file is created (different name) event though the file content didn't change.

(Could be that the MD5 checksum is different and therefore the archive file name changes: https://github.com/terraform-google-modules/terraform-google-slo/blob/master/modules/slo/main.tf#L70)

svenmueller commented 3 years ago

Possible reason might be the generated unique random resource ID for local_file resources which cause a different MD5 checksum when executing filemd5(file) (https://github.com/terraform-google-modules/terraform-google-slo/blob/master/modules/slo/main.tf#L53).

  + resource "local_file" "error_budget_policy" {
      + content              = jsonencode(
            [
            ...
            ]
        )
         ...
      + id                   = (known after apply)
    }
svenmueller commented 3 years ago

I noticed that there is already an issue related to it: https://github.com/hashicorp/terraform-provider-random/issues/95

ocervell commented 3 years ago

@svenmueller yeah, unfortunately this has been long-running issue with archive_file and local_file resources. The fix is to not create local files, and use source.content instead of the local filepath.

There is a PR that will fix this, but it's a breaking change on the module, requiring to pass config file paths instead of file contents. Feel free to review it as it might help fix your bug.

ocervell commented 3 years ago

Fixed by #63