You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps Pipeline执行Terraform Apply失败求助

问题场景

通过Azure DevOps Pipeline使用Terraform部署Azure资源,执行Apply阶段时出现报错,提示需要执行terraform init且缺少azurerm provider。

流水线YAML配置

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'

报错信息

/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
问题原因
  • Azure DevOps的每个Job运行在独立的Agent虚拟机上,Job之间默认不共享任何文件或运行状态
  • terraform_init Job完成后,初始化生成的.terraform目录(包含下载的provider)、backend配置仅存在于该Job的Agent本地,不会自动传递到后续的terrform_apply Job
  • terrform_apply Job在全新的Agent上启动,未执行terraform init,导致Terraform无法识别backend配置,也没有加载必要的azurerm provider
修复方案

方案1:在Apply Job中重新执行Terraform Init

直接在Apply Job中重复Init步骤,确保在当前Agent上完成backend配置和provider下载:

- job: terrform_apply
    displayName: "Terraform Apply"
    dependsOn: manual_approval   
    steps:    
      - task: TerraformInstaller@0
        displayName: 'Install Terraform latest'
      - task: TerraformTaskV2@2
        displayName: 'Terraform : init'
        inputs:
          backendServiceArm: 'sc-test'
          backendAzureRmResourceGroupName: dowd-rg
          backendAzureRmStorageAccountName: dowd
          backendAzureRmContainerName: tfstatedowd
          backendAzureRmKey: terraform.tfstate
      - task: TerraformTaskV2@2
        displayName: 'Terraform : Apply'
        inputs:
          command: apply
          environmentServiceNameAzureRM: 'sc-test'

方案2:用Stage拆分流程,共享工作目录

通过发布Artifact传递Init后的工作目录,避免重复初始化,适合复杂流水线:

trigger:
- main

pool:
  vmImage: ubuntu-latest

stages:
  - stage: prepare_terraform
    displayName: "Prepare Terraform"
    jobs:
      - job: install_and_init
        displayName: "Install and Init Terraform"
        steps:
          - task: TerraformInstaller@0
            displayName: 'Install Terraform latest'
          - task: TerraformTaskV2@2
            displayName: 'Terraform : init'
            inputs:
              backendServiceArm: 'sc-test'
              backendAzureRmResourceGroupName: dowd-rg
              backendAzureRmStorageAccountName: dowd
              backendAzureRmContainerName: tfstatedowd
              backendAzureRmKey: terraform.tfstate
          # 发布初始化后的工作目录为Artifact
          - publish: $(System.DefaultWorkingDirectory)
            artifact: terraform-workspace

  - stage: manual_approval
    displayName: "Manual Approval"
    dependsOn: prepare_terraform
    jobs:
      - job: approval
        displayName: "Wait for Approval"
        pool: server
        steps:
          - task: ManualValidation@0
            timeoutInMinutes: 5
            inputs:
              instructions: "Hi, please validate"

  - stage: apply_terraform
    displayName: "Terraform Apply"
    dependsOn: manual_approval
    jobs:
      - job: apply
        displayName: "Apply Terraform Configuration"
        steps:
          - task: TerraformInstaller@0
            displayName: 'Install Terraform latest'
          # 下载之前发布的工作目录Artifact
          - download: current
            artifact: terraform-workspace
          - task: TerraformTaskV2@2
            displayName: 'Terraform : Apply'
            inputs:
              command: apply
              environmentServiceNameAzureRM: 'sc-test'
              workingDirectory: $(Pipeline.Workspace)/terraform-workspace

内容的提问来源于stack exchange,提问作者balaganesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 22:14:59