Reputation: 154
Can we make argument usage present in a resource block optional for eg like in below resource block table_name , mapping_rule_name and data_format are optional parameter and I want to have a standard format for terraform where I can make these values optional for e.g like if var.table_name variable has value it should provide it to table_name otherwise table_name should be ignored i.e. eventhubconnection be formed without table_name,mapping_rule_name and data_format as these are optional value
resource "azurerm_kusto_eventhub_data_connection" "eventhub_connection" {
name = "my-kusto-eventhub-data-connection"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
cluster_name = azurerm_kusto_cluster.cluster.name
database_name = azurerm_kusto_database.database.name
eventhub_id = azurerm_eventhub.eventhub.id
consumer_group = azurerm_eventhub_consumer_group.consumer_group.name
table_name = var.table_name #(Optional)
mapping_rule_name = var.mapping_rule_name #(Optional)
data_format = var.data_format #(Optional)
}
Is there any way to do that in terraform?
Upvotes: 0
Views: 7324
Reputation: 1537
You can use Conditionally Omitted Arguments, which allow for variables with the value null
to be interpreted as "unset" by Terraform. I like to use the following pattern to make more "toggle-able" attributes leveraging this feature:
# variables.tf
variable "table_name" {
type = string
# ...
default = null
}
variable "mapping_rule_name" {
type = string
# ...
default = null
}
# myterraform.tfvars
table_name = "my-table-name"
# eventhub.tf
resource "azurerm_kusto_eventhub_data_connection" "eventhub_connection" {
# ...
table_name = var.table_name # instantiated to value in .tfvars
mapping_rule_name = var.mapping_rule_name # is "unset" by default null value
}
If you'd like to do the same, but with nested blocks of attributes instead of just attributes, you can check out Dynamic Nested Blocks and apply conditional logic to determine whether said blocks will be generated.
Upvotes: 1