1. The typical pipeline #
Plan on merge request, apply on merge.
Steps #
name: terraform
on: { pull_request: {}, push: { branches: [main] } }
permissions: { id-token: write, contents: read, pull-requests: write }
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
- uses: aws-actions/configure-aws-credentials@v4
with: { role-to-assume: "arn:aws:iam::123456789012:role/ci-terraform", aws-region: eu-west-3 }
- run: terraform init -input=false
- run: terraform fmt -check && terraform validate && terraform test
- run: terraform plan -input=false -out=plan.tfplan
- if: github.ref == 'refs/heads/main'
run: terraform apply -input=false plan.tfplan2. Federated identities #
No secrets in the CI.
OIDC #
The CI provider issues an OIDC token; the cloud exchanges it for temporary credentials of a restricted role (AWS IAM role, Azure federated identity, Google Workload Identity Federation).
Good practice: Separate the roles: read-only for the plan on merge requests, write access for the apply on the main branch.
3. Dedicated tools #
Atlantis, HCP Terraform, drift.
Overview #
| Tool | Benefit |
|---|---|
| Atlantis | plan and apply driven by merge request comments |
| HCP Terraform | Remote runs, Sentinel/OPA policies, cost estimation |
| Infracost | Cost of the change shown in the merge request |
| Drift detection | plan -refresh-only scheduled every night, alert on differences |
My progress
Sign in to track your progress and take notes. Sign in
💬 Questions about this lesson
No questions yet: be the first to ask for help or share your configuration.
Recommended Articles
Terraform best practices: structure, state, security, review and maintenance
Terraform best practices for teams: repository structure, naming, pinned versions, encrypted and ...
Terraform: principles, architecture and workflow
Understand Terraform: declarative infrastructure as code, architecture (core, providers, state), ...
Testing and securing Terraform: validate, terraform test, tflint, Checkov and policies
Ensure the quality of Terraform code: fmt and validate, native terraform test tests with mocked p...
Terraform state: remote backends, locking, import and refactoring
Manage Terraform state: the role of the state file, sensitive data, remote backends (S3, azurerm,...
Infrastructure as Code and GitOps: Terraform, Ansible and Argo CD
Manage infrastructure as code: declarative or imperative, Terraform (providers, state, plan, modu...
Preparing for the HashiCorp Certified: Terraform Associate certification
Preparation guide for the Terraform Associate: format, official objectives (IaC concepts, workflo...
Terraform modules: designing, versioning and reusing
Structure Terraform with modules: root module and child modules, inputs and outputs, sources (loc...
Terraform multi-environment, workspaces and repository organization
Organize Terraform for several environments: one folder per environment, tfvars files, CLI worksp...
Terraform across multiple clouds: AWS, Azure, Google Cloud, OVHcloud and Kubernetes
Use Terraform with the main providers: aws, azurerm, google, ovh and openstack, kubernetes and he...