fbpx

Learn Build5Nines Forum

Find answers, ask questions, and connect with our
global community of IT professionals.

Forums HashiCorp Terraform Azure Terraform with depends_on with modules

  • Azure Terraform with depends_on with modules

    Posted by Joshua Falken on March 15, 2024 at 3:10 pm

    I’m new at terraform and I created a custom azure policies on module structure. each policy represents a custom module. One of the modules that I have created is enabling diagnostics logs for any new azure resource created. but, I need a storage account for that. (before enabling the diagnostics settings how can I implement “depends_on”? or any other methods? I want to create first the storage account and then the module of diagnostics settings. on the main.tf (where calling all the other modules) or inside the resource (module)?

    Thanks for the help!! 🙂

    this below code represents the main.tf file:

    //calling the create storage account name
    
    module "createstorageaccount" {
    
    source = "./modules/module_create_storage_account"
        depends_on = [
        "module_enable_diagnostics_logs"
      ]
    
    }
    

    this one represents the create storage account module

    resource "azurerm_resource_group" "management" {
    
    
      name     = "management-rg"
      location = "West Europe"
    }
    
    resource "azurerm_storage_account" "test" {
      name                     = "diagnostics${azurerm_resource_group.management.name}"
      resource_group_name      = "${azurerm_resource_group.management.name}"
      location                 = "${azurerm_resource_group.management.location}"
      account_tier             = "Standard"
      account_replication_type = "LRS"
    
      tags = {
        environment = "diagnostics"
      }
    }
    
        depends_on = [
        "module_enable_diagnostics_logs"
      ]
    

     

    Joshua Falken replied 1 month, 2 weeks ago 1 Member · 1 Reply
  • 1 Reply
  • Joshua Falken

    Member
    March 15, 2024 at 3:11 pm

    Based on the information provided in the retrieved documents and your description, it seems you are looking to manage dependencies between Terraform modules, specifically ensuring that a storage account is created before enabling diagnostic settings. The depends_on argument in Terraform is used to handle such dependencies, but it is important to note that depends_on cannot be used within a module block to refer to resources in another module. Instead, it can only be used within resource and data blocks to handle dependencies.

    To achieve the desired outcome, you should reverse the dependency order in your main.tf file. The module_enable_diagnostics_logs should depend on the module_createstorageaccount, not the other way around. Here’s how you can adjust your main.tf:

    // Calling the create storage account module
    module "createstorageaccount" {
      source = "./modules/module_create_storage_account"
    }
    
    // Calling the enable diagnostics logs module
    module "enable_diagnostics_logs" {
      source = "./modules/module_enable_diagnostics_logs"
    
      // Add a variable in the diagnostics module to accept the storage account ID
      storage_account_id = module.createstorageaccount.storage_account_id
    
      // Use depends_on to ensure the storage account is created first
      depends_on = [
        module.createstorageaccount
      ]
    }
    

    In the module_enable_diagnostics_logs, you would need to add a variable to accept the storage account ID from the module_createstorageaccount. This way, the dependency is explicitly defined, and Terraform will create the storage account before the diagnostics settings.

    Please note that the actual implementation may vary based on the structure of your modules and the resources defined within them. The above example assumes that you have an output named storage_account_id in your module_createstorageaccount that provides the ID of the created storage account.

    The retrieved documents do not contain specific examples of Terraform code for implementing depends_on with modules, as they focus on different Terraform use cases such as creating a lab in Azure DevTest Labs, creating an Azure Container Instance, and configuring Azure DDoS Network Protection. However, the general principles of Terraform and module dependencies apply.

     

    FYI, This answer was written with the help of AI 🙂

Log in to reply.