Terraform Import (For existing Devices)

Terraform can import existing infrastructure resources. This functionality lets you bring existing resources under Terraform management.

The procedure to import Terraform is as follows:

  • create a config file with the resources you want to manage from Terraform
terraform {
  required_providers {
    hivelocity = {
      source   = "hivelocity/hivelocity"
    }
  }
}
resource "hivelocity_bare_metal_device" "myserver" {
  product_id        = 490
  os_name           = "CentOS 7.x"
  location_name     = "ATL2"
  hostname          = "server.terraform.test"
}
  • run import command for each resource so terraform knows they already exist and shouldn't be created from scratch
$ terraform import hivelocity_bare_metal_device.myserver 1234                                                                                                                                                                                                                  

hivelocity_bare_metal_device.myserver: Importing from ID "1234"...
hivelocity_bare_metal_device.myserver: Import prepared!
  Prepared hivelocity_bare_metal_device for import
hivelocity_bare_metal_device.myserver: Refreshing state... [id=1234]

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.
  • then you can make edits to the config file and run terraform apply. Terraform will adjust the infrastructure to match what is outlined in the config. It might for instance reload a server if the OS is different
$ terraform apply 
                                                                                                                                                                                                                                                               
hivelocity_bare_metal_device.myserver: Refreshing state... [id=1234]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # hivelocity_bare_metal_device.myserver will be updated in-place
  ~ resource "hivelocity_bare_metal_device" "myserver" {
      ~ hostname          = "relaxing-currant.hivelocitydns.com" -> "server.terraform.test"
        id                = "1234"
      ~ os_name           = "AlmaLinux 8.x" -> "CentOS 7.x"
        tags              = []
        # (9 unchanged attributes hidden)

        # (1 unchanged block hidden)
    }

Plan: 0 to add, 1 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: