Search code examples
azure-resource-managerazure-bicepazure-deployment

Unable to edit or replace deployment {deploymentName}: previous deployment from {datetime} is still active (expiration time is {datetime})


I'm trying to deploy an Azure Bicep template with

az deployment sub create --name "deploymentName" --template-file template.json --location="westeurope" --parameters ...

When I go to Azure Portal to review my resource group deployments I can see this error message: Unable to edit or replace deployment {deploymentName}: previous deployment from {datetime} is still active (expiration time is {datetime})

enter image description here

And, If I access to the wrong deployment I can't see any error. Azure says Your deployment is complete.

enter image description here

What's wrong? This error occurs intermittently, on different resources each time. We need delete the wrong deployments many times to make it works.

I reviewed my Azure Bicep template and all resources contains the depends_on clause in order to avoid concurrency issues


Solution

  • It is crucial to ensure your deployments (in other words bicep modules) have unique names in runtime.

    You generate a bicep module name using the current DateTime and probably reuse it within the main deployment. The code below shows an idea of what happens to you (secrets and secrets2 modules end up with the same name):

    param now string = utcNow('u')
    
    module secrets 'resources/common/secrets.bicep' = {
      name: 'kv-${now}'
    }
    
    /* another bicep file within the same main deployment */    
    
    param now string = utcNow('u')
    
    module secrets2 'resources/common/secrets.bicep' = {
      name: 'kv-${now}'
    }
    

    I would not recommend relying on dates, since deployments must be idempotent. Instead use paramers or variables that provide context where the module is reused. For example, systemShortName:

    param systemShortName string = 'sql-secrets'
    
    module secrets 'resources/common/secrets.bicep' = {
      name: 'kv-${systemShortName}'
    }
    
    /* another bicep file within the same main deployment */ 
    
    param systemShortName string = 'storageaccount-secrets'
    
    module secrets2 'resources/common/secrets.bicep' = {
      name: 'kv-${systemShortName}'
    }