skip to Main Content

I am currently running Azure Devops Pipeline for to deploy the resources in Azure via Terraform .
In this case am failing with errors , below is my Azure Devops Yaml pipeline

trigger:
- main

pool:
  vmImage: ubuntu-latest

jobs:

  - job: install_terraform
    displayName: "Installing Terraform"
    steps:
      - task: TerraformInstaller@0
        displayName: 'Install Terraform latest'

  - job: terraform_init
    displayName: "Terraform Init"
    dependsOn: install_terraform
    steps:
      - task: TerraformTaskV2@2
        displayName: 'Terraform : init'
        inputs:
          
          backendServiceArm: 'sc-test'
          backendAzureRmResourceGroupName: dowd-rg
          backendAzureRmStorageAccountName: dowd
          backendAzureRmContainerName: tfstatedowd
          backendAzureRmKey: terraform.tfstate
          

  - job: manual_approval
    displayName: "Manual Approval"
    dependsOn: terraform_init
    pool: server
    steps:
      - task: ManualValidation@0
        timeoutInMinutes: 5
        inputs:
          instructions: "Hi, please validate"

  - job: terrform_apply
    displayName: "Terraform Apply"
    dependsOn: manual_approval   
    steps:    
      - task: TerraformTaskV2@2
        displayName: 'Terraform : Apply'
        inputs:
          command: apply
          environmentServiceNameAzureRM: 'sc-test'

below is the screenshot of error and the error message

/usr/local/bin/terraform providers

│ Error: Backend initialization required, please run "terraform init"
│
│ Reason: Initial configuration of the requested backend "azurerm"
│
│ The "backend" is the interface that Terraform uses to store state,
│ perform operations, etc. If this message is showing up, it means that the
│ Terraform configuration you're using is using a custom configuration for
│ the Terraform backend.
│
│ Changes to backend configurations require reinitialization. This allows
│ Terraform to set up the new configuration, copy existing state, etc. Please
│ run │ "terraform init" with either the "-reconfigure" or "-migrate-state" flags
│ to
│ use the current configuration.
│
│ If the change reason above is incorrect, please verify your configuration
│ hasn't changed and try again. At this point, no changes to your existing
│ configuration or state have been made.

/usr/local/bin/terraform validate
╷
│ Error: Missing required provider
│ 
│ This configuration requires provider
│ registry.terraform.io/hashicorp/azurerm, but that provider isn't available.
│ You may be able to install it automatically by running:
│   terraform init
╵
##[error]Error: The process '/usr/local/bin/terraform' failed with exit code 1
Finishing: Terraform : Apply

I would like to run the pipeline i dont know what am missing here

2

Answers


  1. Error: Backend initialization required, please run "terraform init"

    You should put the terraform install, init, plan,apply steps/tasks in one job, as different jobs run on different agent machine, it cannot find terraform init in another agent.

    As doc stated:
    enter image description here

    My DevOps yaml for your reference:

    name: "Terraform Apply"
    
    trigger: none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
      - task: TerraformInstaller@0
        displayName: 'install'
        inputs:
          terraformVersion: '0.15.0'
      
      - task: TerraformTaskV4@4
        inputs:
          provider: 'azurerm'
          command: 'init'
          backendServiceArm: 'testconn1'
          backendAzureRmResourceGroupName: 'testrg'
          backendAzureRmStorageAccountName: 'testsa'
          backendAzureRmContainerName: 'testcontainer'
          backendAzureRmKey: 'testkey'
    
      - task: TerraformTaskV4@4
        inputs:
          provider: 'azurerm'
          command: 'apply'
          commandOptions: '$(Pipeline.Workspace)/terraform.tfplan'
          environmentServiceNameAzureRM: 'testconn1'
    

    In addition, if you need manual_approval job in your pipeline, you can put it as first job, and all others(terraform steps) in 2nd job which depends on the first one.

    Login or Signup to reply.
  2. First and foremost ensure that you use single quotes around resource group names, storage account names, container names, and state file names in the terraform_init job. Basically this ensures that special characters don’t cause issues in the YAML.

    - job: terraform_init
    displayName: "Terraform Init"
    dependsOn: install_terraform
    steps:
      - task: TerraformTaskV2@2
        displayName: 'Terraform : init'
        inputs:
          command: 'init'  # Here I mean 
          backendServiceArm: 'sc-test'
          backendAzureRmResourceGroupName: 'dowd-rg' 
          backendAzureRmStorageAccountName: 'dowd'  
          backendAzureRmContainerName: 'tfstatedowd'  
          backendAzureRmKey: 'terraform.tfstate'  
    

    Additionally, you can specify the command: ‘init’ to explicitly run the terraform init command and also make sure you provide the correct backend configuration. Also ensure that the version of Terraform you’re using is compatible with your configuration(The latest version of Terraform is recommended).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search