有没有办法跟踪状态文件的放置/引用位置?

时间:2019-05-07 19:13:27

标签: terraform terraform-provider-gcp

我在GCP上使用TF 0.11.11。我有backend.tfvars文件设置,可以按状态文件存储在GCP存储桶中:

# bucket where terraform state file will be kept
bucket = "this-is-my-tf-bucket"

# folder in the bucket where the terraform state file will be kept
prefix = "bucket.folder.sbx"

credentials = "greg-cred.json"

但是,在运行init并应用后,我注意到文件夹(bucket.folder.sbx)在我的存储桶中不存在。初始化和应用运行良好,没有错误。所以我想知道...我的状态文件去哪了?

有没有办法追踪这个?

作为第二个问题,似乎我的本地“ terraform.tfstate”文件没有放在我的.terraform目录中,而是放在了根目录中。我想知道为什么会这样吗?您如何控制本地terraform.tfstate文件的放置位置?

2 个答案:

答案 0 :(得分:0)

预期在项目的根目录中具有terraform.tfstate。 根据Terrafor的体系结构,应在.terraform文件夹中安装模块和提供程序,而不是状态文件。

如果要将状态文件存储在GCP存储桶中,则需要声明“ terraform”块,请参见documentation

terraform {
  backend "gcs" {
    bucket  = "tf-state-prod"
    prefix  = "terraform/state"
  }
}

因此,如果您在本地看到terraform.tfstate,则它肯定不在云中。 Terraform只能在本地或在远程存储桶(具有上述配置)中创建一个状态文件。

答案 1 :(得分:0)

创建backend.tfvars不会创建后端配置。而且,它甚至不会自动加载。稍后再讨论。

要创建GCS远程后端配置,请遵循Terraform GCS backend documentation

terraform {
  backend "gcs" {
    # bucket where terraform state file will be kept
    bucket = "this-is-my-tf-bucket"
    # folder in the bucket where the terraform state file will be kept
    prefix = "bucket.folder.sbx"
    credentials = "greg-cred.json"
  }
}

如果不提供后端配置,则会获得默认的本地后端,并根据default backend documentation状态文件路径:

  

默认相对于根模块默认为“ terraform.tfstate”。

对于backend.tfvars*.tfvars不会自动加载(terraform.tfvars*.auto.tfvars除外)。请参见input variables documentation。如果要加载backend.tfvars文件,则应使用-var-file=backend.tfvars。您还必须从后端配置中引用这些变量。参见此example