Terraform Language Basics
terraform-learning Hands-on Terraform configs, modules, and practice projects on GitHub.Basics of Terraform Language
Every Terraform configuration is built from a simple pattern: a block contains arguments, and you reference attributes from resources you’ve created. Understanding these three concepts is the foundation for reading and writing any .tf file.
<block_type> "<parameters>" {
<arguments>
}
When building infrastructure, the block type tells Terraform what kind of thing you’re defining — resource to create infrastructure, output to expose values, variable to accept inputs.
- A block is a container that groups related Terraform configuration.
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t3.micro"
}
resource "aws_instance" "web"
│ │ │
│ │ └── Local resource name
│ └───────────── Resource type
└──────────────────────── Block type
- An argument assigns a value to a name inside a block.
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t3.micro"
}
ami = "ami-123456"
instance_type = "t3.micro"
- In practical Terraform usage, attribute often refers to a piece of data belonging to an object/resource, especially values you can read/reference.
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t3.micro"
}
output "server_ip" {
value = aws_instance.web.public_ip
}
Here :
aws_instance.web.public_ip
│ │ │
│ │ └── Attribute
│ └────────── Resource name
└─────────────────── Resource type
Putting them together:
resource "aws_instance" "backend" {
ami = "ami-123456"
instance_type = "t3.micro"
tags = {
Name = "backend-server"
}
}
output "backend_ip" {
value = aws_instance.backend.public_ip
}
resource "aws_instance" "backend" { ← BLOCK
ami = "ami-123456" ← ARGUMENT
instance_type = "t3.micro" ← ARGUMENT
tags = { ← ARGUMENT
Name = "backend-server"
}
}
output "backend_ip" { ← BLOCK
value = aws_instance.backend.public_ip
│ │ │
resource name attribute
}