0
0
Terraformcloud~10 mins

Terraform in GitHub Actions - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Terraform in GitHub Actions
Start: Push code to GitHub
GitHub Actions workflow triggers
Set up Terraform environment
Terraform init: Prepare backend and providers
Terraform plan: Show changes
Terraform apply: Make changes
Workflow ends with success or failure
This flow shows how pushing code triggers GitHub Actions to run Terraform commands step-by-step.
Execution Sample
Terraform
name: Terraform
on: [push]
jobs:
  terraform:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: hashicorp/setup-terraform@v2
      - run: terraform init
      - run: terraform plan
      - run: terraform apply -auto-approve
This GitHub Actions workflow runs Terraform init, plan, and apply on every push.
Process Table
StepActionCommand RunResultNext Step
1Trigger workflowpush eventWorkflow startsCheckout code
2Checkout codeactions/checkout@v3Code availableSetup Terraform
3Setup Terraformhashicorp/setup-terraform@v2Terraform readyterraform init
4Terraform initterraform initBackend and providers initializedterraform plan
5Terraform planterraform planPlan shows changesterraform apply
6Terraform applyterraform apply -auto-approveInfrastructure updatedWorkflow ends
7EndN/ASuccess or failure reportedStop
💡 Workflow stops after apply completes or if any step fails.
Status Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
Code checked outNoYesYesYesYesYes
Terraform installedNoYesYesYesYesYes
Backend initializedNoNoYesYesYesYes
Plan createdNoNoNoYesYesYes
Infrastructure appliedNoNoNoNoYesYes
Key Moments - 3 Insights
Why does the workflow fail if 'terraform init' is not successful?
'terraform init' sets up backend and providers needed for plan and apply. Without it, later steps cannot run, as shown in execution_table step 4.
What happens if 'terraform plan' shows no changes?
Even if no changes are planned, 'terraform apply' runs but makes no updates. This is shown in execution_table step 5 and 6.
Why do we use '-auto-approve' in 'terraform apply'?
It skips manual confirmation so the workflow can run unattended, as seen in execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 4?
ACode available
BBackend and providers initialized
CPlan shows changes
DInfrastructure updated
💡 Hint
Check the 'Result' column for step 4 in execution_table.
At which step does the infrastructure get updated?
AStep 6
BStep 4
CStep 3
DStep 5
💡 Hint
Look for 'Infrastructure updated' in the 'Result' column.
If the 'terraform init' step fails, what happens next?
AWorkflow continues to plan
BApply runs anyway
CWorkflow stops with failure
DCode checkout repeats
💡 Hint
Refer to the exit_note and step 4 in execution_table.
Concept Snapshot
Terraform in GitHub Actions:
- Triggered by code push
- Steps: checkout, setup, init, plan, apply
- 'terraform init' prepares environment
- 'terraform plan' previews changes
- 'terraform apply -auto-approve' updates infra
- Workflow stops on failure or success
Full Transcript
This visual execution shows how Terraform runs inside GitHub Actions. When code is pushed, the workflow starts and checks out the code. Then it sets up Terraform. Next, 'terraform init' prepares the backend and providers. After that, 'terraform plan' shows what changes will happen. Finally, 'terraform apply -auto-approve' makes the changes automatically. The workflow ends with success or failure depending on these steps. Variables like code checked out and infrastructure applied change step-by-step. Key points include the importance of 'terraform init' and the use of '-auto-approve' to avoid manual input. The quiz questions help check understanding of each step's result and workflow behavior.