DEV Community

Cover image for Terraform for Beginners: Build Cloud Infrastructure by Writing Simple Code
MICHAEL ABAJEMITO
MICHAEL ABAJEMITO

Posted on

Terraform for Beginners: Build Cloud Infrastructure by Writing Simple Code

A short story to start

Imagine you spent a whole afternoon clicking around the Azure or AWS website to set up a server, a network, a database and a few security rules. It works. Well done.

Now your manager says: "Great, can you build exactly the same thing again for testing, and another copy in Europe?" Can you remember every click? Every setting? Every small box you ticked?

Most people cannot. That is the problem Terraform solves. Instead of clicking, you write down what you want in a simple text file. Terraform reads the file and builds it for you, the same way, every time.

1. What is Terraform, in one sentence?

Terraform is a tool that turns a simple text file into real cloud infrastructure.

This idea is called Infrastructure as Code, or IaC for short. Your servers, networks and databases are described in code, just like an app.

A good way to picture it is a building plan. An architect does not build a house by shouting instructions to workers one by one. She draws a plan. Builders follow the plan. If you want ten identical houses, you use the same plan ten times. Terraform files are your plan.

Terraform is made by a company called HashiCorp. It works with almost every cloud: AWS, Microsoft Azure, Google Cloud and hundreds of other services. You may also hear about OpenTofu, a free open source version that works in almost the same way. Everything you learn here applies to both.

2. How does Terraform work?

Figure 1: You write the code, Terraform reads it, the provider talks to the cloud.
Figure 1: You write the code, Terraform reads it, the provider talks to the cloud.

There are only a few moving parts:

  1. You write files ending in .tf that describe what you want.
  2. Terraform reads those files and works out what needs to be created, changed or deleted.
  3. A provider is a plugin that knows how to talk to one platform. There is a provider for AWS, one for Azure, one for Google Cloud, and many more. Think of it as a translator.
  4. The real cloud ends up with the servers, networks and storage you asked for.

The most important idea: you describe what you want, not how to build it step by step. Terraform works out the steps.

3. The words you will hear (made simple)

Word What it means in plain English
Provider The plugin that talks to a platform, such as aws or azurerm.
Resource One thing you want to create, such as a server, a network or a storage bucket.
Variable A value you can change without editing the main code, such as a region or a name.
Output A value Terraform prints at the end, such as a website address or a server IP.
State Terraform's memory of what it has already built.
Plan A preview of the changes Terraform is about to make.
Module A reusable bundle of Terraform code, like a Lego set you can use again and again.

4. Install Terraform

Go to the official Terraform website from HashiCorp, download the version for your computer (Windows, Mac or Linux) and follow the install steps. On a Mac you can also use Homebrew, and on Windows you can use Chocolatey or winget.

Then open a terminal and type:

terraform -version
Enter fullscreen mode Exit fullscreen mode

If you see a version number, you are ready. I also recommend installing Visual Studio Code with the official HashiCorp Terraform extension. It colours your code and spots mistakes as you type.

5. Your first project (no cloud account needed)

The best way to learn Terraform without any risk of a cloud bill is to start with something local. We will ask Terraform to create a simple text file on your own computer. The steps are exactly the same as for a real server in the cloud.

Step 1: Create a folder and a file

Make a new folder called my-first-terraform and inside it create a file called main.tf with this code:

resource "local_file" "hello" {
  filename = "hello.txt"
  content  = "Hello from Terraform!"
}
Enter fullscreen mode Exit fullscreen mode

Let us read it slowly:

  • resource says "I want to create something".
  • "local_file" is the type of thing: a file on this computer.
  • "hello" is the nickname we give it inside our code.
  • Inside the curly brackets are the settings: the file name and what to write in it.

Step 2: Run the four magic commands

Figure 2: init, plan, apply and destroy. You will use these every single day.
Figure 2: init, plan, apply and destroy. You will use these every single day.

Open a terminal in your folder and run:

terraform init
Enter fullscreen mode Exit fullscreen mode

init gets everything ready. It notices you used local_file and downloads the provider that knows how to create files.

terraform plan
Enter fullscreen mode Exit fullscreen mode

plan shows a preview. You should see something like "Plan: 1 to add, 0 to change, 0 to destroy". Nothing has been created yet. This is your chance to check before anything happens.

