Terraforming Linode

terraform linode

I have been hosting my websites with Linode (now part of Akamai), and my hosting has been very stable. I created my first nanode server manually, but I started using Terraform for some of my other projects. Creating a nanode server is a great intro to using Terraform.

The first step is to install Terraform in your environment. Hashicorp's instructions are very clear. I typically work with Linux so my path is to setup the Hashicorp package repository and install Terraform from there.

Log in your Linode account, and generate an API token. The API Tokens menu is located on the account dropdown menu of the web console. Click the Create A Personal Access Token. Enter a label for the token and click the Create Token at the bottom of the panel. Please heed the advice in the popup window. This token is only displayed once. Save it in a password manager. This is an ideal use case for Hashicorp Vault, but that is not necessary for this exercise.

Create a project directory and navigate into the directory.

mkdir my-project
cd my-project

Create a file named main.tf in your Terraform project directory and add the following code. Replace <YOUR_LINODE_API_TOKEN> with the API token that was just generated in the main.tf file.

terraform {
  required_providers {
    linode = {
      source = "linode/linode"
      version = "1.29.4"
    }
  }
}
provider "linode_instance" {
  token = "<YOUR_LINODE_API_TOKEN>"
}

Add the resource code block to define your new Nanode server. Replace <YOUR_SSH_PUBLIC_KEY> and <YOUR_ROOT_PASSWORD> with your SSH public key and root password respectively in the main.tf file.

resource "linode_instance" "nanode-server" {
        image = "linode/ubuntu18.04"
        label = "my-nanode-server"
        group = "my-group"
        region = "us-east"
        type = "nanode-1"
        authorized_keys = [ "<YOUR_PUBLIC_SSH_KEY>" ]
        root_pass = "<YOUR_ROOT_PASSWORD>"
}

Save the main.tf file and initialize the file with the terraform init command. The results should look similar to this.

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding linode/linode versions matching "1.29.4"...
- Installing linode/linode v1.29.4...
- Installed linode/linode v1.29.4 (signed by a HashiCorp partner, key ID F4E6BBD0EA4FE463)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Generate the execution plan with the terraform plan. The results should look similar to this.

$ terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
  + create

Terraform will perform the following actions:

  # linode_instance.nanode-server will be created
  + resource "linode_instance" "nanode-server" {
      + authorized_keys    = [
          + "YOUR_PUBLIC_SSH_KEY",
        ]
      + backups            = (known after apply)
      + backups_enabled    = (known after apply)
      + boot_config_label  = (known after apply)
      + booted             = (known after apply)
      + group              = "my-group"
      + id                 = (known after apply)
      + image              = "linode/ubuntu20.04"
      + ip_address         = (known after apply)
      + ipv4               = (known after apply)
      + ipv6               = (known after apply)
      + label              = "my-nanode-server"
      + private_ip_address = (known after apply)
      + region             = "us-east"
      + resize_disk        = false
      + root_pass          = (sensitive value)
      + shared_ipv4        = (known after apply)
      + specs              = (known after apply)
      + status             = (known after apply)
      + swap_size          = (known after apply)
      + type               = "nanode-1"
      + watchdog_enabled   = true
    }

Plan: 1 to add, 0 to change, 0 to destroy.

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run
"terraform apply" now.

Verify that the output from the plan command looks acceptable. If everything looks good, apply the configuration with the terraform apply command. When prompted, type yes to confirm the changes and proceed with the deployment.

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:
  + create

Terraform will perform the following actions:

  # linode_instance.nanode-server will be created
  + resource "linode_instance" "nanode-server" {
      + authorized_keys    = [
          + "YOUR_PUBLIC_SSH_KEY",
        ]
      + backups            = (known after apply)
      + backups_enabled    = (known after apply)
      + boot_config_label  = (known after apply)
      + booted             = (known after apply)
      + group              = "my-group"
      + id                 = (known after apply)
      + image              = "linode/ubuntu20.04"
      + ip_address         = (known after apply)
      + ipv4               = (known after apply)
      + ipv6               = (known after apply)
      + label              = "my-nanode-server"
      + private_ip_address = (known after apply)
      + region             = "us-east"
      + resize_disk        = false
      + root_pass          = (sensitive value)
      + shared_ipv4        = (known after apply)
      + specs              = (known after apply)
      + status             = (known after apply)
      + swap_size          = (known after apply)
      + type               = "nanode-1"
      + watchdog_enabled   = true
    }

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

Terraform will provision the Nanode server on Linode based on your configuration. Wait for Terraform to finish the deployment. Access your new server when the deployment is complete.

Previous Post Next Post