How did I end up using Terraform ?

Some months ago, being the simple-minded rat that I am, I had never heard of Terraform or even Infrastructure as Code (IaC). The reason was simple: the projects I was building in the cloud didn’t need top-notch infrastructure. A few AWS EC2 instances did the trick, so I set everything up by hand or with the AWS CLI.

But things changed when a client approached me with a project that was already up and running — and guess what? I stumbled upon a shared-infra directory filled with .tf files and a mysterious terragrunt.hcl. A full IaC setup already in place.

Quick note: Terragrunt is a wrapper for Terraform. Think of it as a DRY-enforcer (Don’t Repeat Yourself 😉) that simplifies multi-environment deployments and keeps your infrastructure code clean.

So here I was, forced to learn Terraform to be able to contribute to this project. This article is both a recap of what I learned and a way for me to test whether I really understand the tool well enough to explain it to you.


But what even is Terraform?

“Terraform is an infrastructure as code tool that lets you build, change, and version infrastructure safely and efficiently.”

Basically, this means you can declare in your project what resources to use — servers, databases, networks — and from which cloud providers (AWS, DigitalOcean, GCP, etc.). Then Terraform takes care of provisioning everything for you.

It’s especially useful when scaling up or down, or when you need a consistent and documented view of your infrastructure. Plus, having your infra defined alongside your code is a game changer for collaboration and versioning.


Using Terraform with DigitalOcean

Let’s get our hands dirty. We’ll deploy a simple infrastructure using Terraform on DigitalOcean Specifically, we’ll create a droplet (a lightweight VPS) as an example.

We’ll use the official Learn Terraform GitHub repo as a base.
(And by the way, don’t forget to claim your €200 free credit if you’re a GitHub student.)

Step 1: Installing Terraform on Linux

Here’s how to install Terraform the clean way:

wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \ https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list sudo apt update && sudo apt install terraform

To verify that it worked, run:

terraform -version

You should see something like Terraform v1.x.x.

Step 2: Configure the provider

Terraform works with providers — plugins that let it talk to specific services. Each provider maps to the API of the platform you’re using.

For DigitalOcean, you’ll need a Personal Access Token.
You can generate one here: DigitalOcean API Tokens

Then export it as an environment variable: export DO_PAT="your_personal_access_token"

Now, create a file named provider.tf:

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

variable "do_token" {}
variable "pvt_key" {}

provider "digitalocean" {
  token = var.do_token
}

data "digitalocean_ssh_key" "terraform" {
  name = "terraform"
}

Step 3: Initialize and deploy

Initialize Terraform in your working directory:

terraform init

![[Pasted image 20250423144056.png]]

This downloads all required provider plugins and sets up your project for deployment.

Then, to preview what Terraform will do (without actually creating anything yet):

terraform plan

If everything looks good, you can apply it:

terraform apply

Terraform will show you the plan again and ask for confirmation. Type yes, and in a few seconds your DigitalOcean droplet will be live.

You can verify everything created by running:

terraform show

Cleaning up

When you’re done testing, destroy your resources with:

terraform destroy

And if you ever get stuck, remember that Terraform keeps track of everything in the terraform.tfstate file.

Deleting that file will “reset” Terraform’s memory of what it deployed — but be careful: this doesn’t delete the actual resources. You’ll need to remove them manually in your provider’s dashboard.

Final Thoughts

At first, Terraform might look intimidating — blocks of HCL code, providers, states, and plans everywhere. But once you get the hang of it, it’s actually an elegant way to manage infrastructure consistently and transparently.