Terraform in GitHub Actions - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Terraform inside GitHub Actions, it's important to understand how the time to complete tasks grows as you add more resources.
We want to know how the number of Terraform operations affects the total execution time.
Analyze the time complexity of running Terraform apply on multiple resources in a GitHub Actions workflow.
name: "Terraform Apply"
on:
push:
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- uses: "actions/checkout@v3"
- uses: "hashicorp/setup-terraform@v3"
- name: "Terraform Init"
run: terraform init
- name: "Terraform Apply"
run: terraform apply -auto-approve
This workflow runs Terraform commands to initialize and apply infrastructure changes on each push.
Look at what happens repeatedly during the Terraform apply step.
- Primary operation: Terraform API calls to create or update each resource.
- How many times: Once per resource defined in the Terraform configuration.
As you add more resources, Terraform makes more API calls to create or update them.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 API calls |
| 100 | About 100 API calls |
| 1000 | About 1000 API calls |
Pattern observation: The number of API calls grows directly with the number of resources.
Time Complexity: O(n)
This means the time to apply infrastructure changes grows in a straight line as you add more resources.
[X] Wrong: "Terraform apply time stays the same no matter how many resources I have."
[OK] Correct: Each resource needs its own API call, so more resources mean more work and longer time.
Understanding how Terraform operations scale helps you design efficient workflows and manage infrastructure growth confidently.
"What if we split the Terraform apply into multiple smaller jobs in GitHub Actions? How would the time complexity change?"
Practice
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 CQuick Check:
Terraform automation = automatic runs [OK]
- Thinking GitHub Actions replaces Terraform CLI
- Believing Terraform state is stored in GitHub issues
- Assuming manual edits happen inside GitHub
terraform init?Solution
Step 1: Check GitHub Actions step syntax
Steps usenameandrunkeys to describe and execute commands.Step 2: Validate correct order and keys
- name: Terraform Init run: terraform init usesnamethenrunwith the correct command string.Final Answer:
- name: Terraform Init run: terraform init -> Option DQuick Check:
Step keys = name + run [OK]
- Using 'command' instead of 'run'
- Swapping order of keys causing confusion
- Using invalid keys like 'step'
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Terraform Init
run: terraform init
- name: Terraform Plan
run: terraform planSolution
Step 1: Analyze the steps in the workflow
The workflow checks out code, runsterraform init, then runsterraform plan.Step 2: Understand Terraform commands effect
terraform initprepares the environment;terraform planshows what changes would happen but does not apply them.Final Answer:
Terraform will initialize and then create a plan for changes -> Option BQuick Check:
Init + Plan = prepare and preview [OK]
- Confusing plan with apply
- Assuming apply runs automatically
- Ignoring checkout step importance
- name: Terraform Apply run: terraform apply -auto-approveWhat is a common reason for failure in this context?
Solution
Step 1: Check Terraform command requirements
Terraform requiresterraform initto run first to set up backend and providers.Step 2: Identify missing initialization
Ifterraform initis missing,terraform applywill fail due to uninitialized state.Final Answer:
Missingterraform initbefore apply -> Option AQuick Check:
Init must run before apply [OK]
- Thinking -auto-approve causes failure
- Believing GitHub Actions blocks apply commands
- Assuming step name affects execution
terraform apply only after manual approval in GitHub Actions. Which setup is best?Solution
Step 1: Secure state storage best practice
Remote backends like AWS S3 keep Terraform state safe and shared among users.Step 2: Implement manual approval in workflow
GitHub Actions supports manual approval jobs to pause before applying changes.Step 3: Combine both for safe, controlled deployment
Using remote state plus manual approval ensures safety and control over apply.Final Answer:
Use a remote backend like AWS S3 for state and add a manual approval job before apply -> Option AQuick Check:
Remote state + manual approval = safe apply [OK]
- Storing state in repo risking conflicts
- Running apply automatically without checks
- Using secrets to store entire state file
