π 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.

π 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
A 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 resourcevariable– to define input valuesoutput– to show output after applyingdata– 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"filenameis the name of the file you want to create.contentis 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:
- First, you turn on the machine.
- Then, you write instructions (code).
- After that, the machine does the work for you.
Same thing in Terraform:
Terraform Lifecycle:
- Write Code in a
.tffile - Initialize Terraform
- Plan — See what changes will happen
- 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.tfPaste 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 fileAfter 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
Post a Comment