Home > Back-end >  Bicep does not resolve 'foo${uniqueString(resourceGroup().id)}' when deploying from VSCode
Bicep does not resolve 'foo${uniqueString(resourceGroup().id)}' when deploying from VSCode

Time:01-22

I'm trying to deploy a Bicep template directly from VSCode with the Bicep extension, but it consistently fails. It works fine when I deploy from PowerShell using the New-AzResourceGroupDeployment cmdlet, though. Am I doing something wrong or is it a bug?

Bicep template deploys a new storage account and contains the following parameter for the storage account's name:

param storageName string = 'mystorage${uniqueString(resourceGroup().id)}'

Error when deploying from VSCode

When I deploy from VSCode, it does not resolve storageName automatically and prompts me to confirm it:

VScodePrompt

I believe this prompt is unexpected. storageName should be resolved automatically. I press Enter to confirm. This parameter should be resolved automatically alter after all.

And it fails when I attempt to deploy this Bicep template from VSCode. Here is the error status message:

{
    "status": "Failed",
    "error": {
        "code": "AccountNameInvalid",
        "message": "[format('mystorage{0}', uniqueString(resourceGroup().id))] is not a valid storage account name. Storage account name must be between 3 and 24 characters in length and use numbers and lower-case letters only."
    }
}

As far as I see, for some reason, the storageName parameter was not resolved into an actual string (see the last line of the output). So it's expected that ARM does not let my deploy this template.

Get-AzResourceGroupDeployment -ResourceGroupName VSCodeBicepTest-RG -Name MyTestDeployment-FAILS   

DeploymentName          : MyTestDeployment-FAILS
ResourceGroupName       : VSCodeBicepTest-RG
ProvisioningState       : Failed
Timestamp               : 19/01/2023 19:56:37
Mode                    : Incremental
TemplateLink            : 
Parameters              : 
                          Name             Type                       Value     
                          ===============  =========================  ==========
                          location         String                     "northeurope"
                          storageName      String                     "[format('mystorage{0}', uniqueString(resourceGroup().id))]"

Works fine from PowerShell

Everything seems to be fine when I deploy the template from PowerShell. The deployment has succeeded:

New-AzResourceGroupDeployment -Name MyTestDeployment-SUCCEEDS -ResourceGroupName VSCodeBicepTest-RG -TemplateFile ./sample-template.bicep

DeploymentName          : MyTestDeployment-SUCCEEDS
ResourceGroupName       : VSCodeBicepTest-RG
ProvisioningState       : Succeeded
Timestamp               : 19/01/2023 19:56:16
Mode                    : Incremental
TemplateLink            : 
Parameters              : 
                          Name             Type                       Value     
                          ===============  =========================  ==========
                          location         String                     "northeurope"
                          storageName      String                     "mystoragedcgcd6nvj5tje"

As you may see, the storageName parameter was correctly resolved into the string mystoragedcgcd6nvj5tje. That's expected.

Right now, I don't know why this is happening, but it looks like a bug in the Bicep extension for VSCode.

VSCode 1.74.3 (Universal) on MacOS, Bicep extension v0.13.1.

CodePudding user response:

This appears to be a known bug. As a workaround, you may wrap your interpolated parameters with string().

E.g., change this:

param storageName string = 'mystorage${uniqueString(resourceGroup().id)}'

To this:

param storageName string = string('mystorage${uniqueString(resourceGroup().id)}')
  • Related