I have this bicep code to apply two rules to a storage account policy on an Azure Storage Account:
@description('Name of the storage account')
param name string
@description('Number of days before moving from Hot to Cool storage')
param hotToCoolDays int
@description('Number of days to housekeep blob storage')
param houseKeepDays int
// the storage account to apply the policy to
resource stg 'Microsoft.Storage/storageAccounts@2021-08-01' existing = {
name: name
}
resource sapolicy 'Microsoft.Storage/storageAccounts/managementPolicies@2021-02-01' = {
name: '${name}/default'
dependsOn: [
stg
]
properties: {
policy: {
rules: [
{
enabled: true
name: 'hotToCoolAfter${hotToCoolDays}Days'
type: 'Lifecycle'
definition: {
actions: {
baseBlob: {
tierToCool: {
daysAfterModificationGreaterThan: hotToCoolDays
}
}
}
filters: {
blobTypes: [
'blockBlob'
]
}
}
}
{
enabled: true
name: 'housekeepAfter${houseKeepDays}Days'
type: 'Lifecycle'
definition: {
actions: {
baseBlob: {
delete: {
daysAfterModificationGreaterThan: houseKeepDays
}
}
}
filters: {
blobTypes: [
'blockBlob'
]
}
}
}
]
}
}
}
I want to omit the rule if either variable hotToCoolDays
or houseKeepDays
is 0. I’ve tried using if
statement and the ternary operator but can’t get either to work. Any ideas?
2
Answers
I figured it out with
You can check the documentation: Conditional deployment in Bicep.
For you it should look like that: