Terraform is an infrastructure as code tool that lets you build, change, and version cloud and on-prem resources safely and efficiently.
Infrastructure as Code
Instead of describing infrastructure in a document like
Server: 4CPUs, Memory: 8GB, Ubuntu, Firewall: Port 80,
We write it as code:
resource "google_compute_instance" "web" {
machine_type = "e2-standard-2"
boot_disk {
initialize_params {
image = "ubuntu-2204-lts"
}
}
}
Terraform versions the infrastructure as Code (Terraform configuration files) that define the cloud or on-premises resources. By storing these files in Git, we can track, review, compare and revert infrastructure changes just like application code.
Terraform doesn't version the infrastructure like VM itself. It versions the code that defines the infrastructure. That lets you track, review and roll back infrastructure changes using a version control system like Git.
Terraform State File
The Terraform state file (terraform.tfstate) is a file that stores Terraform's knowledge of the infrastructure it has created and manages.
Without state file, Terraform would forget everything it had created.
It contains:
Resources
google_compute_instance.web
Name
web-server
ID
123456789
Zone
asia-sout1-a
Machine Type
e2-medium
Status
Running
ID = Also knowns as Resource Id used for updation even if the machine type changes in the VM. Resource Id would be constant so we can change the correct resource by looking the Id.
Where is state file stored?
- Local state: It lives on computer.
- Remote state (in companies): Stored remotely such as Google Cloud Storage, AWS S3, Azure Storage.
Remote storage allows everyone on the team to work with the same infrastructure safely.
Without state file: Duplicate resource may be created since it didn't know anything about current state.
Even though state file extension is .tfstate, its contents are in JSON.
Terraform Workflow
1. Write Terraform code
Create configuration files.
In this phase, nothing has been created.
2. Format the code
terraform fmt
Terraform automatically:
- fixes indentation
- aligns equal signs
- improves readability
3. Validate
terraform validate
Terraform checks:
- Syntax
- missing arguments
- invalid references
4. Initialize
terraform init
Terraform init helps to downloads everything needed or the project.
It downloads provider plugin, initializes the backend and modules and create .terraform directory.
It is like installing dependencies from the internet.
Eg:
provider "google" {}
Terraform downloads the Google Cloud provider plugin.
Without it, terraform doesn't know how to talk to Google Cloud APIs.
5. Plan
It is the terraform's decision making step.
It compares current and desired state.
Internally, it performs
Terraform code
↓
Read state file
↓
Ask Google cloud
↓
Compare
↓
Generate Execution plan
HashiCorp calls this an execution plan, which previews the infrastructure changes before anything modified.
6. Review
User's team reviews the plan.
It is one of the terraform's biggest safety features.
7. Apply
terraform apply
Terraform reads the plan, calls google cloud apis, Creates resource, waits until they are ready, writes the stae file.
8. Update State
After a successful apply:
terraform.tfstate is updated.
9. Destroy
If the environment is no longer needed. Then use
terraform destroy
.terraform/ Directory
.terraform/ directory is like node_modules where the downloaded dependencies and all plugin, modules, metadata will be stored.
.terraform.lock.hcl
.terraform.lock.hcl is Terraform's dependency lock file. It stores the provider name, the exact provider version, and checksums (hashes) of the provider plugins.
During future terraform init runs, Terraform uses this file to download the same provider version and verifies the downloaded plugin against the stored checksums.
This ensures everyone on the team and CI/CD pipelines use identical provider versions, preventing unexpected behavior, compatibility issues, and bugs caused by different provider versions.
Terraform Configuration Files
main.tf
This is the main configuration file.
It contains the infrastructure wants terraform to create.
variables.tf
Instead of hardcoding values, we declare variable.
What values are changeable?
terraform.tfvars
This file provides values for the variables.
It provides answers for the variables.tf.
outputs.tf
Used to display useful information after deployment.
providers.tf
Specifies which cloud provider terraform should use.
Top comments (0)