Akshay
Akshay

Reputation: 11

Terraform on Azure DevOps

I am getting below error while running pipeline from Azure DevOps (Using Terraform). I have defined a service connection which is used as Variable on the pipeline.

Error building ARM Config: obtain subscription() from Azure CLI: parsing json result from the Azure CLI: waiting for the Azure CLI: exit status 1: ERROR: Please run 'az login' to setup account.

enter image description here

Below is my YAML file

    parameters:
  environment: ''
  environmentPath: ''
  terraformStateFilename: ''
  artifacts: ''

steps:
  - task: TerraformInstaller@0
    inputs:
      terraformVersion: $(terraformVersion)
  - task: TerraformCLI@0
    displayName: Terraform Init
    inputs:
      provider: 'azurerm'
      command: 'init'
      workingDirectory: $(System.DefaultWorkingDirectory)/${{ parameters.environmentPath }}
      backendServiceArm: $(subscription)
      backendAzureRmResourceGroupName: $(terraformGroup)
      backendAzureRmStorageAccountName: $(terraformStorageName)
      backendAzureRmContainerName: $(terraformContainerName)
      backendAzureRmKey: ${{ parameters.terraformStateFilename }}
  - task: TerraformCLI@0
    displayName: Terraform Plan
    inputs:
      provider: 'azurerm'
      command: 'plan'
      workingDirectory: $(System.DefaultWorkingDirectory)/${{ parameters.environmentPath }}
      environmentServiceNameAzureRM: $(subscription)
      commandOptions: '-out plan.tfplan'
  - task: CopyFiles@2
    inputs:
      SourceFolder: '${{ parameters.environmentPath }}'
      Contents: |
        terraform.lock.hcl
        versions.tf
        providers.tf
        plan.tfplan
        terraform.tfvars
      TargetFolder: '$(Build.ArtifactStagingDirectory)'
    displayName: 'Copy Artifacts'
  - publish: '$(Build.ArtifactStagingDirectory)'
    artifact: ${{ parameters.artifacts }}
e

Upvotes: 0

Views: 1149

Answers (1)

javierlga
javierlga

Reputation: 1652

You need to login to Azure using this step:

steps:
  - task: AzureCLI@1
    displayName: Set Azure vars
    inputs:
      azureSubscription: ${{ parameters.azureSubscription }}
      scriptLocation: inlineScript
      inlineScript: |
        Write-Host "##vso[task.setvariable variable=AZURE_CLIENT_ID]$env:servicePrincipalId"
        Write-Host "##vso[task.setvariable variable=AZURE_CLIENT_SECRET]$env:servicePrincipalKey"
        Write-Host "##vso[task.setvariable variable=AZURE_TENANT_ID]$env:tenantId"
      addSpnToEnvironment: true

Then in the steps where Terraform is required, you add an env to reference the previous variables:

 - task: TerraformCLI@0
    displayName: Terraform Plan
    env: 
      ARM_CLIENT_ID: $(AZURE_CLIENT_ID)
      ARM_CLIENT_SECRET: $(AZURE_CLIENT_SECRET)
      ARM_TENANT_ID: $(AZURE_TENANT_ID)
    inputs:
      provider: 'azurerm'
      command: 'plan'
      workingDirectory: $(System.DefaultWorkingDirectory)/${{ parameters.environmentPath }}
      environmentServiceNameAzureRM: $(subscription)
      commandOptions: '-out plan.tfplan'

Upvotes: 0

Related Questions