Search code examples
terraformterraform-provider-azureterraform-modules

Terraform module writing with different SKU options


I'm looking to create a TF module for provisioning Azure Redis Cache and I'd like to know how to write a module that caters for the different SKU types that the module might be asked to provision.

For example - in Redis there are Basic, Standard and Premium options - and with Premium for example has some options that aren't available for the other SKU options, for example the redis_configuration block has x3 values - rdb_backup_frequency, rdb_backup_max_snapshot_count and rdb_storage_connection_string

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/redis_cache

How do I code a module that will work for non-prod environments that won't have these values, and a prod environment that would?

The official documentation circumvents this question by just listing x4 different seperate examples https://github.com/hashicorp/terraform-provider-azurerm/tree/main/examples/redis-cache

I'd rather use a single module for this sort of thing, if possible

Thanks in advance


Solution

  • As mentioned in my comment you could use conditional logic to set the modules attribute to null when not need as in the case when the sku is basic you can see these values to to null

      rdb_backup_enabled = var.sku == "basic" ? null : var.rdb_backup_enabled
    

    In this case the module will essentially see the value as not set and not try to use it. That being said it will be silently ignored so a user passing a sku type of basic but also passing the rdb_backup_enabled var might not realise their value is being ignored because of the sku type. So it might be worth looking into pre-conditions to error and notify the user of their config mismatch.

    Alternativly you could jsut rely on the modules behaviour and pass these values directly with them defaulting as null then let them module provide the feedback about invalid values.