更改 state 有其風險,State manipulation 有賺有賠,更改前應詳閱官方文件說明書之二
state inspection
課程內容與代碼會放在 Github 上: https://github.com/chechiachang/terraform-30-days
賽後文章會整理放到個人的部落格上 http://chechia.net/
既然手動直接 vim 下去不是一個好方法,我們先來看 terraform 官方支援的 state 操作
state
terraform state list 上堂課已經說明過,放在這邊做範例
cd azure/foundation/compute_network
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
另外一個用例是,有時我們在 web console 上看到 azure cloud 上的 remote resource,例如:在 azure web console 上看到一個 subnet,我們想要
azure/modules/compute_network
內又引用 Azure/network/azurerm
terraform 提供了 state filter by id 的方式
terraform state list -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]
這時我們可以進一步 show 出 state 的內容
terragrunt state show module.network.azurerm_virtual_network.vnet
resource "azurerm_virtual_network" "vnet" {
address_space = [
"10.2.0.0/16",
]
dns_servers = []
guid = "5e5fb9de-600b-4085-a59d-0792c567c3a3"
id = "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet"
location = "southeastasia"
name = "acctvnet"
resource_group_name = "terraform-30-days"
subnet = [
{
address_prefix = "10.2.1.0/24"
id = "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-1"
name = "dev-1"
security_group = ""
},
{
address_prefix = "10.2.2.0/24"
id = "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-2"
name = "dev-2"
security_group = ""
},
{
address_prefix = "10.2.3.0/24"
id = "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-3"
name = "dev-3"
security_group = ""
},
]
tags = {
"environment" = "foundation"
}
vm_protection_enabled = false
}
基本上所有 terraform 中設定 / 產生的資料,state 裡面都有
在看另外一個
terragrunt state show module.network.azurerm_subnet.subnet[0]
zsh: no matches found: module.network.azurerm_subnet.subnet[0]
terragrunt state show 'module.network.azurerm_subnet.subnet[0]'
resource "azurerm_subnet" "subnet" {
address_prefix = "10.2.1.0/24"
address_prefixes = [
"10.2.1.0/24",
]
enforce_private_link_endpoint_network_policies = false
enforce_private_link_service_network_policies = false
id = "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-1"
name = "dev-1"
resource_group_name = "terraform-30-days"
service_endpoint_policy_ids = []
service_endpoints = []
virtual_network_name = "acctvnet"
}
對資安敏感的人,可能看到 state show 已經覺得不太舒服
terraform state 其實是有一定的敏感性,官方文件也有建議要好好保管 terraform state 檔案