1. What Terraform Is
Terraform is an Infrastructure as Code (IaC) tool by HashiCorp for defining, provisioning, and managing cloud infrastructure declaratively via HCL (HashiCorp Configuration Language).
Terraform manages infrastructure lifecycle — create, update, destroy — by comparing your desired configuration to the actual state.
2. Core Concepts
a. Providers
Plugins that enable Terraform to interact with cloud or service APIs.
provider "aws" {
region = "eu-central-1"
}
b. Resources
Define infrastructure components (e.g., EC2, S3, etc.).
resource "aws_instance" "app_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
}
c. Data Sources
Read-only access to existing data or resources.
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"]
}
d. Variables
Enable parameterization and flexibility.
variable "instance_type" {
type = string
default = "t3.micro"
}
Use as var.instance_type.
e. Outputs
Expose values after provisioning.
output "instance_ip" {
value = aws_instance.app_server.public_ip
}
f. State
Tracks deployed infrastructure and relationships. Enables Terraform to detect drift and manage updates efficiently. Stored locally or remotely (terraform.tfstate).
g. Modules
Reusable configuration units — each folder with .tf files is a module.
module "network" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
}
h. Backends
Define where state is stored (local, S3, GCS, Terraform Cloud, etc.).
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "envs/prod/terraform.tfstate"
region = "eu-central-1"
}
}
3. Terraform Workflow
| Step | Command | Purpose |
|---|---|---|
| 1️⃣ | terraform init | Initialize project, download providers/modules |
| 2️⃣ | terraform validate | Validate syntax and configuration |
| 3️⃣ | terraform plan | Preview intended changes |
| 4️⃣ | terraform apply | Apply desired changes |
| 5️⃣ | terraform destroy | Remove all resources |
| 6️⃣ | terraform fmt | Format code |
| 7️⃣ | terraform show | Display current state |
4. Dependency Management
Terraform builds a dependency graph automatically based on references. You can also specify explicit dependencies:
resource "aws_instance" "app" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
depends_on = [aws_vpc.main]
}
5. Execution Model
- Read Configuration – Parse
.tffiles. - Refresh State – Sync with real infrastructure.
- Plan & Apply – Execute changes to match desired state.
6. Common Files and Structure
main.tf # resources
variables.tf # variable definitions
outputs.tf # outputs
provider.tf # providers and backend
terraform.tfvars # variable values
References
- What is Terraform: https://developer.hashicorp.com/terraform/intro
- Language overview: https://developer.hashicorp.com/terraform/language
- Providers: https://developer.hashicorp.com/terraform/language/providers
- Provider requirements: https://developer.hashicorp.com/terraform/language/providers/requirement
- Resources: https://developer.hashicorp.com/terraform/language/resources
- Data sources (AWS AMI): https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami
- Variables: https://developer.hashicorp.com/terraform/language/values/variables
- Outputs: https://developer.hashicorp.com/terraform/language/values/outputs
- Sensitive data in state: https://developer.hashicorp.com/terraform/language/state#sensitive-data-in-state
- Backends overview: https://developer.hashicorp.com/terraform/language/settings/backends/configuration
- S3 backend: https://developer.hashicorp.com/terraform/language/settings/backends/s3
- Dependency graph and depends_on: https://developer.hashicorp.com/terraform/language/meta-arguments/depends_on
- CLI commands: https://developer.hashicorp.com/terraform/cli/commands
Workspaces: https://developer.hashicorp.com/terraform/cli/workspaces