You need to enable JavaScript to run this app.
导航

使用Terraform管理游离态云资源

最近更新时间2024.01.22 17:33:21

首次发布时间2024.01.22 16:43:00

操作场景

如果您在使用 Terraform 管理云资源之前,已经通过火山引擎控制台、SDK、CLI 等方式创建了云资源,期望 Terraform 将这些游离的存量资源纳入生命周期管理体系,您可以参考本篇内容进行资源导入操作,实现对所有云资源的统一管理。

导入存量资源

Terraform 资源导入可以分为以下三个主要步骤:

本文将以使用 Terraform 导入一个 ECS 实例为例,为您展示具体的操作步骤。

步骤一:获取资源 ID

获取待导入资源的资源 ID:

  • 方式一:登录火山引擎云服务器控制台,查询并记录待导入实例的ID,如下图所示:

  • 方式二:通过 Terraform 的 DataSource 进行条件查询过滤,得到待导入实例的 ID:

    data "volcengine_ecs_instances" "default" {
      vpc_id     = "vpc-13fbg2iw2816o3n6nu50v****"
      name_regex = "tf-test"
    }
    
    output "ecs_ids" {
      value = data.volcengine_ecs_instances.default.instances[*].instance_id
    }
    

    执行结果如下:

    Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    ecs_ids = tolist([
      "i-ycwqogkzcw5i3z2x****",
    ])
    

步骤二:声明待导入资源

  1. 进入 Terraform 工作目录,在 .tf 模版文件中定义待导入资源的资源声明,用于指定待导入资源在 .tfstate 状态文件中的存放路径。
    导入操作和创建一个新资源的声明类似,可以暂时忽略具体参数的取值,如下所示:

    resource "volcengine_ecs_instance" "ecs" {}
    
  2. 在工作目录下执行 init 命令完成 Terraform 的初始化工作:

    terraform init --upgrade
    
  3. 初始化成功后,执行 import 命令完成资源的导入操作,完整的命令格式为:
    terraform import <资源类型>.<资源标识> <资源 ID>。具体操作及执行结果如下所示:

    $ terraform import volcengine_ecs_instance.ecs i-ycwqogkzcw5i3z2x****
    
    volcengine_ecs_instance.ecs: Importing from ID "i-ycwqogkzcw5i3z2x****"...
    volcengine_ecs_instance.ecs: Import prepared!
      Prepared volcengine_ecs_instance for import
    volcengine_ecs_instance.ecs: Refreshing state... [id=i-ycwqogkzcw5i3z2x****]
    
    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.
    
  4. 导入成功后,执行以下命令,查看当前工作目录下 .tfstate 状态文件中该资源的具体信息。

    cat terraform.tfstate
    

步骤三:补齐资源定义

由于步骤二在 .tf 模版文件中只定义了最简单的资源声明,没有资源参数的详细定义,因此在执行 import 命令后,.tf 模版文件 和 .tfstate 状态文件之间存在差异,此时直接执行 plan 命令,Terraform 会认为资源需要进行更新操作。
因此,您需要在 .tf 模版文件中手动补齐缺失的参数定义,直至运行 plan 命令不再出现资源变更信息为止。

  1. 在工作目录下执行 show 命令,打印资源的详细信息如下。

    说明

    请将信息拷贝至 .tf 模版文件中,注意要删除不可设置的参数,请参见Terraform官网Attributes Reference部分删除其中涉及的参数,例如如下代码中的 id 等。

    $ terraform show
    
    # volcengine_ecs_instance.ecs:
    resource "volcengine_ecs_instance" "ecs" {
        description          = "tf-test"
        host_name            = "tf-test"
        id                   = "i-ycwqogkzcw5i3z2x****"
        image_id             = "image-yclmzamg7s6p2rpk****"
        instance_charge_type = "PostPaid"
        instance_id          = "i-ycwqogkzcw5i3z2x****"
        instance_name        = "tf-test-ecs"
        instance_type        = "ecs.g3a.large"
        project_name         = "default"
        security_group_ids   = [
            "sg-rrj44qw6v6rkv0x57tt****",
        ]
        subnet_id            = "subnet-13fbg9fhyey2o3n6nu414****"
        vpc_id               = "vpc-13fbg2iw2816o3n6nu50v****"
        zone_id              = "cn-beijing-a"
        
        ... ...
    }
    
  2. 在部分资源的参数中,可能存在一些参数无法查询得到,这类参数在资源被导入后,需要手动加入忽略列表,以防被 Terraform 识别为需要进行更新操作。比如在ECS资源中,需要手动忽略以下参数:

    resource "volcengine_ecs_instance" "foo" {
      ... ...
      
      lifecycle {
        ignore_changes = [password, keep_image_credential, security_enhancement_strategy, auto_renew, auto_renew_period]
      }
    }
    

    说明

    所有需要手动忽略的参数,在Terraform官网参数的 Description 均会有以下提示:
    "When importing resources, this attribute will not be imported. If this attribute is set, please use lifecycle and ignore_changes ignore changes in fields."

  3. 上述操作完成后,执行 plan 命令,显示 No changes,表示资源已经成功导入 Terraform 的管理体系。

    $ terraform plan
    
    volcengine_ecs_instance.ecs: Refreshing state... [id=i-ycwqogkzcw5i3z2x****]
    
    No changes. Your infrastructure matches the configuration.
    
    Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
    

移除已管理资源

在实际操作中,可能存在更多复杂场景,比如回滚资源的误导入操作、修改资源的导入路径、重构调整资源的模版架构等。
对于这一类场景,您需要先将被 Terraform 管理的资源从 .tfstate 状态文件中移除,再重新在调整后的工作目录执行资源导入操作。
Terraform 可以通过执行 state rm 命令来完成已管理资源的移除操作,完整的命令格式为
terraform state rm <资源类型>.<资源标识>。具体操作及执行结果如下所示:

terraform state rm volcengine_ecs_instance.ecs
Removed volcengine_ecs_instance.ecs
Successfully removed 1 resource instance(s).

说明

state rm 命令只会将指定的资源从 .tfstate 状态文件中移除,使其不再被 Terraform 管理,并不会真正删除该资源。