首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用“Terraform init -reconfigure`”将S3后端中的Terraform远程状态转换为本地状态

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

Stack Overflow用户
提问于 2021-06-28 08:24:02
回答 3查看 2.5K关注 0票数 6

我正在使用Terraformv1.0.0,并使用AWS S3和AWS DynamoDB创建了一个远程后端,如Terraform中所解释的&由Yevgeniy运行:

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

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

代码语言:javascript
运行
复制
$ 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,乍一看,这似乎是可行的:

代码语言:javascript
运行
复制
$ 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显示初始化没有成功:

代码语言:javascript
运行
复制
$ 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

代码语言:javascript
运行
复制
$ 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到底是做什么的?

EN

回答 3

Stack Overflow用户

发布于 2021-11-10 12:27:02

从正式的docs1来看,-reconfigure似乎有点破坏性,因为它忽略了现有的配置。我认为,如果您对后端进行了更改,然后运行该命令,那么它只能在假设这是一个新配置的情况下工作。我最近刚看了一下医生,我不知道这就是我的行为。

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

票数 0
EN

Stack Overflow用户

发布于 2022-01-22 07:23:17

下面的工作为我解决了这个问题。

添加下面并运行terraform init

terraform {后端“本地”{}

票数 0
EN

Stack Overflow用户

发布于 2022-08-19 11:05:33

正如MindTooth所说的,命令init -migrate-state完全可以完成您想要做的事情。当配置了不同的后端时,它将不改变状态。

init -reconfigure将以干净的空状态初始化新后端。

另一种方法是将状态从s3后端提取到json文件中。然后使用init -reconfigure初始化一个空的本地后端,并将状态推回。

代码语言:javascript
运行
复制
terraform state pull > state.json
terraform init -reconfigure
terraform state push state.json
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68159962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档