Terraform: quickstart

The basic cycle for deploying infrastructure: init, plan, then apply.

1. Initialize

Downloads the providers and configures the backend. Run it once, then again after every provider or backend change.

bash
terraform init

2. Plan

Computes the changes to apply without modifying anything. The plan file guarantees that what gets applied is exactly what was reviewed.

bash
terraform plan -out=tfplan

3. Deploy

Applies the saved plan.

bash
terraform apply tfplan

Terraform has no deploy command: apply is what deploys.

Example configuration

main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "eu-west-3"
}
resource "aws_s3_bucket" "docs" {
bucket = "my-docs-bucket"
}

Export and read the plan

Optional, outside the basic cycle. The tfplan file is binary: use terraform show to display it in a human-readable format.

bash
terraform show tfplan

Add -no-color to get plain text, for example to paste it in a merge request or a log.

bash
terraform show -no-color tfplan > plan.txt

Export as JSON

For tooling and automation, export the plan in a machine-readable format.

bash
terraform show -json tfplan > plan.json

Then use jq to extract what matters, such as one line per resource with its planned actions.

bash
jq -r '.resource_changes[] | select(.change.actions != ["no-op"]) | "\(.change.actions | join(",")) \(.address)"' plan.json

The plan can contain sensitive values in clear text. Do not commit tfplan or plan.json.

Destroy

bash
terraform destroy