Abhijit
Abhijit

Reputation: 17

Unable to deploy Azure Resource Group from Devops using ARM template and Powershell

I am trying to deploy azure resources [Resource Group] from Devops using ARM Template and Powershell. Below is the setup

  1. Created a service principal with secret.
  2. Assign Contributor role in subscription level for the service principal.
  3. Create a service connection in Devops with that service principal.
  4. Assign the service connection in Yaml pipeline for subscription. YAML code
  5. Powershell scripts to deploy the Resource group with ARM template. Powershell scripts But no success. Azure context is set properly.

Error

error

Upvotes: 0

Views: 222

Answers (1)

wade zhou - MSFT
wade zhou - MSFT

Reputation: 8470

The New-AzResourceGroupDeployment is used to add a deployment to the pre-existing resource group, if the resource group doesn't exist, it will report the error in your screenshot.

To create a resource group, you can simply use New-AzResourceGroup which doesn't require ARM template. Then you can start to deploy on this resource group.

The ARM template on my side is used to deploy storage account on the resource group, fixed the pipeline and powershell as below for example:

stages:
  - stage: Partner
    displayName: Dev
    jobs:
      - deployment: Dev_deployment
        displayName: Dev deployment
        pool:
          vmImage: Windows-latest
        environment: Dev
        strategy:
         runOnce:
           deploy:
             steps:
               - checkout: self
               - task: AzurePowerShell@5
                 displayName: Lab Dev Deployment
                 inputs:
                   azureSubscription: 'ARMConn4'
                   ScriptType: 'InlineScript'
                   Inline: |
                     Set-Location $(System.DefaultWorkingDirectory)
                     ./deployment/master.ps1 -environment "dev" -rgconfig @{"dev"=@{"rgname"="rg_DevOpsCICDNew"}}    # rgconfig is required as hashtable type in your powershell
                   azurePowerShellVersion: 'LatestVersion'

The powershell:

    param
    (
        [Parameter(Mandatory=$true)][string] $environment,
        #[Parameter(Mandatory=$true)][hashtable] $commonconfig,
        [Parameter(Mandatory=$true)][hashtable] $rgconfig
    )
    
    $ARMTemplateFilePath = Resolve-Path -Path .\ARMtemplate\resourcegroup\resourcegroup.json
    $ARMTemplateParamFilePath = Resolve-Path -Path .\ARMtemplate\resourcegroup\parameters.json

    Write-Host $ARMTemplateFilePath
    Write-Host $ARMTemplateParamFilePath
    Write-Host $rgconfig[$environment].rgname

    # $Deployment = @{
    #     ResourceGroupName = $rgconfig[$environment].rgname
    #     TemplateFile = $ARMTemplateFilePath
    #     TemplateParameterFile = $ARMTemplateParamFilePath
    # }
    New-AzResourceGroup -Name $rgconfig[$environment].rgname -Location "East US"
    sleep 60   # add delay after resource group creation

    #New-AzResourceGroupDeployment @Deployment
    New-AzResourceGroupDeployment -ResourceGroupName $rgconfig[$environment].rgname -TemplateFile $ARMTemplateFilePath -TemplateParameterFile $ARMTemplateParamFilePath

    Write-Host "Deployment is complted for ResourceGroup :" $rgconfig[$environment].rgname

The pipeline succeeds, and resource group created, deployment(storageaccount) completed on the resource group.

enter image description here

enter image description here

Upvotes: 0

Related Questions