HCL Declarative Syntax & Core Concepts
HashiCorp Configuration Language (HCL)
Terraform uses HCL to describe the desired state of infrastructure. HCL is designed to be highly readable for humans while being easy for machines to parse.
Core Blocks
1. Providers
Providers are plugins that allow Terraform to interact with cloud platforms (AWS, Azure, GCP), SaaS platforms, and other APIs.
provider "aws" {
region = "us-west-2"
}2. Resources
Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records.
resource "aws_instance" "web" {
ami = "ami-a1b2c3d4"
instance_type = "t2.micro"
}3. Data Sources
Data sources allow Terraform to use information defined outside of Terraform, defined by another separate Terraform configuration, or modified by functions.
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"] # Canonical
}4. Variables and Outputs
- Input Variables serve as parameters for a Terraform module, allowing users to customize behavior without editing the source.
- Output Values are like return values for a Terraform module, providing a way to extract useful information from your infrastructure.
variable "instance_type" {
type = string
default = "t2.micro"
}
output "instance_ip_addr" {
value = aws_instance.web.private_ip
}