Search code examples
azureterraformazure-rmazurecaf

How to generate multiple names / use results output?


When using azurecaf to generate multiple names like in the following code, how do I use the results output?

resource "azurecaf_name" "names" {
  name            = var.appname
  resource_type   = "azurerm_resource_group"
  resource_types  = ["azurerm_mssql_database"]
  prefixes        = [var.environment]
  suffixes        = [var.resource_group_location_short]
  random_length   = 5
  clean_input     = false
}

results - The generated name for the Azure resources based in the resource_types list

How to use this? Also, can I somehow debug / print out what results looks like? (I don't know if it is an array, a key-value structure etc)


Solution

  • You can view the results in two common ways. It is applicable to all attributes of the resource.

    [1] Exporting the attribute required as a terraform output.

    • When you add any attribute as an output in your code by default terraform will show you the values with terraform apply.

    In your used case.

    output "caf_name_result" {
      value = azurecaf_name.names.result
    }
    output "caf_name_results" {
      value = azurecaf_name.names.results
    }
    

    Apply the config with the above outputs definitions you will have the below output on your terminal.

    Changes to Outputs:
      + caf_name_result  = (known after apply)
      + caf_name_results = (known after apply)
    azurecaf_name.names: Creating...
    azurecaf_name.names: Creation complete after 0s [id=YXp1cmVybV9yZXNvdXJjZV9ncm91cAlkZXYtcmctc3RhY2tvdmVyZmxvdy15b2RncC13ZXUKYXp1cmVybV9tc3NxbF9kYXRhYmFzZQlkZXYtc3FsZGItc3RhY2tvdmVyZmxvdy15b2RncC13ZXU=]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    caf_name_result = "dev-rg-stackoverflow-yodgp-weu"
    caf_name_results = tomap({
      "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
      "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
    })
    
    • After the first successful terraform apply you can view them anytime when you want by using the terraform output command.
    $ terraform output       
    caf_name_result = "dev-rg-stackoverflow-yodgp-weu"
    caf_name_results = tomap({
      "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
      "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
    })
    

    You can be very specific also to check only particular output value.

    $ terraform output caf_name_results
    tomap({
      "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
      "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
    })
    

    [2] View your applied resources via Terraform State Commands

    This is only available after the resources are applied and only in cases when terraform execution was done from the same machine where this command is running. (in simple the identity doing terraform execution satisfies all the authentication, authorization and network connectivity conditions. )

    It is not recommended, just to share another option available when requiring a quick look on the resources applied.

    $ terraform state list 
    azurecaf_name.names
    $ terraform state show azurecaf_name.names
    # azurecaf_name.names:
    resource "azurecaf_name" "names" {
        clean_input    = false
        id             = "YXp1cmVybV9yZXNvdXJjZV9ncm91cAlkZXYtcmctc3RhY2tvdmVyZmxvdy15b2RncC13ZXUKYXp1cmVybV9tc3NxbF9kYXRhYmFzZQlkZXYtc3FsZGItc3RhY2tvdmVyZmxvdy15b2RncC13ZXU="
        name           = "stackoverflow"
        passthrough    = false
        prefixes       = [
            "dev",
        ]
        random_length  = 5
        random_seed    = 1676730686950185
        random_string  = "yodgp"
        resource_type  = "azurerm_resource_group"
        resource_types = [
            "azurerm_mssql_database",
        ]
        result         = "dev-rg-stackoverflow-yodgp-weu"
        results        = {
            "azurerm_mssql_database" = "dev-sqldb-stackoverflow-yodgp-weu"
            "azurerm_resource_group" = "dev-rg-stackoverflow-yodgp-weu"
        }
        separator      = "-"
        suffixes       = [
            "weu",
        ]
        use_slug       = true
    }