πŸš€ Understanding Terraform Syntax & What is Code — A Beginner’s Guide

 By Raees Qazi | DevOps Engineer | Learner | Mentor | Creator

πŸ‘‹ Introduction

Today, let’s learn about Terraform syntax and understand what code actually is — in a way that’s simple and easy to relate to.

As a user, I understand both English and Urdu. But machines don’t understand English or Urdu — machines only understand binary language (like 01010101).

Now, machines are dumb by default. They don’t know what to do until we tell them clearly how to do it. That’s why we need to learn how to communicate with machines.

Let me give you an example:

When someone texts you “TC”, you might get confused at first. But with time, you understand that TC = Take Care.
Similarly:

  • GM = Good Morning
  • GN = Good Night

These are just short codes. As users, we learn them over time. The same concept applies to code. We write simple instructions using a defined format, and machines follow them.

This defined format is called syntax.

Press enter or click to view image in full size

πŸ“œ Terraform Syntax (HCL)

Terraform uses a special language called HCL (HashiCorp Configuration Language). Here’s the basic syntax:

<block> <parameters> {
arguments
}

Let’s break this down step by step.

🧱 1. Block

block tells Terraform what you are creating.

For example, if you are launching an EC2 machine in AWS, that EC2 instance is a resource, and you define it in a block.

There are different types of blocks in Terraform:

  • resource – to create a resource
  • variable – to define input values
  • output – to show output after applying
  • data – to fetch data from other sources

All of these blocks start with the <block> syntax in Terraform.

🏷️ 2. Parameters

Inside a block, we define parameters, which include:

  • Type of resource (e.g. local_file)
  • Name of the resource (e.g. my_file)
resource "local_file" "my_file" 

This means:

I’m creating a resource of type local_file and naming it my_file.

These parameters help Terraform understand what and which resource we’re talking about.

πŸ“„ 3. Arguments

Inside the block, we write arguments. These are like details or metadata of the resource.

Example:

filename = "demo.txt"
content = "this is a file, this file is auto created by terraform"
  • filename is the name of the file you want to create.
  • content is the text you want to put inside that file.

These are instructions written by the user.
Once you run terraform apply, Terraform creates the file. At that point, these arguments become known as attributes (because they now exist in the system).

⚙️ How Terraform Works (Simple Flow)

Let’s compare this to using a machine:

  1. First, you turn on the machine.
  2. Then, you write instructions (code).
  3. After that, the machine does the work for you.

Same thing in Terraform:

Terraform Lifecycle:

  1. Write Code in a .tf file
  2. Initialize Terraform
  3. Plan — See what changes will happen
  4. Apply — Actually perform the changes

πŸ§ͺ Hands-On Example — Creating a File

Let’s run a simple Terraform example to create a file.

Step 1: Create a Terraform file

Open terminal and write:

vim main.tf

Paste this code:

resource "local_file" "my_file" {
filename = "demo.txt"
content = "this is a file, this file is auto created by terraform"
}

Step 2: Run Terraform Commands

Now run these commands one by one:

terraform init     # Step 1 – Initialize Terraform
terraform plan # Step 2 – Preview what will be created
terraform apply # Step 3 – Actually create the file

After this, a new file named demo.txt will be created automatically in your system with the content inside.

✅ Summary

  • Machines are dumb — they only follow instructions.
  • We write code in a specific format (syntax) so machines can understand.
  • Terraform uses blocks, parameters, and arguments in its syntax.
  • You can create resources (like files, servers, etc.) using simple Terraform code.
  • The Terraform process is: Write → Init → Plan → Apply

πŸ“’ Final Note

Please try this example on your own system. Practice it a few times until it becomes clear.

Learning DevOps is not just about tools, it’s about understanding how to make machines work for you!

If this helped you, share it with others so they can also start their journey into Terraform and Infrastructure as Code.

Comments

Popular posts from this blog

πŸ“˜ Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator