conditonal infrastructrue changes with Terraform


I am trying to do some conditional changes in terraform.

    resource "azurerm_mssql_database" "war" {
  name                        = "${local.clSqlDatabaseName}-${var.environment}"
  server_id                   = jokes
  min_capacity                = 0.5
  max_size_gb                 = 100
  zone_redundant              = false
  collation                   = "SQL_Latin1_General_CP1_CI_AS"
  sku_name                    = "GP_S_Gen5_2"
  auto_pause_delay_in_minutes = "${var.environment == "Prod" ? -1 : 60}"

  short_term_retention_policy {
   retention_days = 35
   }

auto_pause_delay_in_minutes is dependent on the environment variable == Prod and thats fair enough , but how do i make the short_term_retention_policy be dependent on the Prod variable too ?

---------------Answer---------------

You would want to make the short_term_retention_policy dynamic:

resource "azurerm_mssql_database" "war" {
  name                        = "${local.clSqlDatabaseName}-${var.environment}"
  server_id                   = jokes
  min_capacity                = 0.5
  max_size_gb                 = 100
  zone_redundant              = false
  collation                   = "SQL_Latin1_General_CP1_CI_AS"
  sku_name                    = "GP_S_Gen5_2"
  auto_pause_delay_in_minutes = "${var.environment == "Prod" ? -1 : 60}"

  dynamic "short_term_retention_policy" {
    for_each = var.environment == "Prod" ? [1] : []

    content {
      retention_days = 35
    }
  }
}

This short_term_retention_policy will be applied only if var.environment is "Prod", otherwise it will be ignored.


Previous : Why am I getting this runtime error? Line 1034: Char 9: runtime error: reference binding to null pointer of type 'int'
Next : How to display selected option in select dropdown in React JS edit form