Definition
Terraform - Complete Concepts, Components, and Objects with Broad Definitions
Terraform is a widely used Infrastructure as Code (IaC) tool developed by HashiCorp that allows users to define, provision, and manage infrastructure using a declarative configuration language called HashiCorp Configuration Language (HCL). It supports multiple cloud providers like AWS, Azure, GCP, Kubernetes, and even on-premises data centers.
1. Core Concepts of Terraform
1.1. Infrastructure as Code (IaC)
Terraform enables defining infrastructure in code, ensuring repeatability, consistency, and automation. This helps avoid manual configurations and human errors.
1.2. Declarative vs Imperative Approach
Declarative (Terraform’s approach): You define the desired state, and Terraform figures out how to reach that state.
Imperative (e.g., Bash, Python scripts): You provide step-by-step instructions to modify infrastructure.
1.3. Desired State Management
Terraform compares the actual infrastructure state with the desired configuration and makes necessary changes to bring the infrastructure to the expected state.
2. Terraform Components & Objects
2.1. Terraform Configuration Files (.tf files)
.tf files)Terraform configurations are written in HCL (HashiCorp Configuration Language) or JSON. The main files include:
main.tf: Defines the core infrastructure resources.variables.tf: Stores input variables for dynamic configurations.outputs.tf: Stores output values for references.terraform.tfvars: Stores default values for variables.
2.2. Providers
A provider is a plugin that allows Terraform to interact with cloud services (AWS, Azure, GCP, Kubernetes, etc.).
👉 Example (AWS Provider)
Terraform downloads required providers using:
2.3. Resources
A resource is the fundamental building block of Terraform. It represents a component of infrastructure such as an EC2 instance, S3 bucket, database, VPC, Kubernetes cluster, etc.
👉 Example (Creating an AWS EC2 instance)
2.4. Data Sources
Data sources fetch external information from existing resources without modifying them.
👉 Example (Fetching the latest AMI ID)
2.5. Variables
Variables allow dynamic configurations in Terraform instead of hardcoding values.
📌 Types of Variables:
Input Variables (
var.): User-defined values passed into Terraform configurations.Environment Variables (
TF_VAR_): Set system-wide for Terraform execution.
👉 Example (Defining and using a variable)
2.6. Outputs
Outputs allow Terraform to return values after execution, making them available for reference.
👉 Example (Displaying the public IP of an EC2 instance)
2.7. State File (terraform.tfstate)
terraform.tfstate)Terraform maintains a state file to keep track of infrastructure.
📌 Important Concepts:
terraform.tfstate: Stores infrastructure details and current states.State Locking: Prevents conflicts when multiple users apply changes.
Remote State Storage: Storing state files in S3, Azure Blob, or Terraform Cloud ensures team collaboration.
👉 Example (Storing Terraform state in S3)
2.8. Terraform Lifecycle Hooks
Terraform provides lifecycle hooks to control how resources are created and destroyed.
📌 Common Lifecycle Options:
create_before_destroy: Ensures a new resource is created before deleting the old one.prevent_destroy: Prevents accidental deletion of a resource.ignore_changes: Ignores specific attribute changes.
👉 Example (Lifecycle Block for EC2 instance)
2.9. Provisioners
A Provisioner in Terraform is used to execute scripts or commands on a resource after it is created or modified. It is typically used for bootstrapping resources, configuring applications, or handling post-deployment tasks that Terraform alone cannot manage.
📌 Types of Provisioners:
Remote (SSH/WinRM): Runs scripts inside the instance.
Local (Exec): Runs commands from the local system.
👉 Example (Running a shell script on an EC2 instance)
2.10. Terraform Modules
Modules allow reusing configurations across multiple environments.
📌 Module Directory Structure:
👉 Example (Calling a Module in Terraform)
2.11. Terraform Workspaces
A Terraform workspace is an isolated environment that allows you to manage multiple states within the same Terraform configuration. It helps you handle different environments (like dev, staging, prod) without duplicating code.
📌 Common Commands:
2.12. Terraform Commands
terraform init
Initializes the working directory and downloads providers.
terraform plan
Shows what Terraform will change before applying it.
terraform apply
Creates or updates the infrastructure.
terraform destroy
Deletes all managed infrastructure.
terraform show
Displays current infrastructure state.
2.13. Terraform Cloud & Terraform Enterprise
Terraform Cloud is a managed service for storing remote states, managing execution, and providing collaboration tools.
📌 Features:
Remote state management
Policy enforcement (Sentinel)
Team collaboration
Auto-scaling
Conclusion
This is a comprehensive list of all Terraform concepts that might be asked in interviews.
Pro Tips for Interviews
💡 Revise key Terraform components (Providers, Modules, State Management, Variables). 💡 Practice Terraform syntax & write small infrastructure projects. 💡 Be ready to discuss a real-world Terraform setup from your experience.
Would you like Terraform scenario-based questions for interview practice? 🚀
State File
Keeps track of the real-world infrastructure so Terraform can detect drift and changes.
Provider
A plugin that lets Terraform talk to cloud services like AWS, GCP, or tools like GitHub and Datadog.
Module
A reusable group of Terraform files that organizes and simplifies infrastructure code.
Workspace
Lets you manage multiple environments (dev, staging, prod) using the same code, but with different state files.
Provisioner
Runs scripts or commands on a resource after it is created or changed, usually for setup tasks.
Resource Block
A block that defines a piece of infrastructure (like an EC2 instance or S3 bucket). It's the main building block in Terraform.
Lifecycle
A special block that controls how Terraform manages resources — like preventing destruction or creating before destroying.
Output
Displays values from your Terraform configuration after apply — useful for things like IP addresses or URLs.
Backend
Controls where and how Terraform stores its state (e.g., local, S3, or remote). Important for team collaboration.
Terraform Init
Initializes your configuration, downloads providers, and prepares everything for use.
Terraform Plan
A command that shows what Terraform will change without actually applying it. Useful for reviewing changes.
Terraform Apply
The command that actually makes the changes described in your configuration.
Terraform Destroy
Destroys all infrastructure managed by terraform.
Last updated