【问题标题】:How to reconcile the Terraform State with an existing bucket?如何协调 Terraform 状态与现有存储桶?
【发布时间】:2020-05-09 23:12:13
【问题描述】:

使用 Terraform 11.14 我的 terraform 文件包含以下资源:

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

并且这个bucket已经被创建(它存在于基于之前apply的基础设施上) 但是状态(远程gcs)不一致,似乎不包括这个桶。 结果,terraform apply 失败并出现以下错误:

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

如何调和状态? (terraform refresh 没有帮助)

编辑

在@ydaetskcoR 的回复之后,我做了:

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

输出:

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}

刷新步骤不起作用。我从存在terraform.tfvars 文件的项目根目录运行命令。 我尝试添加-var-file=terraform.tfvars,但没有运气。有什么想法吗?

【问题讨论】:

    标签: google-cloud-storage terraform terraform-provider-gcp


    【解决方案1】:

    您需要将其导入现有的状态文件。对于支持它的任何资源,您都可以使用 terraform import command 执行此操作。

    谢天谢地google_storage_bucket resource does support it

    可以使用名称或项目/名称导入存储桶。如果项目没有传递给导入命令,它将从提供程序块或环境变量中推断出来。如果无法推断,将从 Compute API 中查询(如果未启用 API,则会失败)。

    例如

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

    【讨论】:

    • 非常感谢。它只工作了一半(见我的编辑)。您知道是什么原因造成的吗?
    • 这是一个完全不同的问题,需要一个单独的问题,该问题还应包括重现该错误的代码。
    猜你喜欢
    • 2019-07-18
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2019-04-29
    • 2019-05-02
    • 2017-01-05
    • 2021-12-07
    • 1970-01-01
    相关资源
    最近更新 更多