I cannot set a variable to be optional for azure_mssql_db 'license_type'

Hi folks, I’m struggling when trying to set a variable that TF will understand it’s an optional variable for a specific module.

I’m trying to set the variable license_type for mssql database as follows:

variable "database_license_type" {
  type    = string
  default = "LicenseIncluded"
}

and then I tried to define the license_type under resource in both ways, but didn’t work:

resource "azurerm_mssql_database" "sql-database" {
license_type                = "${ var.serverless == "true" ? null : "LicenseIncluded"}"'
*or*
license_type                = coalesce(each.value.serverless, true) ? try(each.value.license_type, var.database_license_type) : null
}

also tried to declare license_type under locals:

locals {
  license_type = "${ var.serverless == "true" ? null : "LicenseIncluded"}"
}

-Also tried to define the var as null, but didn’t help!-
The error I get after running the plan is:

│ Error: serverless databases do not support license type
│ 
│   with module.sup-fp-we-sql-server.azurerm_mssql_database.sql-database["sup-profi-processengine"],
│   on ../modules/tf-azurerm-sql/database.tf line 1, in resource "azurerm_mssql_database" "sql-database":
│    1: resource "azurerm_mssql_database" "sql-database" {
│ 
╵
╷
│ Error: serverless databases do not support license type
│ 
│   with module.sup-fp-we-sql-server.azurerm_mssql_database.sql-database["sup-job-database"],
│   on ../modules/tf-azurerm-sql/database.tf line 1, in resource "azurerm_mssql_database" "sql-database":
│    1: resource "azurerm_mssql_database" "sql-database" {

it seems to be already an optional value in Terraform azurerm registry:

  • license_type - (Optional) Specifies the license type applied to this database. Possible values are LicenseIncluded and BasePrice .

I think I only saw this error after upgrading the azurerum module to v3.0.2

To me, it seems that Terraform cannot omit the variable and treat it as an optional one, any ideas?

I appreciate your great support ! :slight_smile:
Best, Kareem

Hi @karl18,

You didn’t include the definition of variable "serverless" here and so I can’t be sure if this is the cause but I notice you are comparing it to "true" rather than true, and so if variable "serverless" is a boolean value then that expression will always return false: "true" and true are never equal to one another.

Unless you have a strong reason for representing this boolean value as a string, I would suggest declaring it as being of type bool and then using it consistently as a boolean value:

variable "serverless" {
  type = bool
}

resource "azurerm_mssql_database" "sql-database" {
  # ...

  license_type = var.serverless ? null : "LicenseIncluded"
}

The declaration as type = bool means that the value is guaranteed to be a boolean value, and that means you can use var.serverless directly as the condition to decide. If it’s true then the result will be null, and if it’s false then the result will be LicenseIncluded.

Hi @apparentlymart
Thanks a lot for your reply, I believe that I defined the variable "serverless" as a type=bool already. Sorry forgot to mention this

variable "serverless" {
  type = bool
}

I think the amazing support from your side was to mention that I could leverage var.serverless directly with the condition to decide. Happy to make the value null anyway!

Thanks a lot @apparentlymart !