上篇介绍 state rm,强制 terraform 遗忘已经存在的 state。然而 state rm 後并没有说明如何修复或 undo,让 module 留在一个会激怒 team member 的状态XD
这篇介绍 state rm 的反向操作:terraform import
课程内容与代码会放在 Github 上: https://github.com/chechiachang/terraform-30-days
赛後文章会整理放到个人的部落格上 http://chechia.net/
import 是在 terraform root command,用来 import 已经存在的 remote resource 到 state。可以看 官方文件 import 中描述
要 import 之前,要先手写产生 .tf resource
subnet[0]
,要先写 subnet[0] 的 resource 在 .tf 中subnet[0]
的 resource address,将 remote resource import 近来文件提到 Terraform 目前的 import 实作,只影响 state,不会产生 .tf resource。这是什麽意思?
另一个专案 Google Cloud Platform 推出的 terraformer 有提供直接 import 并产生 .tf 档案的方法
这边接续上堂课 state rm 的范例,直接使用 import
上堂课我们卡在这边
Plan: 1 to add, 0 to change, 0 to destroy.
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
module.network.azurerm_subnet.subnet[0]: Creating...
╷
│ Error: A resource with the ID "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-1" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_subnet" for more information.
│
│ with module.network.azurerm_subnet.subnet[0],
│ on .terraform/modules/network/main.tf line 15, in resource "azurerm_subnet" "subnet":
│ 15: resource "azurerm_subnet" "subnet" {
│
╵
ERRO[0129] 1 error occurred:
* exit status 1
刚好这例子中
module.network.azurerm_subnet.subnet[0]
到 terraform registry 中的各个 resource 文件下查询 import 语法,例如 azurerm_subnet
底下有 import 语法
terraform import azurerm_subnet.exampleSubnet /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Network/virtualNetworks/myvnet1/subnets/mysubnet1
cd azure/foundation/compute_network
terragrunt import "module.network.azurerm_subnet.subnet[0]" "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-1"
module.network.azurerm_subnet.subnet[0]: Importing from ID "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-1"...
module.network.azurerm_subnet.subnet[0]: Import prepared!
Prepared azurerm_subnet for import
module.network.azurerm_subnet.subnet[0]: Refreshing state... [id=/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-1]
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
显示 import 成功
terragrunt state list
module.network.data.azurerm_resource_group.network
module.network.azurerm_subnet.subnet[0]
module.network.azurerm_subnet.subnet[1]
module.network.azurerm_subnet.subnet[2]
module.network.azurerm_virtual_network.vnet
terragrunt plan
Changes to Outputs:
~ vnet_subnets = [
- null,
+ "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-1",
"/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-2",
# (1 unchanged element hidden)
]
You can apply this plan to save these new output values to the Terraform
state, without changing any real infrastructure.
plan 显示剩下唯一有差异的地方是 output 的值,我们进行 apply 来更新这个 output
terragrunt apply
上面我们直接使用 state rm 後的例子来做 import 示范,但实务上会更接近
与前面一样,更改 state 会有多人协作的问题,这边操作流程类似。如果团队在专案途中开始导入 terraform 的话,不妨参考一下这个流程
所以,何时需要 import
一个是团队成员中有 state rm 狂人,或是有人搞砸弄坏 state 被迫上来修
一个是上面第二个例子,就是逐渐导入 terraform 的过程,如果有既有的 resource,这样会常常用到 import,请小心操作
另外一个,也算是常见的例子,就是被中断的 terraform workflow
树有非常多变型,下面是Wiki的截图 以下简单介绍几种常听到的~ AVL Trees、Red Bla...
「我们的连结够安全吗。」 HTTP(HyperText Transfer Protocol) 超文本...
今天介绍另外一个古老的文件技术FTP(File Transfer Protocol)。建立一个档案s...
Feedforward 前馈神经网路 前馈神经网路是一种neuron之间的连接并不形成循环的人工智...
昨天我们新增了一个 UserServiceWithFile 操作本地端档案来管理使用者资料,而档案的...