terraform apply
Enter fullscreen mode Exit fullscreen mode

apply does the real work. Terraform shows the plan again and asks you to type yes. Type it and press Enter. Now look in your folder: there is a new file called hello.txt with your message inside. You just used Infrastructure as Code.

Step 3: Make a change

Change the content line to say "Hello again from Terraform!" and run terraform plan. Terraform notices the difference and tells you it will replace the file. Run terraform apply, type yes, and open the file again. It has been updated. You did not tell Terraform how to update it. You only changed what you wanted.

Step 4: Clean up

terraform destroy
Enter fullscreen mode Exit fullscreen mode

destroy deletes everything Terraform created in this folder. Type yes and hello.txt disappears. In the real cloud this is your best friend: one command and everything you built for practice is gone, so you are not paying for it.

6. The state file: Terraform's memory

Figure 3: Terraform compares what you want, what it remembers and what really exists.
Figure 3: Terraform compares what you want, what it remembers and what really exists.

After you ran apply, you may have noticed a new file called terraform.tfstate. This is Terraform's memory. It records what Terraform has built, so next time it knows what already exists and what needs to change.

A few simple rules for the state file:

  • Never edit it by hand. Let Terraform manage it.
  • Never share it publicly or upload it to GitHub. It can contain passwords and private details.
  • In a team, store it in a shared, safe place such as an Azure Storage account, an AWS S3 bucket or HCP Terraform. This is called remote state, and it stops two people changing the same thing at the same time.

7. Variables and outputs: making your code flexible

Hard coding values works, but variables let you reuse the same code for different situations. Add this to your main.tf:

variable "message" {
  default = "Hello from Terraform!"
}

resource "local_file" "hello" {
  filename = "hello.txt"
  content  = var.message
}

output "file_name" {
  value = local_file.hello.filename
}
Enter fullscreen mode Exit fullscreen mode
  • The variable block creates a value called message with a default.
  • var.message uses that value inside the resource.
  • The output block prints the file name when apply finishes.

Now you can change the message without touching the resource, for example: terraform apply -var="message=Good morning".

8. A small real cloud example

When you are ready for the cloud, the pattern is exactly the same. Here is how you would create a storage bucket on AWS:

provider "aws" {
  region = "eu-west-1"
}

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-first-terraform-bucket-12345"
}
Enter fullscreen mode Exit fullscreen mode

Same commands: init, plan, apply. Same clean up: destroy. Before you try it, create a free tier account, set a budget alert, and log in with the AWS command line tool so Terraform can use your account. Bucket names must be unique across the whole world, so change the numbers at the end.

9. Good habits from day one

  1. Always read the plan before you type yes. It tells you exactly what will be created, changed or deleted.
  2. Keep your .tf files in Git, so you have a history of every change.
  3. Add a .gitignore file that keeps out terraform.tfstate, the .terraform folder and any files with secrets.
  4. Run terraform fmt to tidy your code and terraform validate to catch mistakes early.
  5. Make small changes and apply often, instead of one giant change.
  6. Destroy practice resources as soon as you finish, so you do not get a surprise bill.

10. Common beginner mistakes

  • Typing yes without reading the plan, and deleting something important.
  • Uploading the state file or secret keys to a public GitHub repository.
  • Changing things by hand in the cloud website after Terraform built them. Terraform will get confused, or undo your changes next time.
  • Forgetting to run terraform init after adding a new provider.
  • Trying to learn modules, workspaces and remote state on day one. Master the basics first.

11. A simple learning path

Stage What to do
Week 1 Install Terraform and complete the local_file project in this guide.
Week 2 Learn variables, outputs and terraform fmt and validate.
Week 3 Create one small real cloud resource, then destroy it.
Week 4 Build a small network and a virtual machine, and keep your code on GitHub.
After that Learn remote state, modules, and running Terraform inside a CI/CD pipeline.

Final words

Terraform can look scary at first, but the core idea is simple: write down what you want, check the plan, then let Terraform build it. The same file can build the same setup again tomorrow, next month or in another country.

Your challenge this week: create the hello.txt file with Terraform, change it, and destroy it. Those three commands, plan, apply and destroy, are the heart of everything you will ever do with Terraform.

Happy building!

Top comments (0)