Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Terraform in GitHub Actions
📖 Scenario: You are working on a cloud infrastructure project. You want to automate the process of applying Terraform configurations using GitHub Actions. This will help you deploy infrastructure changes automatically when you push code to your repository.
🎯 Goal: Build a GitHub Actions workflow file that initializes Terraform, plans the changes, and applies them automatically on the main branch.
📋 What You'll Learn
Create a GitHub Actions workflow YAML file named terraform.yml in the .github/workflows directory.
Set the workflow to trigger on pushes to the main branch.
Add a job named terraform that runs on ubuntu-latest.
Configure steps to checkout the repository, setup Terraform, initialize Terraform, run terraform plan, and run terraform apply with auto-approve.
💡 Why This Matters
🌍 Real World
Automating Terraform deployments with GitHub Actions helps teams deploy infrastructure safely and consistently whenever code changes.
💼 Career
DevOps engineers and cloud architects often use CI/CD pipelines like GitHub Actions to manage infrastructure as code with Terraform.
Progress0 / 4 steps
1
Create the workflow file and trigger
Create a file named terraform.yml inside the .github/workflows folder. Add the workflow name Terraform CI and set it to trigger on push events to the main branch. Write the YAML lines for name and on with push and branches: [main].
Terraform
Hint
Start by defining the workflow name and the event trigger for the main branch.
2
Add the job and runner
Add a job named terraform under jobs. Set the runner to ubuntu-latest. Write the YAML lines for jobs:, terraform:, and runs-on: ubuntu-latest.
Terraform
Hint
Define the job and specify the operating system runner.
3
Add steps to checkout and setup Terraform
Under the terraform job, add a steps section. Add a step named Checkout repository that uses actions/checkout@v3. Then add a step named Setup Terraform that uses hashicorp/setup-terraform@v2 with terraform_version: 1.5.7.
Terraform
Hint
Use the official GitHub Action to checkout code and the HashiCorp action to install Terraform.
4
Add Terraform init, plan, and apply steps
Add three steps under steps: Terraform Init running terraform init, Terraform Plan running terraform plan, and Terraform Apply running terraform apply -auto-approve. Use run to execute these commands.
Terraform
Hint
Run the Terraform commands in order to initialize, plan, and apply the infrastructure changes.
Practice
(1/5)
1. What is the main purpose of using Terraform in GitHub Actions workflows?
easy
A. To store Terraform state files in GitHub issues
B. To manually edit cloud resources from GitHub
C. To automatically run Terraform commands when code changes
D. To replace Terraform CLI with GitHub commands
Solution
Step 1: Understand Terraform automation
Terraform automates cloud resource management by running commands like plan and apply.
Step 2: Role of GitHub Actions
GitHub Actions can trigger these Terraform commands automatically when code changes happen.
Final Answer:
To automatically run Terraform commands when code changes -> Option C
Quick Check:
Terraform automation = automatic runs [OK]
Hint: Terraform in GitHub Actions automates runs on code changes [OK]
Common Mistakes:
Thinking GitHub Actions replaces Terraform CLI
Believing Terraform state is stored in GitHub issues
Assuming manual edits happen inside GitHub
2. Which syntax correctly defines a GitHub Actions step to run terraform init?
easy
A. - name: Terraform Init
command: terraform init
B. - run: terraform init
name: Terraform Init
C. - step: terraform init
run: true
D. - name: Terraform Init
run: terraform init
Solution
Step 1: Check GitHub Actions step syntax
Steps use name and run keys to describe and execute commands.
Step 2: Validate correct order and keys
- name: Terraform Init
run: terraform init uses name then run with the correct command string.
Final Answer:
- name: Terraform Init
run: terraform init -> Option D
Quick Check:
Step keys = name + run [OK]
Hint: GitHub Actions steps use 'name' then 'run' keys [OK]
Common Mistakes:
Using 'command' instead of 'run'
Swapping order of keys causing confusion
Using invalid keys like 'step'
3. Given this GitHub Actions snippet, what will happen when a push occurs?