๐ Terraform Summary Guide
๐ Terraform Summary Guide
I. What is Terraform?
Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to provision, manage, and version infrastructure across a variety of providers such as AWS, Azure, GCP, VMware, and more using a declarative language (HCL - HashiCorp Configuration Language).
II. Key Concepts
Concept | Description |
---|---|
IaC (Infrastructure as Code) | Manage infrastructure using machine-readable files instead of manual processes. |
Provider | Plugin that interacts with APIs of platforms like AWS, Azure, GCP, etc. |
Resource | Basic building block of infrastructure (e.g., an EC2 instance, S3 bucket). |
Module | Reusable and shareable group of Terraform configurations. |
State File | Stores information about managed infrastructure and current state. |
Plan | Preview of what Terraform will do before applying changes. |
Apply | Executes the planned changes to infrastructure. |
III. Terraform Workflow
1. Write
Write configuration in .tf
files using HCL syntax.
2. Init
Initializes working directory and downloads providers.
3. Plan
Shows what will be changed.
4. Apply
Applies the changes to infrastructure.
5. Destroy
Tears down all resources defined in the config.
IV. Configuration File Structure
Example: main.tf
V. Providers
-
Connect Terraform to APIs.
-
Examples: AWS, Azure, GCP, Kubernetes, Docker.
-
Declared in configuration with authentication credentials.
VI. Resources
Resources describe infrastructure objects:
VII. Variables and Outputs
Variables
Defined using variable
block or passed in via CLI, files, or environment.
Outputs
Used to print or export values from your configuration.
VIII. State Management
-
terraform.tfstate stores the current state of infrastructure.
-
Should be stored securely (e.g., in S3 for remote teams).
-
Commands:
-
terraform state list
-
terraform state show
-
terraform state rm
-
IX. Modules
Modules are containers for multiple resources that can be reused.
Module Types
-
Local Modules (within your repo)
-
Remote Modules (Terraform Registry, GitHub, S3, etc.)
X. Backends
Defines where Terraform state is stored (local or remote).
XI. Terraform CLI Commands Summary
Command | Purpose |
---|---|
| Initializes directory and downloads plugins. |
| Shows what Terraform will do. |
| Applies the planned changes. |
| Removes all resources. |
| Validates configuration syntax. |
| Formats code to HCL standards. |
| Displays current state. |
XII. Terraform Best Practices
-
Use modules for reusability and clarity.
-
Store state remotely with locking (e.g., S3 + DynamoDB).
-
Use workspaces for managing environments (dev/stage/prod).
-
Use version constraints for providers.
-
Keep secrets and sensitive data out of
.tf
files (use Vault or env vars). -
Use
terraform plan
before every apply to avoid surprises. -
Use
.terraformignore
to exclude files from state.
XIII. Terraform vs Other Tools
Comments
Post a Comment