I recently worked on an infrastructure deployment using Terraform + Azure, and I hit a problem that made my head spin for hours. Everything in my code looked logically perfect, yet the deployment kept failing. Only later I discovered that it was not a โcode issueโ but a dependency issue and it taught me a valuable Terraform lesson.
Let me share that journey, so nobody else loses hours like I did.
๐ฅ The Setup
I was provisioning multiple resources using Terraform, including:
- Azure Redis Cache
- Azure App Service, which needed the Redis connection string as an environment variable
On paper, the flow looked simple:
Create Redis โ get connection string โ use it in App Service
But reality had other plans.
โ The Unexpected Failure
Whenever I ran terraform apply, the Redis creation startedโฆ
But while Redis was still provisioning (and Azure Redis usually takes time), Terraform tried to create the App Service immediately.
As a result:
Error: Redis endpoint not found
I double-checked the code multiple time everything looked correct!
So why was it failing?
๐ง The Hidden Reason Terraform Doesn't Automatically Wait
Terraform executes resources in parallel by default to speed up deployment.
It only waits for dependencies if they are explicitly defined.
Even though I used Redis output inside the App Service configuration, Terraform didnโt treat it as a strict dependency and started creating both resources simultaneously.
Redis was still provisioning โ App Service needed Redis โ App Service failed.
That was the missing piece.
๐ง The Fix depends_on
I added an explicit dependency in the App Service resource:
depends_on = [
azurerm_redis_cache.redis
]
And boom Terraform waited for Redis to finish provisioning before creating the App Service. Deployment succeeded. ๐ฏ
๐ท What I Learned
Hereโs the takeaway from this experience:
Without depends_on
|
With depends_on
|
|---|---|
| Terraform runs resources in parallel | Terraform respects resource order |
| May fail if referenced resource isnโt ready | Ensures resource readiness before execution |
| Hidden debugging headaches | Clear creation sequence |
Terraform is smart, but not psychic it wonโt guess resource order unless we tell it.
โ Final Thoughts
This issue took me hours to understand, but it taught me something crucial:
Terraform isn't just โwriting infra as codeโ itโs also about controlling resource orchestration.
Next time you are creating interdependent services:
- If one resource must finish before another starts โ use
depends_on - Donโt assume Terraform will wait automatically
๐ฌ Your Turn
Did you ever face something similar with Terraform parallel execution or Azure resource delays?
Iโd love to hear your war stories and fixes!
Top comments (0)