A GitHub Actions workflow calls Terraform by spinning up an ephemeral runner, authenticating with your cloud provider, installing the Terraform CLI, and executing standard Terraform CLI commands via YAML steps. -------------------------------------- 1. How the Execution Flow Works -------------------------------------- When a repository event occurs (such as a pull_request or push to main), GitHub Actions runs a sequence of steps defined in a .github/workflows/*.yml file: Checkout Code-> The workflow uses actions/checkout to clone your Terraform configuration files (.tf) onto the runner. Authenticate Cloud Credentials-> Because the runner needs permission to create or modify infrastructure, it authenticates with your cloud provider. Modern setups use OpenID Connect (OIDC) to request short-lived temporary credentials (e.g. via aws-actions/configure-aws-credentials), avoiding static secret keys. Install Terraform CLI-> The official HashiCorp Setup Terraform Action (hashicorp/setup-terraform) downloads and configures your specified Terraform binary version on the runner. Run Terraform Commands-> Subsequent run: steps execute standard CLI commands inside your Terraform directory: -terraform init connects to your remote state backend (such as an S3 bucket or Terraform Cloud) and downloads provider plugins. -terraform plan previews infrastructure changes—typically triggered on pull requests so reviewers can inspect the diff before merging. -terraform apply -auto-approve provisions the changes—typically triggered automatically once a pull request merges into main. -------------------------------------- 2. Example Workflow YAML -------------------------------------- Here is how a standard pipeline looks in .github/workflows/terraform.yml, following the official HashiCorp GitHub Actions Tutorial: (https://developer.hashicorp.com/terraform/tutorials/automation/github-actions) # terraform.yaml name: 'Terraform CI/CD' on: push: branches: [ main ] pull_request: permissions: id-token: write # Required for OIDC authentication contents: read # Required to checkout code jobs: terraform: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - name: Configure AWS Credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::123456789012:role/GitHubActionsRole aws-region: us-east-1 - name: Setup Terraform uses: hashicorp/setup-terraform@v3 with: terraform_version: 1.9.0 - name: Terraform Init run: terraform init - name: Terraform Plan if: github.event_name == 'pull_request' run: terraform plan -no-color - name: Terraform Apply if: github.ref == 'refs/heads/main' && github.event_name == 'push' run: terraform apply -auto-approve -------------------------------------- 3. Critical Requirement: Remote State -------------------------------------- Because GitHub Actions runners are stateless (destroyed immediately after the job finishes), local terraform.tfstate files are lost between runs. DevOps engineers must configure a remote backend—such as Amazon S3, Azure Blob Storage, Google Cloud Storage, or HCP Terraform — so every workflow run reads and locks the same shared state file.