Share via

Azure CLI code in lesson not deploying - AI says it's out of date

Daniel Cecchini 0 Reputation points
2026-05-20T18:07:54.5066667+00:00

The following CLI code will not deploy. AI says it's out of date for Azure and everything I have tried to fix it is not working. I can't complete this Bicep lesson if it won't deploy. This comes from - https://learn.microsoft.com/en-us/training/modules/build-flexible-bicep-files-conditions-loops/ - Here is the code that Learn says is correct :

@description('The Azure region into which the resources should be deployed.')

param location string

@secure()

@description('The administrator login username for the SQL server.')

param sqlServerAdministratorLogin string

@secure()

@description('The administrator login password for the SQL server.')

param sqlServerAdministratorLoginPassword string

@description('The name and tier of the SQL database SKU.')

param sqlDatabaseSku object = {

name: 'Standard'

tier: 'Standard'

}

@description('The name of the environment. This must be Development or Production.')

@allowed([

'Development'

'Production'

])

param environmentName string = 'Development'

@description('The name of the audit storage account SKU.')

param auditStorageAccountSkuName string = 'Standard_LRS'

var sqlServerName = 'teddy${location}${uniqueString(resourceGroup().id)}'

var sqlDatabaseName = 'TeddyBear'

var auditingEnabled = environmentName == 'Production'

var auditStorageAccountName = take('bearaudit${location}${uniqueString(resourceGroup().id)}', 24)

resource sqlServer 'Microsoft.Sql/servers@2024-05-01-preview' = {

name: sqlServerName

location: location

properties: {

administratorLogin: sqlServerAdministratorLogin

administratorLoginPassword: sqlServerAdministratorLoginPassword

}

}

resource sqlDatabase 'Microsoft.Sql/servers/databases@2024-05-01-preview' = {

parent: sqlServer

name: sqlDatabaseName

location: location

sku: sqlDatabaseSku

}

resource auditStorageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' = if (auditingEnabled) {

name: auditStorageAccountName

location: location

sku: {

name: auditStorageAccountSkuName

}

kind: 'StorageV2'

}

resource sqlServerAudit 'Microsoft.Sql/servers/auditingSettings@2024-05-01-preview' = if (auditingEnabled) {

parent: sqlServer

name: 'default'

properties: {

state: 'Enabled'

storageEndpoint: environmentName == 'Production' ? auditStorageAccount.properties.primaryEndpoints.blob : ''

storageAccountAccessKey: environmentName == 'Production' ? auditStorageAccount.listKeys().keys[0].value : ''

}

}

Here are the 2 error codes I'm getting which won't allow it to deploy:

  • The value of type "Microsoft.Storage/storageAccounts | null" may be null at the start of the deployment, which would cause this access expression (and the overall deployment with it) to fail.
  • A resource of type "Microsoft.Storage/storageAccounts | null" may or may not exist when this function is called, which could cause the deployment to fail.

I have included a screen shot of where VS Code says the errors lie. AI gave me several 'fixes' which just lead to more and different error codes that still wouldn't deploy. Screenshot from 2026-05-20 12-54-26.png - Here's what I need help with:

  1. Is it out of date? I'm thinking so because it won't deploy and AI says it is. If not, then what am I doing wrong?
  2. How do I fix it if it is? AI had me running in circles, including adding ! and ?, as well as adding modules as workarounds.
  3. Has anyone else had the issue of Learn lessons being out of date? Is this common? I'm new to this.

Thanks in advance for any advice you can give.

This question is related to the following Learning Module

Azure | Azure Training
0 comments No comments

1 answer

Sort by: Most helpful
  1. Divyesh Govaerdhanan 11,065 Reputation points MVP Volunteer Moderator
    2026-05-21T16:29:31.7833333+00:00

    Hi Daniel Cecchini,

    Welcome to Microsoft Q&A,

    When you declare a resource conditionally with = if (...), newer versions of the Bicep compiler treat that resource as a type Microsoft.Storage/storageAccounts | null because it may or may not exist at deployment time. When you then access .properties.primaryEndpoints.blob or call .listKeys() on it, the compiler raises diagnostic BCP318 because it cannot guarantee the resource exists at that point. The training module was written before the Bicep compiler introduced stricter null safety checks, which is why it worked then, but now flags.

    Add the null-forgiving operator (!) directly after auditStorageAccount in these two lines inside your sqlServerAudit resource:

    storageEndpoint: environmentName == 'Production' ? auditStorageAccount!.properties.primaryEndpoints.blob : ''
    storageAccountAccessKey: environmentName == 'Production' ? auditStorageAccount!.listKeys().keys[0].value : ''
    

    The ! tells the Bicep compiler "I know this resource is not null here, trust the condition." Since both lines are already guarded by environmentName == 'Production' which matches the same condition the storage account deploys under, this is safe to use.

    Please Upvote and accept the answer if it helps!!

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.