setting null value for defaultTTL for azure cosmos bicep

Viewed 828

Hi I have tried to turn off the TTL for Azure cosmos. I know that it could be turn off by Not include defaultTtl in 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"

enter image description here

Here is my bicep code

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
    }
  }
}] 

I feel weird cause in the azure documents, defaultTTL should accept null value https://docs.microsoft.com/en-us/azure/cosmos-db/time-to-live. However it seem I face a syntax error when set it to null here. Could anyone help please. Thanks

Note: Please note that set defaultTTL to -1 is not acceptable solution for my case. we want to turn off the TTL completely.

1 Answers

I tried as well the null or json('null') and it didnt work.

Here you need to conditionally add the defaultTtl property based on a condition. This bicep file defines some default properties and add the defaultTtl if required:

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
    } : {})
  }
}

Related