0Pricing
DevOps Bootcamp · Lesson

IaC with Terraform and GitHub Actions

Understand how to integrate Terraform into your GitHub Actions workflows to provision and manage infrastructure.

IaC with Terraform and GitHub Actions is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Infrastructure as Code (IaC)

Welcome! In this lesson, we'll dive into Infrastructure as Code (IaC). This approach means managing and provisioning your infrastructure (like servers, databases, and networks) using code instead of manual processes.

Think of it as writing software for your infrastructure. This brings many benefits, including consistency, repeatability, and version control for your entire setup.

Meet Terraform for IaC

One of the most popular tools for IaC is Terraform, an open-source tool developed by HashiCorp.

Terraform allows you to define your infrastructure resources in human-readable configuration files. It then uses these files to provision and manage resources across various cloud providers (like AWS, Azure, GCP) and other services.

Terraform's HCL Syntax

Terraform uses its own declarative language called HashiCorp Configuration Language (HCL). HCL is designed to be easy for humans to read and write, making your infrastructure definitions clear.

Here's a tiny example of HCL defining an AWS VPC (Virtual Private Cloud):

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "my-vpc"
  }
}

The Terraform Core Workflow

Terraform follows a consistent three-step workflow to manage your infrastructure:

  • terraform init: Initializes your working directory, downloading necessary provider plugins.
  • terraform plan: Generates an execution plan, showing what changes Terraform will make to your infrastructure.
  • terraform apply: Executes the changes outlined in the plan, provisioning or modifying your resources.

Automating with GitHub Actions

Integrating Terraform with GitHub Actions supercharges your IaC strategy. It allows you to automate critical steps in your infrastructure deployment pipeline.

With GitHub Actions, you can:

  • Automatically run terraform plan on every pull request.
  • Ensure infrastructure changes are reviewed and approved.
  • Automate terraform apply for controlled deployments.

Your Terraform Workflow File

A GitHub Actions workflow for Terraform is defined in a YAML file within your repository's .github/workflows/ directory. This file specifies when the workflow runs and what steps it performs.

Here's a basic structure for a workflow that could plan changes:

name: 'Terraform CI'
on: 
  pull_request:
    branches:
      - main
jobs:
  terraform:
    name: 'Terraform'
    runs-on: ubuntu-latest
    steps:
      # Your workflow steps will go here

Setup Terraform & Init Step

Before running any Terraform commands, your workflow needs to check out your code, set up the Terraform CLI, and initialize the working directory.

The hashicorp/setup-terraform action makes this easy:

- name: Checkout Code
  uses: actions/checkout@v4

- name: Setup Terraform
  uses: hashicorp/setup-terraform@v2
  with:
    terraform_version: 1.5.x

- name: Terraform Init
  run: terraform init

The Terraform Plan Step

The terraform plan command is a critical step in any IaC workflow. It performs a 'dry run' to determine what actions are necessary to achieve the desired state defined in your configuration files.

This step allows you to review proposed changes before anything is actually modified in your cloud environment.

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

Quick Check: Terraform Plan

Understanding the core commands is key to using Terraform effectively in your automated workflows.

Terraform Apply Considerations

The terraform apply command is where the magic happens – it makes actual changes to your infrastructure.

In automated pipelines, it's common practice to trigger terraform apply only after a human review of the plan output, or only when merging to specific branches (e.g., main or production). This prevents accidental or unapproved infrastructure changes.

IaC with Terraform & Actions Recap

In this lesson, we explored how Infrastructure as Code (IaC) helps manage infrastructure using code. We focused on Terraform as the leading tool for IaC, learning about its HCL syntax and the essential init, plan, apply workflow.

You also saw how GitHub Actions can automate terraform init and terraform plan steps, making your infrastructure deployments safer, more consistent, and fully integrated into your CI/CD pipelines.

Frequently asked questions

Is the “IaC with Terraform and GitHub Actions” lesson free?

Yes — the full text of “IaC with Terraform and GitHub Actions” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “IaC with Terraform and GitHub Actions”?

Understand how to integrate Terraform into your GitHub Actions workflows to provision and manage infrastructure. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “IaC with Terraform and GitHub Actions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. IaC with Terraform and GitHub Actions
  2. Managing Cloud Resources
  3. Automating Infrastructure Updates
  4. Managing Terraform State and Remote Backends
← Back to DevOps Bootcamp