4 min read

Deploy to Linode using Terraform

Deploy a Linode instance with a StackScript
Deploy to Linode using Terraform
The peerless infrastructure deployment tool

Terraform got official support for Linode back in October 2018, so if you aren’t using Terraform to deploy your Linode instances and you’d like to know how, this article is for you! 🚀

And if you didn’t already know, Terraform is a handy little tool used to create and deploy infrastructure, generally in “the cloud”. Think of it like a cloud-agnostic AWS CloudFormation or Azure Templates.

The companion video for this article

What do we need to get started?

We’re going to create our very own Linode instance with Terraform, and provision it using a Linode StackScript. In order to do this, you’ll need these things:

  • A Linode API key
  • A recent version of Terraform (0.11.x)
  • A StackScript ID (optional)

Be sure to set your API key as an environment variable, or pass it to Terraform as a run-time variable. Never hard code credentials in Terraform files. Ever.

A recent version of Terraform can be found at the Terraform website. I used version 0.11.10, though later versions will likely be backwards compatible. 🤞

If you want to provision your Linode instance with a StackScript, you’ll need an ID. This is totally optional and can be omitted if desired.

What is a StackScript?

A Linode StackScript is a handy way to provision your Linode instance directly after deployment. It’s slightly different than a CM tool like Ansible in that, the StackScript runs immediately after the deployment, almost as part of the deployment process.

StackScripts are particularly useful if you need something installed or users added (or whatever) as soon as your instance is up and ready, rather than having to run CM scripts each time after you deploy.

The main.tf file

Bust out your favorite code editor and create a new file called main.tf. Inside the file, add a module with the following attributes:module "linode" {
 source           = "github.com/egee-irl/terraform-linode-instance.git"
 authorized_keys  = ["${chomp(file("./id_rsa.pub"))}"]
 type             = "g6-nanode-1"
 region           = "us-central"
 image            = "linode/ubuntu18.04"
 root_pass        = "Terra4ormr0x!"
 stackscript_id   = "394412" # Optional
}

And underneath this module, add an output to print the IP address to the console like so:output "instance_ip" {
 value = "${module.linode.public_ip}"
}

What you should have in your main.tf file is a module followed by an output. I created a demo repository with a reference file if you want to check your work.

Last thing before we deploy — move (or create) your rsa public key into the directory relative to your main.tf, or reference your pub key in the authorized_keys attribute. You’ll need this key to log into your instance after it is deployed.

Deploy the things!

Once your main.tf file is finished and you’ve correctly referenced your public key, its time to deploy your Linode instance!

Open up a terminal relative to your main.tf file and run terraform init. The init command should output a bunch of interesting stuff but the important bits are:* provider.linode: version = "~> 1.5"

Terraform has been successfully initialized!

If Terraform successfully initialized the Linode provider, you can run the apply like so terraform apply . If successful, the output should say: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:

Scroll up in the terminal a bit and review the plan. Terraform always runs a plan before a deploy but if you want to only run a plan, you can run terraform plan instead.

If the Terraform plan looks looks good, type yes , press enter, and watch the magic!

It Deployed — Now What?

Congratulations! You just deployed something with Terraform! The logical next step is to ssh into your new machine. In the terminal, Terraform should have returned the instance_ip. Take that ip address and ssh into the machine like so: ssh root@your_instance_ip.

Another option is to simply destroy the machine. Terraform save everything it does to a terraform.tfstate file relative to the directory where the tf files live. From here, you can run terraform destroy to tear down your instance. Keep in mind that if you delete or otherwise screw up your .tfstate file, you’ll have to destroy the instance manually.

If you deployed your instance with the StackScript ID used in my example, you can navigate to your_instance_ip:9090 and log into a remote administration web interface called Cockpit. Since Cockpit uses a self-signed SSL cert by default, you will see one of those Your connection is not private warning messages. You can dismiss the warning and log into Cockpit using the root user and the password you defined in your tf file.

That’s it?

Yep. Though, I recommend destroying your Linode instance when you are finished working with it.

Terraform is such an amazing tool because it is so darn simple. At the time of this article, the Linode Terraform provider is version 1.5 and supports pretty much every resource Linode offers, including user accounts, so you could handle your systems administration with Terraform too!

If you are interested in the demo I created for the companion video to this article, you can check out the GitHub repo here.

You can also check out the Terrafor module I create to deploy Linode instances here, as well as on the official Terraform module repository here.

And lastly, if you enjoy this article and other articles I write, consider following and supporting me on Ko-fi. Thanks! 👋