Bird
Raised Fist0
Terraformcloud~5 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Why is HCL (HashiCorp Configuration Language) important for Terraform users?
easy
A. It makes cloud infrastructure code easy to read and write.
B. It is a programming language for building mobile apps.
C. It replaces the need for cloud providers.
D. It is used only for monitoring cloud resources.

Solution

  1. Step 1: Understand HCL's purpose

    HCL is designed to be simple and clear for writing infrastructure code.
  2. Step 2: Compare options

    Options A, B, and C describe unrelated uses or incorrect roles of HCL.
  3. Final Answer:

    It makes cloud infrastructure code easy to read and write. -> Option A
  4. Quick Check:

    HCL simplifies infrastructure code = D [OK]
Hint: HCL is for clear cloud setup code, not app or monitoring [OK]
Common Mistakes:
  • Confusing HCL with general programming languages
  • Thinking HCL replaces cloud providers
  • Assuming HCL is for monitoring only
2. Which of the following is the correct way to define a variable in HCL for Terraform?
easy
A. var region = "us-west-1"
B. define region = "us-west-1"
C. variable "region" { default = "us-west-1" }
D. set variable region to "us-west-1"

Solution

  1. Step 1: Recall HCL variable syntax

    In HCL, variables are declared using the keyword 'variable' followed by the name in quotes and a block with attributes.
  2. Step 2: Check each option

    variable "region" { default = "us-west-1" } matches the correct HCL syntax. Options A, C, and D use invalid or non-HCL syntax.
  3. Final Answer:

    variable "region" { default = "us-west-1" } -> Option C
  4. Quick Check:

    HCL variable uses 'variable' block = B [OK]
Hint: HCL variables use 'variable' blocks with quoted names [OK]
Common Mistakes:
  • Using programming language variable syntax
  • Omitting quotes around variable names
  • Writing variables as simple assignments
3. Given this HCL snippet:
resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

What does this code do when applied with Terraform?
medium
A. Updates the AWS CLI configuration.
B. Creates an AWS EC2 instance with the specified AMI and type.
C. Deletes an existing AWS EC2 instance.
D. Creates a storage bucket in AWS.

Solution

  1. Step 1: Identify resource type and name

    The code defines a resource of type 'aws_instance' named 'web', which means an EC2 instance.
  2. Step 2: Understand resource attributes

    The 'ami' and 'instance_type' specify the machine image and size for the EC2 instance.
  3. Final Answer:

    Creates an AWS EC2 instance with the specified AMI and type. -> Option B
  4. Quick Check:

    Resource block creates EC2 instance = C [OK]
Hint: Resource blocks define cloud resources to create [OK]
Common Mistakes:
  • Confusing resource creation with deletion
  • Mixing resource types (instance vs bucket)
  • Assuming it changes CLI settings
4. Identify the error in this HCL snippet:
resource "aws_s3_bucket" "mybucket" {
  bucket = my-bucket-name
  acl    = "private"
}
medium
A. The bucket name should be in quotes.
B. The resource type is incorrect.
C. The acl value must be a number.
D. The resource name cannot have uppercase letters.

Solution

  1. Step 1: Check bucket attribute syntax

    In HCL, string values must be enclosed in quotes. 'my-bucket-name' is missing quotes.
  2. Step 2: Verify other attributes

    The resource type and acl value are correct; acl is a string and resource name is valid.
  3. Final Answer:

    The bucket name should be in quotes. -> Option A
  4. Quick Check:

    String values need quotes = A [OK]
Hint: Always quote string values in HCL [OK]
Common Mistakes:
  • Forgetting quotes around strings
  • Assuming acl needs a number
  • Thinking resource names have case restrictions
5. You want to share your Terraform cloud setup with your team so everyone can understand and update it easily. Why is using HCL beneficial in this case?
hard
A. HCL runs Terraform commands faster than other languages.
B. HCL automatically fixes errors without user input.
C. HCL encrypts your cloud credentials for security.
D. HCL is human-readable and structured, making collaboration simple.

Solution

  1. Step 1: Understand collaboration needs

    Teams need clear, easy-to-read code to share and update cloud setups without confusion.
  2. Step 2: Evaluate HCL features

    HCL is designed to be human-readable and structured, which helps teams understand and maintain code together.
  3. Step 3: Eliminate incorrect options

    HCL does not auto-fix errors, encrypt credentials, or affect command speed directly.
  4. Final Answer:

    HCL is human-readable and structured, making collaboration simple. -> Option D
  5. Quick Check:

    Readable code helps team collaboration = A [OK]
Hint: Readable code means easier team work [OK]
Common Mistakes:
  • Thinking HCL auto-corrects errors
  • Confusing HCL with security tools
  • Assuming HCL affects Terraform speed