In this section, we will explore how to integrate Terraform with GitHub Actions to automate your infrastructure deployments. GitHub Actions is a powerful CI/CD tool that allows you to automate workflows directly from your GitHub repository.

Objectives

  • Understand the basics of GitHub Actions.
  • Learn how to set up a GitHub Actions workflow for Terraform.
  • Automate Terraform commands using GitHub Actions.
  • Securely manage secrets and environment variables.

Prerequisites

  • Basic understanding of Terraform and GitHub.
  • A GitHub repository to work with.
  • Terraform installed locally for initial setup and testing.

  1. Introduction to GitHub Actions

GitHub Actions allows you to automate tasks within your software development lifecycle. You can create workflows that build, test, and deploy your code right from GitHub.

Key Concepts

  • Workflow: A configurable automated process made up of one or more jobs.
  • Job: A set of steps executed on the same runner.
  • Step: An individual task that can run commands or actions.
  • Runner: A server that runs your workflows when triggered.

  1. Setting Up a GitHub Actions Workflow for Terraform

Step 1: Create a .github/workflows Directory

In your GitHub repository, create a directory named .github/workflows. This is where your workflow files will reside.

mkdir -p .github/workflows

Step 2: Create a Workflow File

Create a new file named terraform.yml inside the .github/workflows directory.

name: Terraform

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  terraform:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Terraform
      uses: hashicorp/setup-terraform@v1

    - name: Terraform Init
      run: terraform init

    - name: Terraform Plan
      run: terraform plan

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

Explanation

  • name: The name of the workflow.
  • on: Specifies the events that trigger the workflow (e.g., push, pull_request).
  • jobs: Defines the jobs to be run.
  • runs-on: Specifies the runner environment (e.g., ubuntu-latest).
  • steps: Lists the steps to be executed in the job.

Step 3: Configure Secrets

To securely manage sensitive information like cloud provider credentials, use GitHub Secrets.

  1. Go to your GitHub repository.
  2. Navigate to Settings > Secrets > New repository secret.
  3. Add the necessary secrets (e.g., AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY).

Example of Using Secrets in Workflow

Modify the terraform.yml to include environment variables for secrets.

    - name: Terraform Init
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      run: terraform init

    - name: Terraform Plan
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      run: terraform plan

    - name: Terraform Apply
      if: github.ref == 'refs/heads/main'
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      run: terraform apply -auto-approve

  1. Automating Terraform Commands

Terraform Init

Initializes the Terraform working directory.

- name: Terraform Init
  run: terraform init

Terraform Plan

Generates an execution plan.

- name: Terraform Plan
  run: terraform plan

Terraform Apply

Applies the changes required to reach the desired state of the configuration.

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

  1. Practical Exercise

Exercise: Automate Terraform Deployment with GitHub Actions

  1. Create a GitHub Repository: Create a new repository on GitHub.
  2. Add Terraform Configuration: Add your Terraform configuration files to the repository.
  3. Set Up GitHub Actions: Follow the steps above to create a .github/workflows/terraform.yml file.
  4. Add Secrets: Add necessary secrets to your GitHub repository.
  5. Push Changes: Push your changes to the repository and observe the GitHub Actions workflow in action.

Solution

  1. Repository Structure:

    .
    ├── .github
    │   └── workflows
    │       └── terraform.yml
    ├── main.tf
    └── variables.tf
    
  2. main.tf:

    provider "aws" {
      region = "us-west-2"
    }
    
    resource "aws_s3_bucket" "example" {
      bucket = "my-unique-bucket-name"
      acl    = "private"
    }
    
  3. variables.tf:

    variable "aws_region" {
      description = "The AWS region to deploy to"
      default     = "us-west-2"
    }
    
  4. terraform.yml:

    name: Terraform
    
    on:
      push:
        branches:
          - main
      pull_request:
        branches:
          - main
    
    jobs:
      terraform:
        runs-on: ubuntu-latest
    
        steps:
        - name: Checkout code
          uses: actions/checkout@v2
    
        - name: Set up Terraform
          uses: hashicorp/setup-terraform@v1
    
        - name: Terraform Init
          env:
            AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
            AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          run: terraform init
    
        - name: Terraform Plan
          env:
            AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
            AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          run: terraform plan
    
        - name: Terraform Apply
          if: github.ref == 'refs/heads/main'
          env:
            AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
            AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          run: terraform apply -auto-approve
    

Conclusion

In this section, you learned how to integrate Terraform with GitHub Actions to automate your infrastructure deployments. You set up a GitHub Actions workflow, configured secrets, and automated Terraform commands. This integration helps streamline your CI/CD pipeline, making your infrastructure as code (IaC) practices more efficient and reliable.

Next, we will explore how to use Terraform Cloud and Enterprise for advanced collaboration and management features.

© Copyright 2024. All rights reserved