【问题标题】:terraform init creating empty tfstate fileterraform init 创建空的 tfstate 文件
【发布时间】:2020-11-11 22:13:44
【问题描述】:

当我执行 terraform init 命令时,TF 没有 tfstate(我的 tfstate 文件在 s3 存储桶中)。我也看不到 .terraform 文件夹中的 terraform backend-config 文件。

我使用的是 terraform 0.10.4 版本


输出:

$ terraform --version
Terraform v0.10.4
    
$ terraform init \

-lock="true"
-backend-config="bucket=$TF_STATE_BUCKET"
-backend-config="key=$TF_STATE_KEY"
-backend-config="dynamodb_table=$TF_LOCK_TABLE"
-backend-config="region=$AWS_REGION"
-backend-config="profile=$AWS_PROFILE"
-backend-config="encrypt=true"
.
Downloading modules...
Get: git::ssh://XXXXXXXXXXXXXXXXX/add/tf-vpc.git?ref=1.0.1
Get: git::ssh://XXXXXXXXXXXXXXXXX/add/tf-ec-redis.git?ref=1.1.3
Get: git::ssh://XXXXXXXXXXXXXXXXX/add/tf-rds-pg.git?ref=1.3.0

Initializing provider plugins...
    
Checking for available provider plugins on https://releases.hashicorp.com...
Downloading plugin for provider "aws" (0.1.4)...
    
The following providers do not have any version constraints in configuration, so the latest version was installed.
    
To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below.

provider.aws: version = "~> 0.1"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.

$ ll -al .terraform/
total 8
drwxr-xr-x 1 XXXXX 1049089 0 Sep 12 18:10 modules/
drwxr-xr-x 1 XXXXX 1049089 0 Sep 12 18:10 plugins/

在当前位置,TF 会备份 s3-tfstate 文件,但它不存在。

【问题讨论】:

  • $ terraform refresh 空或不存在的状态文件。刷新不会做任何事情。刷新不会出错或返回错误的退出状态,因为许多自动化脚本使用刷新、计划、然后应用,并且可能还没有第一次运行的状态文件。输出:dev_private_zone_name = XXXXXXXXXX.local private_zone_name = XXXXXXXXXX.local

标签: terraform


【解决方案1】:

这是 0.9.x 之后的预期行为。 .terraform/terraform.tfstate 的本地 tfstate 文件几乎是一个空文件。唯一的变化是其中的序列号。它不断增加。

"serial": 1,

如果不运行terraform apply,远程tfstate 文件将不会更新。如果您从未运行过terraform apply,则远程 tfstate 文件不存在。

因此尝试进行一些更改,然后检查远程 tfstate 文件(在您的情况下,它是 s3://$TF_STATE_BUCKET/$TF_STATE_KEY),您应该会看到不同之处。

【讨论】:

  • 作为您的 cmets,我已将 tfstate 文件 seriel 修改为 1 并上传到 s3 存储桶,然后我像往常一样运行 terraform 命令,再次 TF 创建 Empty 或不存在的状态文件。 ------------------------------------- 命令输出: - $ terraform refresh Empty or non-existent state file. Refresh will do nothing. Refresh does not error or return an erroneous exit status because many automation scripts use refresh, plan, then apply and may not have a state file yet for the first run. Outputs: output values....
  • 还有一个信息,tfstate 文件未在 .terraform 文件夹中创建。
  • terraform.tfstate 文件将在.terraform 文件夹中创建,但其中没有资源信息。
  • terraform.tfstate 文件未在 .terraform 文件夹中创建。见下文,只有模块和插件。 [ $ ls .terraform/ ------- 模块/插件/ ]
  • 如果您使用后端配置正确运行init,则无需手动将 tfstate 文件上传到 s3 存储桶。所以你的环境有问题。您粘贴的 init 命令对我来说看起来不错。
猜你喜欢
  • 1970-01-01
  • 2019-05-16
  • 1970-01-01
  • 2019-12-08
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
相关资源
最近更新 更多