Anything you do to Terraform twice should be automated. This walks the path from a manual install to a repeatable, pipeline-driven setup.
1. Install it reproducibly
brew install terraform # or download the binary from releases
terraform -install-autocomplete
terraform version2. Describe the desired state
A minimal AWS example with remote state and a variable.
terraform {
required_providers { aws = { source = "hashicorp/aws", version = "~> 5.0" } }
backend "s3" {
bucket = "acme-tfstate"
key = "prod/network.tfstate"
region = "eu-west-1"
}
}
variable "env" { type = string, default = "prod" }
resource "aws_s3_bucket" "assets" {
bucket = "acme-${var.env}-assets"
tags = { Environment = var.env }
}3. Run it from CI
Plan on every PR, apply only on main.
plan:
stage: test
script:
- terraform init -input=false
- terraform plan -input=false -out=tfplan
apply:
stage: deploy
script: terraform apply -input=false -auto-approve tfplan
rules:
- if: '$CI_COMMIT_BRANCH == "main"'Want senior help putting Terraform to work - or a team bootcamp on it? CodeWizard runs Israel's largest DevOps community and embeds named senior engineers with a transparent hours log. Book a free consult or explore the Academy.