CodexBloom - Programming Q&A Platform

Terraform: implementing Resource Dependencies and Circular Reference scenarios in Azure Deployment

πŸ‘€ Views: 101 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-08
terraform azure circular-dependency HCL

I've been working on this all day and I'm optimizing some code but I'm working with a circular dependency scenario when trying to deploy resources in Azure using Terraform 1.3.5. The goal is to create an Azure Function App that depends on an App Service Plan, which in turn depends on a Resource Group. However, I keep getting the following behavior: ``` behavior: behavior creating Function App "my-function-app": resources.ResourceGroupClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original behavior: Code="CannotCreateFunctionAppWithHostPlanInDifferentResourceGroup" Message="Function App and App Service Plan must be in the same resource group." ``` Here’s a simplified version of the code I’m using: ```hcl variable "resource_group_name" { default = "my-resource-group" } resource "azurerm_resource_group" "example" { name = var.resource_group_name location = "East US" } resource "azurerm_app_service_plan" "example" { name = "my-app-service-plan" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name sku { tier = "Standard" size = "S1" } } resource "azurerm_function_app" "example" { name = "my-function-app" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name app_service_plan_id = azurerm_app_service_plan.example.id storage_account_name = azurerm_storage_account.example.name storage_account_access_key = azurerm_storage_account.example.primary_access_key } resource "azurerm_storage_account" "example" { name = "mystorageaccount" resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location account_tier = "Standard" account_replication_type = "LRS" } ``` I've tried to adjust the resource dependencies by explicitly defining dependencies with `depends_on` in the `azurerm_function_app`, but the behavior continues. I'm unsure if this is a misunderstanding of how Terraform handles resource dependencies or if I'm missing something in the Azure resource configuration. Any guidance on resolving this circular dependency scenario would be greatly appreciated! The project is a REST API built with Hcl. I'm developing on CentOS with Hcl. I'm working on a microservice that needs to handle this.