Ken Muse

Fixing APIM Tags - Name Should Not Be Empty

Diving a bit more into using infrastructure as code to deploy Azure API Management using Bicep, there’s another interesting error that sometimes occurs. In this case, it happens when creating tags.

Bicep enforces the published Azure schema with each resource. This normally makes it very easy to define resources with full error checking. Taking advantage of this makes defining a tag surprisingly simple.

1resource apimTagAccountMgmt 'Microsoft.ApiManagement/service/tags@2021-08-01' = {
2  parent: apimService
3  name: 'AcctMgmt'

Unfortunately, deploying the template results in a surprising error: 'Name' should not be empty. The name field is populated, so the error message makes no sense.

It turns out that this is likely a bug in the service’s published Swagger definition. The actual issue appears to be that the resource provider expects the displayName property and resource name to both be present and match. If the displayName is missing, we see this misleading error. The solution is rather straightforward – just add the missing displayName property to the resource definition. Unfortunately, Bicep generates a warning if we do that:

1The property "displayName" is read-only. Expressions cannot be assigned to read-only properties. (BCP073)

Until recently, these kinds of issues from the linter were harder to deal with. Thankfully, a nice addition to the Bicep language arrived in December. This new feature, #disable-next-line, allows us to disable specific warnings. Suppressing these diagnostic messages provides a way to ignore warnings that are caused by invalid resource schema definitions. The code(s) from any linter warnings should immediately follow the directtive. Bicep will automatically stop reporting the specified warnings for the next line of code. Nice, right?

Revising the code above, we get this corrected version:

1resource apimTagAccountMgmt 'Microsoft.ApiManagement/service/tags@2021-08-01' = {
2  parent: apimService
3  name: 'AcctMgmt'
4  properties: {
5    #disable-next-line BCP073
6    displayName: 'AcctMgmt'
7  }