You need to enable JavaScript to run this app.
导航
state管理命令
最近更新时间:2022.07.01 10:39:23首次发布时间:2022.05.17 12:06:31

Terraform会将资源的状态和属性等信息保存在terraform.tfstate文件中,使用terraform state相关命令可以管理state文件。

terraform state list

用于列出state文件中的所有资源,输出格式为:<资源类型>.<资源名称>。

$ terraform state list
volcengine_vpc.foo

terraform state show

用于查看某个资源的所有属性值,命令格式为terraform state show <资源类型>.<资源名称>

$terraform state show volcengine_vpc.foo
# volcengine_vpc.foo:
resource "volcengine_vpc" "foo" {
    account_id            = "XXXXXX"
    associate_cens        = []
    auxiliary_cidr_blocks = []
    cidr_block            = "172.16.0.0/16"
    creation_time         = "2022-04-02T16:02:40+08:00"
    dns_servers           = [
        "114.114.114.114",
        "8.8.8.8",
    ]
    id                    = "vpc-mjhlf3gs61hc5smt1ajya7ct"
    nat_gateway_ids       = []
    route_table_ids       = [
        "vtb-mjhlf5fta3gg5smt1ambg5j3",
    ]
    security_group_ids    = [
        "sg-mjhlf9dvi7eo5smt1aucvf7z",
    ]
    status                = "Available"
    subnet_ids            = []
    update_time           = "2022-04-02T16:02:41+08:00"
    vpc_id                = "vpc-mjhlf3gs61hc5smt1ajya7ct"
    vpc_name              = "tf-test-2"
}

terraform state rm

用于将指定资源从state文件中移除,但并非将资源删除,命令格式为:terraform state rm <资源类型>.<资源名称>

$terraform state rm volcengine_vpc.foo
Removed volcengine_vpc.foo
Successfully removed 1 resource instance(s).

terraform state mv

用于变更指定资源的名称或存放路径。

  • 变更资源名称:命令为:terraform state mv <资源类型>.<旧资源名称> <资源类型>.<新资源名称>

    $terraform state mv volcengine_vpc.foo volcengine_vpc.test
    Move "volcengine_vpc.foo" to "volcengine_vpc.test"
    Successfully moved 1 object(s).
    
  • 变更资源路径:将资源从当前所属的state文件移动到其他state文件中,同时可以变更资源名称,命令为:terraform state mv -state-out=state文件相对/绝对路径 <资源类型>.<旧资源名称> <资源类型>.<新资源名称>。示例如下:

    $terraform state mv -state-out=../vpc/terraform.tfstate  volcengine_vpc.foo volcengine_vpc.test
    Move "volcengine_vpc.foo" to "volcengine_vpc.test"
    Successfully moved 1 object(s).