Terraform Concepts
Terraform models infrastructure as code and keeps track of the real-world resources it manages through state.
Core Concepts
- Provider: the plugin that talks to an external API.
- Resource: a managed object such as a VM, network, or bucket.
- Data source: read-only information fetched from a provider.
- Variable: an input value used to parameterize modules and resources.
- Output: a value returned to users or other modules.
- Module: a reusable bundle of Terraform configuration.
- State: the mapping between configuration and real resources.
Why State Matters
- It lets Terraform detect drift.
- It allows plans to compare desired and current state.
- It gives Terraform a consistent view of resources across runs.
Example HCL
variable "region" {
type = string
default = "eu-west-1"
}
provider "aws" {
region = var.region
}
resource "aws_s3_bucket" "logs" {
bucket = "my-company-logs"
}
output "bucket_name" {
value = aws_s3_bucket.logs.bucket
}
Practical Notes
- Keep modules small and focused.
- Use state backends for collaboration.
- Avoid mixing unrelated infrastructure into one root module.