Workaround for error 'utf-8' codec can't decode byte 0x82 using Azure CLI deployment and Bicep
We migrated most of our automation from ARM Templates to Bicep in the recent months and things have been good so far. We ran into an issue deploying a bicep file containing French characters using Azure CLI. The message was: 'utf-8' codec can't decode byte 0x82 in position 1197: invalid start byte
The problem
I usually write things in English when I code so this was a non-issue for us until... We wanted to write non-compliant messages in French in our Azure Policy. Here is an example that contains the French character é and reproduce my error:
targetScope = 'managementGroup'
var policyAssignmentName = substring(replace(guid('testassignment-#01'), '-', ''), 0, 23)
resource policyAssignment 'Microsoft.Authorization/policyAssignments@2020-09-01' = {
name: policyAssignmentName
properties: {
policyDefinitionId: '/providers/Microsoft.Authorization/policyDefinitions/0473574d-2d43-4217-aefe-941fcdf7e684'
displayName: 'AzCLI and Bicep encoding bug'
description: 'AzCLI and Bicep encoding bug'
enforcementMode: 'Default'
parameters: {
listOfAllowedLocations: {
value: [
'canadaeast'
'canadacentral'
]
}
}
nonComplianceMessages:[
{
message: 'La région qui a été choisi n\'est pas valide...'
}
]
}
}
As you can see, I get the error mentioned at the top when invoking the following command:
az deployment mg create --management-group-id experimental --location canadaeast -f .\azcli-bicep-bug.bicep
'utf-8' codec can't decode byte 0x82 in position 1179: invalid start byte
The error is that the character é is badly handled. UTF-8 is used instead of Unicode in the Bicep wrapper _bicep.py.
An issue has been filled on the GitHub repo of Azure CLI #18029
The workaround
So... this is not working as a one-time operation, calling az deployment
. Bicep can convert to and from ARM Templates. What I ended up doing until this issue is fixed is:
Generate an ARM template from my bicep file and deploy that ARM template. Two steps instead of one.
az bicep build -f .\azcli-bicep-bug.bicep
az deployment mg create --management-group-id experimental --location canadaeast -f .\azcli-bicep-bug.json
Hope it can help some of you!