Terraform
将状态迁移到 HCP Terraform
在使用 Terraform 社区版时,您需要负责维护一个 状态 文件,作为云基础设施的事实来源。Terraform Cloud 提供安全的远程状态存储,使其更易于协作开发基础设施。您可以将状态迁移到 HCP Terraform,而不会中断服务或重新创建现有基础设施。
在本教程中,您将迁移一个本地状态文件到 HCP Terraform。
先决条件
本教程假定您具备以下条件:
- 已在本地安装 Terraform CLI 版本 1.1 或更高版本
- 一个 HCP Terraform 帐户
创建状态
克隆 本教程的示例配置。此配置使用 random 提供程序来生成随机的宠物名称。
$ git clone https://github.com/hashicorp-education/learn-state-migration
接下来,进入该目录。
$ cd learn-state-migration
打开 main.tf 以查看配置。它使用输入变量来确定生成的字符串的长度并输出该值。
main.tf
terraform {
required_providers {
random = {
source = "hashicorp/random"
version = "3.3.2"
}
}
required_version = ">= 1.1.0"
}
variable "name_length" {
description = "The number of words in the pet name"
default = "3"
}
resource "random_pet" "pet_name" {
length = var.name_length
separator = "-"
}
output "pet_name" {
value = random_pet.pet_name.id
}
初始化目录。
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of hashicorp/random from the dependency lock file
- Installing hashicorp/random v3.3.2...
- Installed hashicorp/random v3.3.2 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
现在应用配置,在提示符处输入 yes 以确认操作。
$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
+ create
Terraform will perform the following actions:
# random_pet.pet_name will be created
+ resource "random_pet" "pet_name" {
+ id = (known after apply)
+ length = 3
+ separator = "-"
}
Plan: 1 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ pet_name = (known after apply)
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
random_pet.pet_name: Creating...
random_pet.pet_name: Creation complete after 0s [id=mostly-joint-lacewing]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
pet_name = "mostly-joint-lacewing"
Terraform 在输出中显示生成的名称。
配置 HCP Terraform 集成
现在您已经有了本地状态文件,您需要创建配置中的 cloud 代码块。
要将 HCP Terraform 用作配置的后端,您必须在配置中包含一个 cloud 块。
将 cloud 块添加到您的配置中,如下所示,并将 ORGANIZATION-NAME 替换为您自己的 HCP Terraform 组织名称。
main.tf
terraform {
cloud {
organization = "ORGANIZATION-NAME"
workspaces {
name = "learn-terraform-migrate"
}
}
required_version = ">= 1.1.0"
required_providers {
random = {
source = "hashicorp/random"
version = "3.3.2"
}
}
}
虽然在 `cloud` 节中定义的组织必须已经存在,但工作区不必存在;HCP Terraform 将在必要时创建它。 如果您使用现有的工作区,则它不应具有任何现有的状态。
使用 HCP Terraform 身份验证
配置好 HCP Terraform 集成后,您必须进行 HCP Terraform 身份验证才能将其用于远程操作。
运行 terraform login 并按照提示登录,并在确认提示处输入 yes。
$ terraform login
Terraform will request an API token for app.terraform.io using your browser.
If login is successful, Terraform will store the token in plain text in
the following file for use by subsequent commands:
/Users/username/.terraform.d/credentials.tfrc.json
Do you want to proceed?
Only 'yes' will be accepted to confirm.
Enter a value:
有关登录的更详细说明,请查看 登录教程。
迁移状态文件
要将现有的状态文件迁移到 HCP Terraform,您必须重新初始化配置以更新后端。
重新初始化配置。 Terraform 会检测到您更新的后端,并确认您希望将状态文件迁移到 HCP Terraform。 输入 yes 以确认迁移。
$ terraform init
Initializing HCP Terraform...
Do you wish to proceed?
As part of migrating to HCP Terraform, Terraform can optionally copy your
current workspace state to the configured HCP Terraform workspace.
Answer "yes" to copy the latest state snapshot to the configured
HCP Terraform workspace.
Answer "no" to ignore the existing state and just activate the configured
HCP Terraform workspace with its existing state, if any.
Should Terraform migrate your existing state?
Enter a value: yes
Initializing provider plugins...
- Reusing previous version of hashicorp/random from the dependency lock file
- Using previously-installed hashicorp/random v3.0.1
HCP Terraform has been successfully initialized!
You may now begin working with HCP Terraform. Try running "terraform plan" to
see any changes that are required for your infrastructure.
If you ever set or change modules or Terraform Settings, run "terraform init"
again to reinitialize your working directory.
配置 HCP Terraform 工作区
将状态迁移到 HCP Terraform 后,请登录到 HCP Terraform Web UI,然后导航到您的 learn-terraform-migrate 工作区。 然后,转到工作区的 状态 页面。 HCP Terraform 会列出迁移到您新的工作区中的状态。

