0
0
Terraformcloud~30 mins

Terraform in GitHub Actions - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Run the Terraform commands in order to initialize, plan, and apply the infrastructure changes.