RousselTMLEARNING

Article · Terraform: from discovery to expertise

Terraform in CI/CD

1. The typical pipeline #

Plan on merge request, apply on merge.

Steps #

Terraform pipeline
Applying exactly the reviewed plan avoids surprises.
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.tfplan

2. 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 #

ToolBenefit
Atlantisplan and apply driven by merge request comments
HCP TerraformRemote runs, Sentinel/OPA policies, cost estimation
InfracostCost of the change shown in the merge request
Drift detectionplan -refresh-only scheduled every night, alert on differences

📄 Terraform in CI/CD: pipelines, plan review, OIDC and GitOps →

My progress

  • ⏱ Estimated time : ≈ 5 min

Sign in to track your progress and take notes. Sign in

⭐ Rate this lesson

5.0 initial rating from the teaching team

Sign in to rate this lesson →

💬 Questions about this lesson

No questions yet: be the first to ask for help or share your configuration.

Ask a question about this lesson →

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...

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!