简体   繁体   中英

How to reconcile the Terraform State with an existing bucket?

Using Terraform 11.14 My terraform file contains the following resource:

resource "google_storage_bucket" "assets-bucket" {
  name          = "${local.assets_bucket_name}"
  storage_class = "MULTI_REGIONAL"
  force_destroy = true
}

And this bucket has already been created (it exists on the infrastructure based on a previous apply ) However the state (remote on gcs ) is inconsistent and doesn't seem to include this bucket. As a result, terraform apply fails with the following error:

google_storage_bucket.assets-bucket: googleapi: Error 409: You already own this bucket. Please select another name., conflict

How can I reconcile the state? ( terraform refresh doesn't help)

EDIT

Following @ydaetskcoR's response, I did:

terraform import module.bf-nathan.google_storage_bucket.assets-bucket my-bucket

The output:

module.bf-nathan.google_storage_bucket.assets-bucket: Importing from ID "my-bucket"...
module.bf-nathan.google_storage_bucket.assets-bucket: Import complete! Imported google_storage_bucket (ID: next-assets-bf-nathan-botfront-cloud)
module.bf-nathan.google_storage_bucket.assets-bucket: Refreshing state... (ID: next-assets-bf-nathan-botfront-cloud)

Error: module.bf-nathan.provider.kubernetes: 1:11: unknown variable accessed: var.cluster_ip in:

https://${var.cluster_ip}

The refreshing step doesn't work. I ran the command from the project's root where a terraform.tfvars file exists. I tried adding -var-file=terraform.tfvars but no luck. Any idea?

You need to import it into the existing state file. You can do this with the terraform import command for any resource that supports it.

Thankfully the google_storage_bucket resource does support it :

Storage buckets can be imported using the name or project/name. If the project is not passed to the import command it will be inferred from the provider block or environment variables. If it cannot be inferred it will be queried from the Compute API (this will fail if the API is not enabled).

eg

$ terraform import google_storage_bucket.image-store image-store-bucket $ terraform import google_storage_bucket.image-store tf-test-project/image-store-bucket

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM