The information contained in this article has been verified as up-to-date on the date of the original publication of the article. HashiCorp endeavors to keep this information up-to-date and correct, but it makes no representations or warranties of any kind, express or implied, about the ongoing completeness, accuracy, reliability, or suitability of the information provided.
All information contained in this article is for general information purposes only. Any reliance you place on such information as it applies to your use of your HashiCorp product is therefore strictly at your own risk.
Introduction
This article provides instructions for how to avoid the following terraform apply runs to recreate the already-existing Google Cloud Storage (GCS) objects that were created in the previous apply run.
Problem
Users have updated their terraform codes and left the part of codes containing GCS object resources unchanged. When executing a plan run, the plan outputs report that the GCS objects are subject to be recreated. Users may also see the same even when no changes have been made to the codes and submit another planor apply run. The plan outputs always tell that the GCS objects will be recreated because a different hash is detected.
Solution
The way to prevent Terraform from recreating the GCS object is by adding detect_md5hash in the resource block. Here are the steps to generate the md5hash for a GCS object and add it to the resource.
- Run
gsutilto calculate the md5hash value,gsutil hash -m sample_file.txt
The command output will look like this,
Hashes [base64] for sample_file.txt:
Hash (md5): UXeljcZcihTckMads7890g==
Operation completed over 1 objects/7.0 B. - Copy the calculated hash value and add it to the resource block,
resource "google_storage_bucket_object" "default" {
name = "sample_file.txt"
source = "./sample_file.txt"
detect_md5hash = "UXeljcZcihTckMads7890g=="
bucket = google_storage_bucket.static.id
}
Outcome
On the first apply run, the GCS object will be created. The GCS object will not be recreated after submitting another apply run.