Problem
A Terraform plan for google_storage_bucket_object
shows the resource will be replaced but after apply the resource does not exist at all.
Example Output
Plan shows resource replacement:
Terraform will perform the following actions:
# google_storage_bucket_object.example must be replaced
+/- resource "google_storage_bucket_object" "example" {
Apply completes without error:
Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
But subsequent plan shows plan to create, and resource does not exist in Google Cloud Platform:
Terraform will perform the following actions:
# google_storage_bucket_object.example will be created
+ resource "google_storage_bucket_object" "example" {
Cause
This is caused by a combination of create_before_destroy = true
in the Terraform configuration and not changing the name
argument on the google_storage_bucket_object
resource.
Example Output
The plan shows that the name
argument is not changing during the resource replacement operation:
# google_storage_bucket_object.example must be replaced
+/- resource "google_storage_bucket_object" "example" {
...
name = "example_object"
...
~ source = "image.png" -> "image2.png" # forces replacement
The apply shows that the resource is first created, then destroyed:
google_storage_bucket_object.example: Creating...
google_storage_bucket_object.example: Creation complete after 0s [id=hashi_example_bucket-example_object]
google_storage_bucket_object.example (deposed object f3bb18b6): Destroying... [id=hashi_example_bucket-example_object]
google_storage_bucket_object.example: Destruction complete after 0s
Explanation
This is due to a create_before_destroy
in the configuration, either in the google_storage_bucket_object
resource itself:
resource "google_storage_bucket_object" "example" {
name = "example_object"
source = "image2.png"
bucket = google_storage_bucket.example.name
lifecycle {
create_before_destroy = true
}
}
Or a create_before_destroy
in a dependent resource, i.e. a resource that depends on google_storage_bucket_object
implicitly or explicitly:
resource "null_resource" "example" {
lifecycle {
create_before_destroy = true
}
depends_on = [google_storage_bucket_object.example]
}
Per the create_before_destroy
documentation, dependent resources must enforce this behavior on the resources they depend on to avoid dependency cycles.
When used in combination with this resource, this causes an issue because the name
is the identifier string for this resource in the Google Cloud Platform API, so the destroy request will destroy the recently overwritten resource. Other resources with this same API design can also face this issue.
Solution
Always change the name
argument on the google_storage_bucket_object
when using create_before_destroy = true
, or do not use create_before_destroy = true
with this resource or resources that depend on it.