您的配置依赖于一个输入变量。 导航到工作区的 变量 页面,并创建一个名为 name_length 的 Terraform 变量,其值为 5。

如果配置依赖于云提供商,您还会在此页面上设置提供程序凭据。
在新工作区发起运行
在验证 Terraform 将你的状态迁移到 HCP Terraform 后,移除你的本地状态文件。
$ rm terraform.tfstate
触发新的运行。Terraform 将会建议替换你的资源以反映 name_length 输入变量的更新。通过键入 yes 来确认操作。
$ terraform apply
Running apply in HCP Terraform. Output will stream here. Pressing Ctrl-C
will cancel the remote apply if it's still pending. If the apply started it
will stop streaming the logs, but will not stop the apply running remotely.
Preparing the remote apply...
To view this run in a browser, visit:
https://app.terraform.io/app/hashicorp-training/learn-terraform-migrate/runs/run-d7aKcNjPL5WjHwuR
Waiting for the plan to start...
Terraform v1.2.2
on linux_amd64
Initializing plugins and modules...
random_pet.pet_name: Refreshing state... [id=ghastly-supreme-tuna]
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
# random_pet.pet_name must be replaced
-/+ resource "random_pet" "pet_name" {
~ id = "ghastly-supreme-tuna" -> (known after apply)
~ length = 3 -> 5 # forces replacement
# (1 unchanged attribute hidden)
}
Plan: 1 to add, 0 to change, 1 to destroy.
Changes to Outputs:
~ pet_name = "ghastly-supreme-tuna" -> (known after apply)
Do you want to perform these actions in workspace "learn-terraform-migrate"?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
random_pet.pet_name: Destruction complete after 0s
random_pet.pet_name: Creating...
random_pet.pet_name: Creation complete after 0s [id=possibly-eminently-sadly-inspired-mongoose]
Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
Outputs:
pet_name = "possibly-eminently-sadly-inspired-mongoose"
Terraform 将日志流式传输到你的本地控制台,并且还在工作区 UI 中显示运行详情。

销毁你的基础设施
运行 terraform destroy 以清理你的资源。
$ terraform destroy
Running apply in HCP Terraform. Output will stream here. Pressing Ctrl-C
will cancel the remote apply if it's still pending. If the apply started it
will stop streaming the logs, but will not stop the apply running remotely.
Preparing the remote apply...
To view this run in a browser, visit:
https://app.terraform.io/app/hashicorp-training/learn-terraform-migrate/runs/run-StNegAY8UrBCT6FB
Waiting for the plan to start...
Terraform v1.2.2
on linux_amd64
Initializing plugins and modules...
random_pet.pet_name: Refreshing state... [id=possibly-eminently-sadly-inspired-mongoose]
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# random_pet.pet_name will be destroyed
- resource "random_pet" "pet_name" {
- id = "possibly-eminently-sadly-inspired-mongoose" -> null
- length = 5 -> null
- separator = "-" -> null
}
Plan: 0 to add, 0 to change, 1 to destroy.
Changes to Outputs:
- pet_name = "possibly-eminently-sadly-inspired-mongoose" -> null
Do you really want to destroy all resources in workspace "learn-terraform-migrate"?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
random_pet.pet_name: Destruction complete after 0s
Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
你还可以从工作区的设置页面中可选地删除你的 HCP Terraform 工作区。请查看 销毁资源和工作区教程 以获取详细指导。
下一步
在本教程中,你将状态文件从你的本地机器迁移到 HCP Terraform 工作区。要了解有关相关概念和 HCP Terraform 功能的更多信息,请查阅以下资源
- 查看有关 从多个本地工作区迁移状态 的文档
- 按照有关使用 HCP Terraform 运行触发器 连接工作区的教程
- 了解 如何在 HCP Terraform 中管理权限