0
0
Terraformcloud~5 mins

Why HCL matters as Terraform's language - Why It Works

Choose your learning style9 modes available
Introduction
Terraform uses a special language called HCL to describe infrastructure. This language makes it easy to write, read, and manage cloud resources without confusion.
When you want to create cloud resources with clear and simple code.
When you need to share infrastructure code with teammates who may not be programmers.
When you want to avoid complex or hard-to-read configuration files.
When you want to use a language designed specifically for infrastructure tasks.
When you want to easily understand and update your cloud setup over time.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example_bucket" {
  bucket = "example-terraform-bucket-12345"
  acl    = "private"
}

This file uses HCL to tell Terraform to create an AWS S3 bucket.

terraform block sets the Terraform version.

provider block configures AWS region.

resource block defines the S3 bucket with a name and access control.

Commands
This command sets up Terraform in the current folder. It downloads necessary plugins and prepares the environment to work with the configuration.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will do before making any changes. It helps you check if the configuration matches your expectations.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.example_bucket will be created + resource "aws_s3_bucket" "example_bucket" { + acl = "private" + bucket = "example-terraform-bucket-12345" + id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the changes to create the resources defined in the configuration. The flag skips the manual approval step for faster execution.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Creating... aws_s3_bucket.example_bucket: Creation complete after 2s [id=example-terraform-bucket-12345] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply step without asking for confirmation
This command removes all resources created by Terraform in this configuration. The flag skips confirmation to quickly clean up.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Destroying... aws_s3_bucket.example_bucket: Destruction complete after 1s Destroy complete! Resources: 1 destroyed.
-auto-approve - Automatically approve the destroy step without asking for confirmation
Key Concept

If you remember nothing else, remember: HCL makes infrastructure code easy to read, write, and share, helping you manage cloud resources clearly and safely.

Common Mistakes
Writing Terraform configuration in JSON instead of HCL.
JSON is harder to read and write for infrastructure, making mistakes more likely and collaboration harder.
Use HCL syntax for Terraform files to keep configurations clear and simple.
Not running 'terraform init' before other commands.
Terraform needs to download plugins and prepare the environment; skipping init causes errors.
Always run 'terraform init' first in a new or changed configuration folder.
Applying changes without reviewing the plan.
You might create or destroy resources unintentionally, causing downtime or extra costs.
Run 'terraform plan' to review changes before 'terraform apply'.
Summary
Terraform uses HCL to write clear and simple infrastructure code.
'terraform init' prepares the environment by downloading needed plugins.
'terraform plan' shows what changes will happen before applying them.
'terraform apply' creates or updates resources as defined in the code.