GitHub Actions Terraform Tutorial CI CD Infrastructure as Code

Logan Jul 13, 2026

Embarking on your Infrastructure as Code (IaC) journey with Terraform and eager to integrate it with GitHub Actions? You've come to the right place. This comprehensive tutorial will guide you step-by-step through the process of setting up automated infrastructure deployments using Terraform and GitHub Actions.

Terraform for Beginners in 1 Hour
Terraform for Beginners in 1 Hour

By the end of this guide, you'll understand how to harness the power of GitHub Actions to automate your Terraform workflow, from initialize to apply. Let's dive right in!

an info sheet with information about the different types of cheethhets and how to use them
an info sheet with information about the different types of cheethhets and how to use them

Setting Up Your GitHub Repository

First, ensure you have a GitHub repository set up for your Terraform configuration. This repository will contain all your Terraform files, such as main.tf, variables.tf, and outputs.tf.

Terraform Workflow Process Explained
Terraform Workflow Process Explained

For this tutorial, let's assume you're using the AWS provider to launch an EC2 instance. Therefore, you'll need to initialize your Terraform configuration with the AWS provider.

Initialize the Terraform Configuration

How GitHub Actions 10x my productivity
How GitHub Actions 10x my productivity

In your terminal, navigate to your local Terraform configuration directory. Then, run the following command to initialize the Terraform configuration:

```bash terraform init ```

Understanding the Terraform Workflow

The Terraform workflow consists of four main stages: initialize, plan, apply, and destroy. Throughout this tutorial, we'll focus on the initialize, plan, and apply stages, as they form the core of automating your infrastructure deployments.

a blue and green brochure with the words transform cheethhete on it
a blue and green brochure with the words transform cheethhete on it

Creating the GitHub Actions Workflow

Now that your Terraform configuration is set up, it's time to create a GitHub Actions workflow to automate the initialization, planning, and application of your infrastructure.

In your GitHub repository, create a new directory named .github. Inside this directory, create another directory named workflows. Create a new file named terraform.yml inside the workflows directory. This file will contain the definition of your GitHub Actions workflow.

Terraform Workflow | write -> init -> plan -> apply -> destroy
Terraform Workflow | write -> init -> plan -> apply -> destroy

Defining the Workflow Job

Open terraform.yml in your favorite text editor and define your workflow job as follows:

Earth spell effect
Earth spell effect
Git Workflow Roadmap
Git Workflow Roadmap
n8n automation n8n expert n8n workflows n8n solutions n8n specialist n8n ai agents n8n operations
n8n automation n8n expert n8n workflows n8n solutions n8n specialist n8n ai agents n8n operations
Push Your Local Project to GitHub: Git Setup Guide for Beginners
Push Your Local Project to GitHub: Git Setup Guide for Beginners
Git
Git
an info sheet describing how to use the new material for your character's avatar
an info sheet describing how to use the new material for your character's avatar
Git Workflow
Git Workflow
GitHub Issues · Project planning for developers
GitHub Issues · Project planning for developers
Deploy vite app to GitHub pages for Free
Deploy vite app to GitHub pages for Free
GASP ALS Tutorial - How To Add Overlay States - Free Unreal Project on GitHub
GASP ALS Tutorial - How To Add Overlay States - Free Unreal Project on GitHub
(Higgsas) Geometry Nodes Groups Blender 3.3/5.0
(Higgsas) Geometry Nodes Groups Blender 3.3/5.0
an image of a box with the text you can rip edges with glits
an image of a box with the text you can rip edges with glits
two different views of the same area, one with an arrow pointing up to it
two different views of the same area, one with an arrow pointing up to it
a blackboard with blue and green graphics on it
a blackboard with blue and green graphics on it
3D Controller on Godot Engine for Game Developers
3D Controller on Godot Engine for Game Developers
four different screens showing the various stages of animation
four different screens showing the various stages of animation
Contribute to CircuitPython with Git and GitHub
Contribute to CircuitPython with Git and GitHub
3d Modeling Ideas, Simple Blender Projects, Blender Modeling Tips, Blender 3d Projects, Blender Inspo, 3d Modeling Guide, Blender Cheat Sheet, Blender Material Tutorial, Blender Art
3d Modeling Ideas, Simple Blender Projects, Blender Modeling Tips, Blender 3d Projects, Blender Inspo, 3d Modeling Guide, Blender Cheat Sheet, Blender Material Tutorial, Blender Art
an image of a rock with moss growing on it and the words geomety notes 3 0
an image of a rock with moss growing on it and the words geomety notes 3 0
Utilizing Mixer in Terrain Production
Utilizing Mixer in Terrain Production

```yaml name: Terraform on: push: branches: - main jobs: terraform: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Setup Terraform uses: hashicorp/setup-terraform@v1 - name: Terraform Init run: terraform init - name: Terraform Plan run: terraform plan -no-color - name: Terraform Apply run: terraform apply -auto-approve ```

Broken down, here's what this workflow does:

  • The workflow triggers on a push event to the main branch.
  • Each job runs on the ubuntu-latest runner.
  • The checkout step checks out your repository code.
  • The Setup Terraform step sets up the Terraform CLI.
  • The Terraform Init, Terraform Plan, and Terraform Apply steps initialize, plan, and apply your Terraform configuration, respectively.

Running the Workflow

Now that your GitHub Actions workflow is defined, it's time to run it. Push your changes to the main branch, and GitHub Actions will trigger the workflow automatically.

The final Terraform state will be stored in a remote backend, such as AWS S3 or Azure RM. Remember to configure your remote backend in your Terraform configuration.

Monitoring the Workflow Run

You can monitor the workflow run in the Actions tab of your GitHub repository. The workflow run will display the output of each step, allowing you to debug any issues that may arise.

Congratulations! You've successfully automated your Terraform workflow using GitHub Actions. This powerful combination enables you to maintain infrastructure as code and automated deployments, ensuring your infrastructure is version-controlled and tested.

As you continue your IaC journey, consider exploring other features of GitHub Actions, such as environment protection, secrets, andmatrix builds, to further enhance your workflow. happy automating!