Terraform
Mini definitions
Absolutely! Let’s create a Terraform cheat sheet in the same “Service/Component: One-line definition” style, including all core concepts, resources, and features relevant for interviews. I’ll make it complete, concise, and ready to speak naturally.
Terraform Cheat Sheet: Core Concepts & Components
Core Concepts
Terraform: Terraform is an infrastructure-as-code tool that allows you to define and provision cloud and on-prem resources using declarative configuration files.
Provider: A Provider is a plugin that allows Terraform to interact with a specific cloud platform or service (e.g., AWS, Azure, GCP).
Resource: A Resource is a specific infrastructure object managed by Terraform, such as an EC2 instance, S3 bucket, or VPC.
Data Source: A Data Source allows Terraform to fetch existing infrastructure details from providers without managing them.
Variable: A Variable is a parameter used to make Terraform configurations flexible and reusable.
Output: An Output is a value exported from Terraform that can be displayed or used by other configurations.
State File: The State File is a local or remote file that stores the current state of your infrastructure managed by Terraform.
Module: A Module is a reusable group of Terraform configurations that can be shared and instantiated multiple times.
Workspace: A Workspace allows multiple independent states within the same configuration, useful for environments like dev, staging, prod.
Commands / Actions
terraform init: Initializes a Terraform configuration and downloads required providers and modules.
terraform plan: Generates an execution plan showing what changes Terraform will make to reach the desired state.
terraform apply: Applies the planned changes to provision or modify infrastructure.
terraform destroy: Destroys all resources managed by Terraform in a configuration.
terraform fmt: Formats Terraform configuration files to standard style.
terraform validate: Validates syntax and structure of Terraform configuration files.
terraform import: Imports existing infrastructure into Terraform state management.
terraform taint: Marks a resource for recreation on the next apply.
terraform state: Commands to inspect and manipulate Terraform state file.
Advanced Concepts
Provisioner: A Provisioner allows execution of scripts or commands on a resource after it’s created, e.g., for configuration management.
Lifecycle Block: Lifecycle Block controls resource behavior, like
create_before_destroyorprevent_destroy.Backend: Backend defines where Terraform state is stored, e.g., locally or remotely in S3, Terraform Cloud, or Consul.
Count & For_Each:
countandfor_eachallow creating multiple resources dynamically.Expressions / Interpolation: Expressions allow referencing variables, attributes, and outputs in configurations.
Dynamic Blocks: Dynamic blocks allow generating nested configuration blocks programmatically.
Best Practices / Concepts Often Asked
Immutable Infrastructure: Terraform encourages creating new resources rather than modifying in-place to reduce errors.
Version Control: All Terraform configurations should be version-controlled using Git.
Remote State Locking: Remote state ensures multiple users don’t overwrite state concurrently.
Modular Design: Use modules to create reusable, maintainable infrastructure components.
Idempotency: Terraform ensures repeated
applyruns produce the same infrastructure state.
If you want, I can now condense all this into a 1-minute “memorize-and-speak” Terraform cheat sheet with short, precise one-line definitions for every core concept — perfect for interviews.
Do you want me to do that?
✅ Problem Statement: Why Do We Need Terraform?
Managing cloud infrastructure manually (via UI or CLI) leads to:
Human errors during provisioning or changes
Lack of version control over infrastructure
Difficulty replicating environments (e.g., staging ≠ production)
Complex dependency management between resources (e.g., VPCs before subnets)
Challenges in collaboration and auditing changes
As infrastructure grows, manual methods become slow, error-prone, and unscalable — especially across teams or multiple environments.
🛠️ Solution: What Terraform Offers
Terraform is an open-source Infrastructure as Code (IaC) tool by HashiCorp that enables safe and predictable infrastructure provisioning.
Key benefits:
Declarative syntax: Define desired state using simple
.tfconfiguration files.Version control: Store infrastructure code in Git for auditing and collaboration.
Idempotency: Ensures consistent results — running the same code multiple times doesn't break anything.
Plan and apply workflow:
terraform plan: Preview changesterraform apply: Safely make those changes
Multi-cloud support: Works with AWS, GCP, Azure, and more — using the same language.
Supports modules and reusability, making complex setups easier to manage and scale.
Enables automation and CI/CD integration for infrastructure deployments.
awesome! here’s a compact-but-complete “Terraform Concepts” cheat sheet in the same spirit as your Kubernetes notes—each concept has a quick purpose line + a crisp example block + a few extra nuggets (like your kube-proxy round-robin note).
Terraform Architecture & Core Flow
What Terraform is: Declarative IaC tool. You describe desired state in HCL; Terraform builds a dependency graph and converges infra to match it.
Core workflow: init → plan → apply → destroy (with state stored locally or remotely).
Planner & Graph:
Builds a graph from references (
resource Ausesresource B.id) and meta-args (depends_on).Parallel apply is controlled by
-parallelism(default 10).
Project Structure & Files
Tip: Use versions.tf to pin versions and prevent accidental upgrades.
Providers
Purpose: Plugins that talk to cloud APIs (AWS/Azure/GCP/K8s/etc).
Notes:
Auth via env vars, shared config, or explicit creds (avoid hardcoding).
Use provider aliases for multi-account, multi-region, cross-service patterns.
Resources (Create Things)
Meta-arguments you’ll use a lot:
depends_on– force edges in the graph.count/for_each– multiplicity.lifecycle–create_before_destroy,prevent_destroy,ignore_changes.provider– choose an alias.
Data Sources (Read Things)
Purpose: Query existing objects for IDs/attributes (no changes).
Variables (Inputs) & Validation
Locals (Computed)
Outputs
Expressions, Loops, Conditionals, Functions
Useful functions: coalesce, try, regex, format, join/split, cidrsubnet, jsonencode, file, templatefile.
State (The Source of Truth)
Purpose: Tracks real-world objects ↔ Terraform resources.
Local vs Remote: Use remote for teams to enable locking, versioning, and DR.
State Locking:
S3 backend + DynamoDB provides a lock to prevent concurrent
apply(like your “round-robin” nugget, here the key point is optimistic locking using a conditional write).Terraform Cloud/Enterprise backends lock automatically.
Key Commands:
terraform state list/show/mv/rm– inspect & refactor state.terraform refresh– re-read remote objects (or-refresh-only plan).terraform taint(legacy) → prefer-replace=addrduring apply.
Workspaces (Logical Environments)
Purpose: Multiple state instances from same config (e.g., dev, stage, prod).
Note: Great for sandboxes; for serious prod, prefer separate folders/stacks & remote state per env.
Modules (Reuse & Composition)
Purpose: Package infra patterns; version via registry or Git.
Best practices:
Minimal inputs, typed vars, sensible defaults.
Version pinning for registry modules (
source = "terraform-aws-modules/vpc/aws" version = "~> 5.0").Expose clean outputs; avoid leaking internals.
Lifecycle & Change Control
Replace flows:
terraform apply -replace=aws_instance.webUse
movedblocks to keep history on refactors (rename resource addresses):
Count vs for_each (Cardinality)
Provisioners (Last Resort)
Use only when nothing else works (prefer cloud-init/user-data, Packer, or config mgmt).
Notes: Non-idempotent, flaky; failures require special handling (on_failure).
Files, Templates & Cloud-Init
cloud_init.tftpl example:
Importing Existing Infra
Modern approach (HCL import blocks):
Then: terraform plan shows proposed state; apply records it without changing the bucket.
Testing (terraform test) & Validation
Unit-ish tests for modules using .tftest.hcl.
tests/basic.tftest.hcl
Run: terraform test
Policy as Code & Security
Sentinel / OPA (Conftest) to enforce rules (tags required, regions restricted).
Sensitive inputs/outputs:
State protection: Enable encryption & locking; never commit state. Use workspaces or separate backends per env.
Drift & Change Review
terraform plan -out=tfplan→ store artifact in CI, review before apply.Refresh-only plan: detect drift safely.
terraform apply tfplanensures you apply what you reviewed.
CI/CD Patterns
Lint:
tflint,terraform fmt -check,terraform validate.Plan on PR, comment summary; manual or auto-apply on
main.Use Terraform Cloud (VCS-driven runs, remote state, policies) or self-hosted runners with an S3 backend + DynamoDB lock.
Workhorse Meta-Args Cheat Sheet
When to use what (like your node selector/affinity/taints table):
Simple multiplicity:
countStable keyed multiplicity:
for_eachOrdering / explicit edge:
depends_onZero-downtime replace:
create_before_destroyKeep hands off drift:
ignore_changesForce recreate:
-replaceon apply
Useful CLI
End-to-End Mini Example (AWS: VPC + EC2 + Remote State)
backend.tf
main.tf
variables.tf
cloud_init.tftpl
Run:
Extra Nuggets (like your kube-proxy “round robin” bit)
Graph & Ordering: Terraform doesn’t “sequence” files; it resolves dependencies from references and
depends_on.Parallelism: Increase with
-parallelism=50for read-heavy plans; be careful with API rate limits.Zero-Downtime Swaps: Use
create_before_destroy+ unique names (or LT + ASG rolling deployments).Idempotence: Prefer data sources + computed names to avoid accidental replacement.
Cost Controls: Tag everything; integrate
infracostin PRs.Secrets: Use external secret stores (SSM Parameter Store, Secrets Manager); mark vars as
sensitive.RBAC/IAM blast radius: Use separate AWS accounts per env when possible; provider aliases keep config tidy.
If you want, I can drop this into a printable doc or a one-file reference with a few ready-to-run module templates (VPC, ALB, EKS, RDS) tailored to your ₹2000/mo Superset offering.
Great question, Pavan. Below is a comprehensive list of Terraform components, resources, and concepts, along with real-world use cases — exactly the kind of deep practical understanding Amazon expects at a senior DevOps level.
✅ Terraform Core Concepts, Resources, and When to Use Them
1. resource
resourceDefinition: Defines an infrastructure object (e.g.,
aws_instance,aws_s3_bucket).Use When: You want to create, manage, or destroy infrastructure in a declarative way.
2. data (Data Source)
data (Data Source)Definition: Reads existing information from a provider without creating it.
Use When: You need to reference existing resources (e.g., AMI IDs, VPCs, SSM parameters).
3. variable
variableDefinition: Parameterizes inputs to make modules/configs dynamic.
Use When: You want to avoid hardcoding and allow flexibility across environments.
4. output
outputDefinition: Prints values after apply or passes values between modules.
Use When: You want to expose resource attributes like
ALB DNS,RDS endpoint.
5. module
moduleDefinition: A self-contained group of resources reused across configurations.
Use When: You want to reuse infrastructure patterns (e.g., VPC, ECS, IAM roles).
6. locals
localsDefinition: Temporary named values for computation within a module.
Use When: You want to simplify logic, DRY up repeated expressions.
7. provider
providerDefinition: Plugin that lets Terraform communicate with APIs (e.g., AWS, GCP).
Use When: You define what cloud or service Terraform should use.
8. terraform block
terraform blockDefinition: Defines backend, required providers, versions.
Use When: You configure state storage, required versions, provider constraints.
9. backend
backendDefinition: Where Terraform stores state (e.g., S3, remote, local).
Use When: You want to enable team collaboration and persistence of infra state.
10. workspace
workspaceDefinition: Isolated state for the same config (e.g.,
dev,prod).Use When: You want multiple environments from a single codebase.
11. lifecycle
lifecycleDefinition: Controls resource behavior during create/update/delete.
prevent_destroy
Prevent accidental deletion of critical resources (e.g., RDS, ALB)
create_before_destroy
Avoid downtime by creating new before destroying old (e.g., ALB listener rules, EC2 AMIs)
ignore_changes
Prevent unnecessary changes from triggering updates (e.g., external secrets rotation)
12. depends_on
depends_onDefinition: Explicitly define dependency between resources.
Use When: Implicit dependency isn't enough (e.g., Lambda depends on IAM policy).
13. count
countDefinition: Creates multiple instances of a resource based on integer value.
Use When: You need N identical resources (e.g., multiple subnets, EC2s).
14. for_each
for_eachDefinition: Create resources from a set or map.
Use When: You want unique named resources (e.g., per-service ALB rule, tagging).
15. dynamic blocks
dynamic blocksDefinition: Programmatically build nested blocks like
ingressorstatement.Use When: You want to generate variable numbers of blocks from data (e.g., security group rules, IAM policies).
16. templatefile()
templatefile()Definition: Loads external template files (e.g., userdata scripts).
Use When: You need to inject variables into files like
EC2 user_data, config files.
17. remote-exec, local-exec provisioners
remote-exec, local-exec provisionersDefinition: Run shell scripts after resource creation.
Use When: Temporary bootstrapping (e.g., install agent, trigger CI webhook).
⚠️ Avoid overusing; violates declarative IaC principles.
18. terraform import
terraform importDefinition: Brings existing resources under Terraform management.
Use When: Migrating unmanaged infrastructure to Terraform without recreate.
19. terraform taint / terraform untaint
terraform taint / terraform untaintDefinition: Marks a resource for recreation on next apply.
Use When: Force resource replacement (e.g., corrupted EBS volume).
20. terraform state
terraform stateDefinition: Command to inspect, move, or remove state data manually.
Use When: Fix drift, split state, remove orphaned resources.
21. null_resource
null_resourceDefinition: A resource that does nothing but allows provisioners or triggers.
Use When: Control logic like scripts or wait conditions without infra.
22. time_sleep, external, random_* resources
time_sleep, external, random_* resourcesUse Cases:
time_sleep: Add wait time during deployment.external: Run external scripts (JSON I/O).random_id,random_pet: Unique naming for buckets, instances.
23. sensitive attribute
sensitive attributeDefinition: Marks an output or variable as sensitive.
Use When: You want to hide secrets in CLI output (e.g., passwords, tokens).
24. override files (override.tf)
override files (override.tf)Definition: Allow user-local overrides.
Use When: Rarely used — mostly in legacy projects.
25. Pre/Post condition checks (v1.2+)
Use When: Enforce resource constraints:
🔧 DevOps/Production Usage Scenarios Table
prevent_destroy
Protect prod RDS, S3, ALBs
create_before_destroy
Rolling updates, immutable infra
ignore_changes
Ignore drift like password rotation or manually updated tags
count
Spin up 3 NAT gateways or AZs
for_each
Create resources per service (tags, IAM roles)
modules
Reuse: VPC, ALB, ECS, RDS, CloudWatch
workspaces
Isolate dev/stage/prod environments
backend "s3"
Remote state for collaboration
dynamodb_table
Locking to prevent race conditions
locals
DRY up repeated values like common_tags
templatefile()
Dynamic EC2 user-data or app config
external
Call external scripts (e.g., fetch from REST API)
random_id
Generate unique names for globally-scoped resources (e.g., S3)
Let me know if you want:
A PDF cheat sheet of this
A diagram of Terraform architecture
Terraform design doc template for real projects (great for interviews)
Would you like me to build that next?
Last updated