Terraform: Export Object

less than 1 minute read

Description:

Short post, but I wanted to help someone with a module and I noticed that the object wasn’t being exported from the module because it was using a count. Here is how I fixed it:

To Resolve:

  1. First, if you want specific properties that is easy when you have a resource with count, you just use a wildcard like so:

    1
    2
    3
    4
    5
    6
    7
    
    output "databasesql_database_ids" {
    value = azurerm_mssql_database.sql_database.*.id
    }
    
    output "databasesql_database_names" {
    value = azurerm_mssql_database.sql_database.*.name
    }
    
    • Note that the same link says that if I were to instead use for_each, the outputs would be like:
    1
    2
    3
    4
    5
    6
    7
    
    output "databasesql_database_ids" {
    value = [for value in azurerm_mssql_database.sql_database: value.id]
    }
    
    output "databasesql_database_names" {
    value = [for value in azurerm_mssql_database.sql_database: value.name]
    }
    
  2. But what if you want all of the properties? There doesn’t seem to be many examples of this online but the fix (for a resource with count) is to just not include the properties:

    1
    2
    3
    
    output "databasesql_database_objects" {
    value = azurerm_mssql_database.sql_database[*]
    }
    
    • I haven’t tested, but I assume with for_each you would do
    1
    2
    3
    
    output "databasesql_database_objects" {
    value = [for value in azurerm_mssql_database.sql_database: value]
    }
    

Comments