My Digital Garden

Importing Linode resources to Terraform

Importing Linode resources to Terraform

  1. Make sure your local copy of the Terraform CLI is up to date

  2. Make sure your Terraform definition files include a backend definition, e.g (for Terraform cloud).

    terraform {
      cloud {
        organization = "MYORG"
    
        workspaces {
          name = "MYWORKSPACE"
        }
      }
    }
  3. if you haven't already, run terraform init

  4. Ensure that Python 3 and pip3 are both installed on your local machine (Linode API prerequisite)

  5. Get a Linode API token

    • Set environment variables LINODE_TOKEN and LINODE_CLI_TOKEN with the value of the token
  6. Install Linode CLI (you may want to define and activate a Python Virtual Environment first)

  7. Get a list of your resources using CLI commands, e.g.

    • servers - linode-cli linodes list --json --pretty
    • domains - linode-cli domains list --json --pretty
    • domain records - linode-cli domains records-list DOMAINID --json --pretty
    • node balancers - linode-cli nodebalancers list --json --pretty
    • node balancer config - linode-cli nodebalancers configs-list NODEBALANCERID --json --pretty
    • node balancer nodes - linode-cli nodebalancers nodes-list NODEBALANCERID NODEBALANCERCONFIGID --json --pretty
  8. From the output of the previous command(s), make a note of the id for each resource

  9. For each resource

    1. create a placeholder in the Terraform model
    2. run terraform import linode_instance.LABEL ID
    3. fill in the details of the resource definition based on data from terraform show
  10. test your model by running terraform plan - should get zero changes proposed when your model is complete

See also