无法使用“terraform init -reconfigure”将 S3 后端中的 Terraform 远程状态转换为本地状态

osc*_*ter 9 terraform terraform-remote-state

我正在使用 Terraform v1.0.0 并使用 AWS S3 和 AWS DynamoDB 创建远程后端,如Yevgeniy Brikman 的Terraform Up & Running中所述:

\n
    \n
  1. 我为 S3 存储桶和 DynamoDB 表编写了代码,并通过以下方式创建了资源terraform apply
  2. \n
  3. 我添加terraform { backend "S3" {} }到我的代码中
  4. \n
  5. 我创建了一个backend.hcl包含所有相关参数的文件
  6. \n
  7. 我通过调用将本地状态移至 S3terraform init -backend-config=backend.hcl
  8. \n
\n

现在我想将远程状态转换回本地状态,以便我可以安全地删除远程后端。Brikman 解释说,要做到这一点,必须删除backend配置并调用terraform init. 当我尝试这个时,我看到这个:

\n
$ terraform init\nInitializing modules...\n\nInitializing the backend...\n\xe2\x95\xb7\n\xe2\x94\x82 Error: Backend configuration changed\n\xe2\x94\x82 \n\xe2\x94\x82 A change in the backend configuration has been detected, which may require migrating existing state.\n\xe2\x94\x82 \n\xe2\x94\x82 If you wish to attempt automatic migration of the state, use "terraform init -migrate-state".\n\xe2\x94\x82 If you wish to store the current configuration with no changes to the state, use "terraform init -reconfigure".\n\xe2\x95\xb5\n
Run Code Online (Sandbox Code Playgroud)\n

我认为正确的方法是使用-reconfigure乍一看似乎有效的方法:

\n
$ terraform init -reconfigure\nInitializing modules...\n\nInitializing the backend...\n\nInitializing provider plugins...\n- Reusing previous version of hashicorp/random from the dependency lock file\n- Reusing previous version of hashicorp/aws from the dependency lock file\n- Using previously-installed hashicorp/aws v3.47.0\n- Using previously-installed hashicorp/random v3.1.0\n\nTerraform has been successfully initialized!\n
Run Code Online (Sandbox Code Playgroud)\n

但是执行terraform plan发现初始化没有成功:

\n
$ terraform plan\n\xe2\x95\xb7\n\xe2\x94\x82 Error: Backend initialization required, please run "terraform init"\n\xe2\x94\x82\n\xe2\x94\x82 Reason: Unsetting the previously set backend "s3"\n\xe2\x94\x82\n\xe2\x94\x82 The "backend" is the interface that Terraform uses to store state,\n\xe2\x94\x82 perform operations, etc. If this message is showing up, it means that the\n\xe2\x94\x82 Terraform configuration you\'re using is using a custom configuration for\n\xe2\x94\x82 the Terraform backend.\n\xe2\x94\x82\n\xe2\x94\x82 Changes to backend configurations require reinitialization. This allows\n\xe2\x94\x82 Terraform to set up the new configuration, copy existing state, etc. Please run\n\xe2\x94\x82 "terraform init" with either the "-reconfigure" or "-migrate-state" flags to\n\xe2\x94\x82 use the current configuration.\n\xe2\x94\x82\n\xe2\x94\x82 If the change reason above is incorrect, please verify your configuration\n\xe2\x94\x82 hasn\'t changed and try again. At this point, no changes to your existing\n\xe2\x94\x82 configuration or state have been made.\n\xe2\x95\xb5\n
Run Code Online (Sandbox Code Playgroud)\n

取消设置后端的唯一方法似乎是通过terraform init -migrate-state

\n
$ terraform init -migrate-state\nInitializing modules...\n\nInitializing the backend...\nTerraform has detected you\'re unconfiguring your previously set "s3" backend.\nDo you want to copy existing state to the new backend?\n  Pre-existing state was found while migrating the previous "s3" backend to the\n  newly configured "local" backend. No existing state was found in the newly\n  configured "local" backend. Do you want to copy this state to the new "local"\n  backend? Enter "yes" to copy and "no" to start with an empty state.\n\n  Enter a value: yes\n    \nSuccessfully unset the backend "s3". Terraform will now operate locally.\n
Run Code Online (Sandbox Code Playgroud)\n

terraform init -reconfigure尽管 Terraform 明确告诉我,是否无法通过转换状态?如果是这样,terraform init -reconfigure具体是做什么的?

\n

Min*_*oth 0

从官方文档1来看,它似乎-reconfigure有点破坏性,因为它忽略了现有的配置。我认为,如果您对后端进行了更改,然后运行该命令,那么它只能在假设这是一个新配置的情况下才有效。我最近才自己阅读文档,但我不知道这是这种行为。

因此,回到您的问题,我认为-migrate-state这是在不同后端之间迁移状态时所需使用的选项。terraform init -migrate-state我从你的问题中了解到,这是使用?的情况。