Lewislbr

Set up a virtual machine in DigitalOcean with Terraform

Setting up a virtual machine from scratch can seem like a daunting task, and depending on the provider, it can be. Personally, I find DigitalOcean to be the simplest and leanest solution out there, as I prefer to have the bare metal thing rather than a full-featured solution with too much going on.

As a developer, I just set up a virtual machine from time to time to run some personal project or to test something, so it's quite easy to forget exactly the steps I took every time I had to create one. An awesome solution to automate this is Terraform, a tool to manage infrastructure with code.

Installation

To get started, create a DigitalOcean account if you don't have one and download or install Terraform with your preferred package manager. I'm using Homebrew myself like:

brew install terraform

Configuration

Now we can create a terraform.tfvars file to store the DigitalOcean secrets that will be used by Terraform:

do_api_token = "[digitalocean_api_token]"
do_ssh_key_fingerprint = "[digitalocean_ssh_key_fingerprint]"

Then we can create the configuration file for Terraform named main.tf:

terraform {
  required_providers {
    digitalocean = {
      source = "digitalocean/digitalocean"
      version = "~> 2.7.0"
    }
  }
}

variable "do_api_token" {}
variable "do_ssh_key_fingerprint" {}

provider "digitalocean" {
  token = var.do_api_token
}

resource "digitalocean_droplet" "droplet_name" {
  image = "debian-10-x64"
  ipv6 = true
  monitoring = true
  name = "droplet_name"
  private_networking = false
  region = "fra1"
  size = "s-1vcpu-1gb"
  ssh_keys = [
    var.do_ssh_key_fingerprint
  ]
}

output "public_ipv4" {
  value = digitalocean_droplet.droplet_name.ipv4_address
}

In this file we define the provider we'll use and the secrets it uses, the resource (the thing we want to create) with its base configuration, and an optional output value we want after completion, in this case the public IP of the virtual machine so we can then SSH into it.

A few things to take into account:

  • Find the latest version of the provider here.

  • Find a reference for the types of resources DigitalOcean offers here.

  • Note that droplet_name can be changed to any name.

Commands

With this, we can initialize Terraform to install the required files:

terraform init

And then we can run it to create a list of the actions to perform:

terraform plan

This command will also save the current state, and future changes will be added on top of it, so it's important to run it often.

If everything looks fine, we are ready to execute the actions and create the virtual machine:

terraform apply

This should result in a success message and the virtual machine's IP address displayed in the terminal, which means the virtual machine is up and running and available to the internet.

After working on it, if we don't need the virtual machine anymore, we can remove it with the following command:

terraform destroy

This will undo anything created by Terraform, returning to the inital state.

So now, whenever we need a virtual machine, we can just run the plan and apply commands to create it. Wonderful!



If you're using dark mode, do you like the code blocks's theme? I have it available for VS Code, feel free to check it.