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

Posted

技术标签:

【中文标题】无法使用“terraform init -reconfigure”将 S3 后端中的 Terraform 远程状态转换为本地状态【英文标题】:Cannot convert Terraform remote state in S3 backend to local state with `terraform init -reconfigure` 【发布时间】:2021-09-10 13:23:34 【问题描述】:

我正在使用 Terraform v1.0.0,并使用 AWS S3 和 AWS DynamoDB 创建了一个远程后端,正如 Yevgeniy Brikman 在 Terraform Up & Running 中所解释的那样:

    我为 S3 存储桶和 DynamoDB 表编写了代码,并通过terraform apply 创建了资源 我在代码中添加了terraform backend "S3" 我创建了一个包含所有相关参数的backend.hcl 文件 我通过调用 terraform init -backend-config=backend.hcl 将我的本地状态移动到 S3

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

$ terraform init
Initializing modules...

Initializing the backend...
╷
│ Error: Backend configuration changed
│ 
│ A change in the backend configuration has been detected, which may require migrating existing state.
│ 
│ If you wish to attempt automatic migration of the state, use "terraform init -migrate-state".
│ If you wish to store the current configuration with no changes to the state, use "terraform init -reconfigure".
╵

我认为正确的方法是使用-reconfigure,乍一看似乎可行:

$ terraform init -reconfigure
Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/random from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v3.47.0
- Using previously-installed hashicorp/random v3.1.0

Terraform has been successfully initialized!

但是,执行terraform plan 显示初始化没有成功:

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

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

$ terraform init -migrate-state
Initializing modules...

Initializing the backend...
Terraform has detected you're unconfiguring your previously set "s3" backend.
Do you want to copy existing state to the new backend?
  Pre-existing state was found while migrating the previous "s3" backend to the
  newly configured "local" backend. No existing state was found in the newly
  configured "local" backend. Do you want to copy this state to the new "local"
  backend? Enter "yes" to copy and "no" to start with an empty state.

  Enter a value: yes
    
Successfully unset the backend "s3". Terraform will now operate locally.

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

【问题讨论】:

【参考方案1】:

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

所以,回到您的问题,我假设-migrate-state 是在不同后端之间迁移状态时使用的理想选项。我从您的问题中了解到,使用terraform init -migrate-state 就是这种情况?

【讨论】:

【参考方案2】:

以下解决方法为我解决了这个问题。

在下面添加并运行 terraform init

地形 后端“本地”

【讨论】:

以上是关于无法使用“terraform init -reconfigure”将 S3 后端中的 Terraform 远程状态转换为本地状态的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 StorageClass 配置卷 - 无法获取存储帐户的存储密钥

Worklight Studio 和本地开发,有时无法使用 Java 类,有时无法使用 HTML 文件

ADB无法使用解决办法

Ubuntu 80端口无法使用-非root用户无法使用1024以下端口

无法在 SQL Server 视图中使用工作查询:“IS”无法识别“>”无法识别

LINUX下的mail\mailx为啥无法使用外部SMTP发邮件