Search code examples
azureazure-resource-managerazure-automationazure-bicepazure-runbook

Deployment of runbook module failed, because it reached terminal provisioning state 'Failed'


I want to create some automation runbooks with PowerShell on Azure using bicep. For that I need some custom modules listed below.

var modules = [
    'Microsoft.Graph.Authentication'
    'Microsoft.Graph.Groups'
    'Microsoft.Graph.Mail'
    'Microsoft.Graph.Planner'
    'Microsoft.Graph.Teams'
    'Microsoft.Graph.Users'
    'Microsoft.Graph.Users.Actions'
]

Here is how I create the modules:

resource automationResource 'Microsoft.Automation/automationAccounts@2022-08-08' = {
    // Some other properties

    resource moduleResources 'modules' = [for module in modules: {
        name: module
        properties: {
            contentLink: {
                uri: 'https://www.powershellgallery.com/api/v2/'
            }
        }
    }]
}

When I deploy this to Azure I got next error for all modules:

{
    "status": "Failed",
    "error": {
        "code": "ResourceDeploymentFailure",
        "target": "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Automation/automationAccounts/xxx/modules/Microsoft.Graph.Teams",
        "message": "The resource write operation failed to complete successfully, because it reached terminal provisioning state 'Failed'."
    }
}

Why won't the modules not be added to the automation account?


Solution

  • Few things here:

    1. You need the full url of the package: name + version
    2. If a package has some dependencies, dependencies need to be imported first.

    Here, all the packages depends on the Microsoft.Graph.Authentication package.

    The easiest way to achieve that is to deploy the modules sequentially but it will be quite slow:

    var modules = [
      {
        name: 'Microsoft.Graph.Authentication'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Groups'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Mail'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Planner'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Teams'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Users'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Users.Actions'
        version: '2.0.0-preview8'
      }
    ]
    
    resource automationAccount 'Microsoft.Automation/automationAccounts@2022-08-08' existing = {
      name: automationAccountName
    }
    
    @batchSize(1)
    resource automationAccountModules 'Microsoft.Automation/automationAccounts/modules@2022-08-08' = [for module in modules: {
      parent: automationAccount
      name: module.name
      properties: {
        contentLink: {
          uri: toLower('https://devopsgallerystorage.blob.core.windows.net:443/packages/${module.name}.${module.version}.nupkg')
        }
      }
    }]
    

    or you could import the Microsoft.Graph.Authentication beofre importing other modules:

    var modules = [
      {
        name: 'Microsoft.Graph.Authentication'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Groups'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Mail'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Planner'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Teams'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Users'
        version: '2.0.0-preview8'
      }
      {
        name: 'Microsoft.Graph.Users.Actions'
        version: '2.0.0-preview8'
      }
    ]
    
    resource automationAccount 'Microsoft.Automation/automationAccounts@2022-08-08' existing = {
      name: automationAccountName
    }
    
    var authPackage = first(modules)
    resource authModule 'Microsoft.Automation/automationAccounts/modules@2022-08-08' = {
      parent: automationAccount
      name: authPackage.name
      properties: {
        contentLink: {
          uri: toLower('https://devopsgallerystorage.blob.core.windows.net:443/packages/${authPackage.name}.${authPackage.version}.nupkg')
        }
      }
    }
    
    var otherPackages = skip(modules, 1)
    resource automationAccountModules 'Microsoft.Automation/automationAccounts/modules@2022-08-08' = [for module in otherPackages: {
      dependsOn: [ authModule ]
      parent: automationAccount
      name: module.name
      properties: {
        contentLink: {
          uri: toLower('https://devopsgallerystorage.blob.core.windows.net:443/packages/${module.name}.${module.version}.nupkg')
        }
      }
    }]