Getting Terraform into your pipeline is where it starts paying off - automated, repeatable and reviewed. Here is a working integration and the habits that keep it reliable.
A working pipeline snippet
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"'Do this
- Remote, locked state; separate state per environment/component
- Pin provider and module versions; commit .terraform.lock.hcl
- Keep modules small and composable; review plans in the PR
- Use workspaces or directories, never edit prod state by hand
Avoid this
- Local state on a laptop - use remote state with locking (S3+DynamoDB, TFC)
- Giant monolithic states that take minutes to plan and block the whole team
- Running apply manually without reviewing the plan
- Hardcoded secrets in .tf files instead of a secret manager
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.