Terraform Syntax With Examples (original) (raw)
Last Updated : 30 Apr, 2026
Terraform Uses HashiCorp Configuration Language (HCL) which help engineers in designing repeatable and production-ready systems with ease.
- Terraform Eliminates manual provisioning through automated infrastructure management.
- Uses HCL, a human-readable language to define the desired infrastructure state.
- Supports multi-cloud and on-premises deployments with consistent workflows.
- Enables scalable, reliable, and version-controlled infrastructure.
Language Used In Terraform
HashiCorp Configuration Language (HCL) is the DSL (Domain Specific Language) that Terraform uses to create infrastructure resources. HCL is a human-readable language, designed to be easy to understand while still being expressive.
- It is Declarative rather than procedural.
- Supports complex data structures.
- Compatible with JSON.
Terraform Syntax With Examples
Terraform syntax is built around blocks, arguments, and expressions that define infrastructure resources and their configurations. Terraform follows a declarative model, meaning you specify what your infrastructure should look like, and Terraform determines how to achieve that state by building a dependency graph and executing changes in the correct order.
1. Terraform Block Syntax
The terraform block defines project-level settings such as required providers and Terraform version constraints.
terraform {
required_version = ">= 1.5.0"
required_providers {aws = {
source = "hashicorp/aws"
version = "~> 5.0"}
}
}
**2. Terraform Resource Block **Syntax
The fundamental building block in Terraform is a resource block. It refers to a single piece of infrastructure that you want to control. Here is an illustration of how to create an AWS EC2 instance:
resource "aws_instance" "gfg-ec2" {
ami = "ami-0c94855ba95c286c99"
instance_type = "t2.micro"
}
In the above code we have used the resource type as "aws_instance" and given it the name "gfg-ec2" and specified the AMI ID and the type of instance we need for it.
**3. Terraform Variable Declaration Syntax
We are able to parameterize our configurations in Terraform with the help of variables, which can make our code much more reusable and adaptable. For example:
variable "region" {
description = "AWS region where the resources will be deployed."
type = string
default = "us-east-1"
}
In the above example, we have created a variable region with a default value of "us-east-1".
**4. Terraform Output Values Syntax
You can extract data from your infrastructure and send it to other configurations or use it as a reference by using Terraform's output values. For example:
output "gfg_instance_ip" {
value = aws_instance.example.public_ip
description = "Public IP of the EC2 instance recently deployed."
}
In the above code, we are trying to get the public ip of our EC2 instance by creating an output named "instance_ip".
**5. Terraform Modules Syntax
We can encapsulate and reuse setups using Terraform modules. They support the creation of reusable components from our infrastructure code. Here is a usage example for a module
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.0.1"
name = "my-gfg-vpc"
cidr = "10.0.0.0/16"
}
In the above code, we are creating a VPC and for that, we are using the "terraform-aws-modules/vpc/aws" module from the Terraform Registry. Then we are passing the required parameters inside the block. These examples show the basic syntax and applications of Terraform and HCL. Terraform, however, offers a large selection of resource types, providers, and extra tools like data sources, conditional expressions, and loops.