0
0
Terraformcloud~5 mins

Terraform in GitHub Actions - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform helps you create and change cloud resources automatically. GitHub Actions lets you run these Terraform steps whenever you update your code, so your cloud setup stays up to date without manual work.
When you want to automatically create or update cloud resources after changing your code.
When you want to keep your cloud setup consistent and avoid manual mistakes.
When you want to share your infrastructure setup with your team and track changes in one place.
When you want to test your infrastructure changes safely before applying them.
When you want to save time by automating cloud resource management.
Config File - terraform.yml
terraform.yml
name: Terraform CI

on:
  push:
    branches:
      - main

jobs:
  terraform:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v2
        with:
          terraform_version: 1.5.6

      - name: Terraform Init
        run: terraform init

      - name: Terraform Validate
        run: terraform validate

      - name: Terraform Plan
        run: terraform plan

      - name: Terraform Apply
        if: github.ref == 'refs/heads/main' && github.event_name == 'push'
        run: terraform apply -auto-approve

This GitHub Actions workflow runs Terraform commands automatically when you push to the main branch.

  • Checkout repository: Gets your code so Terraform can use it.
  • Setup Terraform: Installs the right Terraform version.
  • Terraform Init: Prepares Terraform to work with your cloud provider.
  • Terraform Validate: Checks your Terraform files for mistakes.
  • Terraform Plan: Shows what changes Terraform will make.
  • Terraform Apply: Applies the changes automatically on the main branch.
Commands
Clone your repository locally to add the GitHub Actions workflow file.
Terminal
git clone https://github.com/example-user/example-repo.git
Expected OutputExpected
Cloning into 'example-repo'... remote: Enumerating objects: 10, done. remote: Counting objects: 100% (10/10), done. remote: Compressing objects: 100% (8/8), done. remote: Total 10 (delta 1), reused 10 (delta 1), pack-reused 0 Receiving objects: 100% (10/10), 2.34 KiB | 2.34 MiB/s, done. Resolving deltas: 100% (1/1), done.
Add the Terraform workflow file to the staging area to prepare for commit.
Terminal
git add .github/workflows/terraform.yml
Expected OutputExpected
No output (command runs silently)
Commit the workflow file with a clear message describing the change.
Terminal
git commit -m "Add Terraform GitHub Actions workflow"
Expected OutputExpected
[main abc1234] Add Terraform GitHub Actions workflow 1 file changed, 20 insertions(+) create mode 100644 .github/workflows/terraform.yml
Push your changes to the main branch to trigger the GitHub Actions workflow.
Terminal
git push origin main
Expected OutputExpected
Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 8 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 350 bytes | 350.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (1/1), done. To https://github.com/example-user/example-repo.git def5678..abc1234 main -> main
Check the status of the GitHub Actions runs to see if the Terraform workflow ran successfully.
Terminal
gh run list --repo example-user/example-repo
Expected OutputExpected
ID NAME STATUS CONCLUSION WORKFLOW BRANCH EVENT CREATED AT 12345 Terraform CI completed success Terraform CI main push 2024-06-01T12:00:00Z
--repo - Specify the repository to check workflow runs
Key Concept

If you remember nothing else from this pattern, remember: automating Terraform with GitHub Actions keeps your cloud setup safe, consistent, and up to date without manual steps.

Common Mistakes
Not setting the Terraform version in the setup step.
This can cause the workflow to use an unexpected Terraform version, leading to errors or unexpected behavior.
Always specify the Terraform version explicitly in the setup-terraform action.
Running terraform apply on every branch push.
This can cause unintended changes in your cloud environment from experimental branches.
Limit terraform apply to run only on the main branch or protected branches.
Not committing the workflow file to the correct directory.
GitHub Actions only runs workflows in the .github/workflows directory.
Place workflow YAML files inside .github/workflows before committing.
Summary
Create a GitHub Actions workflow file to run Terraform commands automatically.
Use steps to checkout code, setup Terraform, initialize, validate, plan, and apply changes.
Push changes to the main branch to trigger the workflow and update cloud resources safely.