Skip to content
intermediate Phase 10 · Infrastructure as Code

Terraform Fundamentals

Write Terraform configurations with resources, variables, and outputs. Initialize, plan, apply, and destroy infrastructure state.

1h 10m
0 problems
Topic Progress 0%

HCL Syntax: Resources, Data Sources, Variables, Outputs

Terraform uses HashiCorp Configuration Language (HCL), a declarative language designed for infrastructure.

Resources define infrastructure components like EC2 instances, VPCs, and databases. Data sources query existing infrastructure to reference in your configuration. Variables parameterize configurations for reuse. Outputs expose values after apply.

HCL supports expressions, functions, and conditionals. Reference variables with var.name, resource attributes with resource_type.name.attribute, and use locals for computed values.

Init, Plan, Apply, Destroy Workflow

Terraform follows four main commands.

Init initializes the working directory, downloads providers, and configures the backend. Plan previews changes without applying them, showing resources to add, modify, or destroy. Apply executes the planned changes after confirmation. Destroy removes all managed infrastructure.

Always run plan before apply in CI/CD pipelines. Review the plan carefully: red minus signs indicate destruction, green plus signs indicate new resources, yellow tilde signs indicate modifications.

State File Management and Backends

Terraform state is a JSON file mapping your configuration to real-world resources. Never commit state files to Git.

Remote backends store state centrally. The S3 backend with DynamoDB locking is the standard for AWS. State locking prevents concurrent modifications when multiple people work on the same infrastructure.

Workspaces manage multiple state files for the same configuration, useful for environment separation. Use terraform state commands to list, show, move, and remove resources from state.

Terraform Expressions and Functions

Terraform provides expressions and built-in functions for dynamic configurations.

Conditional expressions use the ternary operator for environment-specific values. For expressions transform lists and maps. Built-in functions handle string manipulation, numeric operations, collection operations, and file templating.

Dynamic blocks generate nested blocks from lists, reducing repetition. Splat expressions simplify accessing attributes from a list of resources.

Quiz

1. What is the purpose of terraform plan?

Question 1 options

2. Why use a remote backend for Terraform state?

Question 2 options

3. What does a data source block do?

Question 3 options

Flashcards

Question

What is Terraform state?

Answer

A JSON file mapping configuration to real-world resources, recording current infrastructure state.

Question

What is terraform init?

Answer

Initializes the working directory, downloads provider plugins, and configures the backend.

Question

What is state locking?

Answer

A mechanism using DynamoDB that prevents concurrent modifications to the same state file.

Question

Resource vs data source blocks?

Answer

Resources create infrastructure. Data sources query existing infrastructure for references.

Revision Notes

Key Takeaways

  • 1. HCL uses resource, data, variable, output blocks
  • 2. Always run init, plan, then apply
  • 3. Use S3 backends with DynamoDB locking for teams
  • 4. Never commit state files to Git

Interview Tips

  • Walk through the Terraform workflow from init to apply
  • Explain why state locking is critical for teams
  • Describe structuring Terraform for multiple environments

Cheat Sheet

Blocks: resource (create), data (read), variable (input), output (export), locals (computed). Workflow: init -> plan -> apply -> destroy. State: S3 backend, DynamoDB locking, never commit to Git.