Menu

Writing Terraform Configuration Files

Writing Terraform Configuration Files

Understanding Terraform Configuration Syntax

  • Terraform configurations are written in HashiCorp Configuration Language and saved in `.tf` files. The language uses readable blocks, arguments and expressions to describe infrastructure.
  • Terraform also accepts JSON syntax, but HashiCorp Configuration Language is generally easier to read and maintain.

Understanding Blocks, Arguments, Expressions and Data Types

resource "aws_instance" "web" {
ami           = data.aws_ami.ubuntu.id
instance_type = var.instance_type

tags = {
Name = "terraform-web-server"
}
}

A resource block declares the object Terraform should manage. In the example, resource is the block type, aws_instance is the provider resource type, web is the local resource name, and the lines inside the braces are arguments.

The expression var.instance_type reads an input variable.

Working with Variables, Operators and String Interpolation

  • Input variables make configurations reusable. Declare them with a type and, where appropriate, a description and validation rule.
  • Interpolation is usually written inside quoted strings, such as `"${var.environment}-logs"`; for simple expressions Terraform also accepts direct references without interpolation. Use locals for values derived from other inputs and outputs to expose selected results.
  • Reference them as `var.region`. Operators support arithmetic, comparison and logical expressions.

Terraform Tutorial

HA

Harini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details