Coder Perfect

changing the defaultTTL for azure cosmos bicep to null

Problem

Hello, I’ve attempted to disable the TTL for Azure Cosmos. I’m aware that it’s possible to disable it by omitting defaultTtl from the code. However I have conditional deployment, I have a loop that check for tenant name to create cosmos container, if it equal to ‘int999’ then set defaultTtl to 604800 otherwise turn off TTL. I tried to set it to null but the pipeline run failed, it has a very general error message “At least one resource deployment operation failed. Please list deployment operations for details”

Here’s the code for my biceps:

resource containers 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-06-15' = [for i in range(0, length(tenants)): {
  name: '${databases[i].name}/messages'
  properties: {
    resource: {
      id: 'messages'
      partitionKey: {
        paths: [
          '/organisationId'
        ]
      }
      defaultTtl: tenants[i] == 'int999' ? 604800 : null
    }
  }
}] 

com/en-us/azure/cosmos-db/time-to-live. However, it appears that when I set it to null, I get a syntax error. Could someone please assist me? Thanks

Note: In my instance, setting defaultTTL to -1 is not an acceptable approach. We want to totally disable the TTL.

Asked by J Nguyen

Solution #1

I also tried null or json(‘null’) but it didn’t work.

You must add the defaultTtl attribute conditionally based on a condition here. This bicep file defines certain default properties and, if necessary, adds the defaultTtl:

param cosmosdbAccountName string
param databaseName string
param tenant string = 'int999'

// reference to the cosmos db database
resource database 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases@2021-06-15' existing = {
  name: '${cosmosdbAccountName}/${databaseName}'
}

// Create default properties for the container
var defaultResource = {
  id: 'messages'
  partitionKey: {
    paths: [
      '/organisationId'
    ]
  }
}

// Create the container
resource container 'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers@2021-06-15' = {
  name: '${database.name}/messages'
  properties: {
    // Add the default ttl if needed
    resource: union(defaultResource, tenant == 'int999' ? {
      defaultTtl: 604800
    } : {})
  }
}

Answered by Thomas

Post is based on https://stackoverflow.com/questions/68796160/setting-null-value-for-defaultttl-for-azure-cosmos-bicep