Terraform in GitHub Actions - Time & Space Complexity
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?"