azure-devopsterraformazure-pipelines

Failed to load “tfplan” as a plan file


I am performing a very simple testing on TF + AzureDevops pipeline. the plan stage is success but the Deploy stage is failing with Failed to load “tfplan” as a plan file

I can see a new plan file is generated in my azure blog storage container and I am using the same name in the plan and deploy stage job.

if I use terraform apply in the same job, then the job is success.

how do I ensure both the TerraformPlan and TerraformApply jobs using the same plan file?

Please help:

trigger:

main
pool:
vmImage: ‘ubuntu-latest’

variables:

group: KV-SQL-SERVER-TEST
stages:

stage: Plan
displayName: ‘Terraform Plan’
jobs:

job: TerraformPlan
displayName: ‘Terraform Plan’
steps:
task: TerraformInstaller@0
inputs:
terraformVersion: ‘latest’
script: |
terraform --version
terraform init -backend-config=“access_key=$(storagetest101-key1)”
terraform plan -out=tfplan
displayName: ‘Terraform Init and Plan’


stage: Deploy
displayName: ‘Terraform Apply’
jobs:

job: TerraformApply
displayName: ‘Terraform Apply’
steps:
task: TerraformInstaller@0
inputs:
terraformVersion: ‘latest’

script: |
terraform --version
terraform init -backend-config=“access_key=$(storagetest101-key1)”
terraform validate
terraform apply -auto-approve -input=false tfplan
displayName: ‘Terraform Init and Apply’

Solution

  • It's the expected behavior. You are running terraform plan and terraform apply in the different jobs. When you use MS-hosted agents, each job will get a brand-new agent. That's to say that your terraform plan and terraform apply are running on the different agents, so that terraform apply can't find the target plan file.

    As a workaround, you can publish the plan file into a build artifact in job "TerraformPlan" and download the artifact in job "TerraformApply" to consume the plan file. I have modified your yaml file as shown below.

    trigger:
    - main
    pool:
      vmImage: ‘ubuntu-latest’
    
    variables:
      group: KV-SQL-SERVER-TEST
    stages:
    - stage: Plan
      displayName: ‘Terraform Plan’
      jobs:
      - job: TerraformPlan
        displayName: ‘Terraform Plan’
        steps:
        - task: TerraformInstaller@0
          inputs:
            terraformVersion: ‘latest’
        - script: |
            terraform --version
            terraform init -backend-config=“access_key=$(storagetest101-key1)”
            terraform plan -out=tfplan
          displayName: ‘Terraform Init and Plan’
        
        - task: CopyFiles@2
          inputs:
            SourceFolder: '$(System.DefaultWorkingDirectory)'
            Contents: '*tfplan*'
            TargetFolder: '$(Build.ArtifactStagingDirectory)'
        - task: PublishBuildArtifacts@1
          inputs:
            PathtoPublish: '$(Build.ArtifactStagingDirectory)'
            ArtifactName: 'planfile'
            publishLocation: 'Container'
    
    - stage: Deploy
      displayName: ‘Terraform Apply’
      jobs:
      - job: TerraformApply
        displayName: ‘Terraform Apply’
        steps:
        - task: DownloadBuildArtifacts@1
          inputs:
            buildType: 'current'
            downloadType: 'single'
            artifactName: 'planfile'
            downloadPath: '$(System.DefaultWorkingDirectory)'
        - task: TerraformInstaller@0
          inputs:
            terraformVersion: ‘latest’
    
        - script: |
            terraform --version
            terraform init -backend-config=“access_key=$(storagetest101-key1)”
            terraform validate
            terraform apply -auto-approve -input=false planfile/tfplan
          displayName: ‘Terraform Init and Apply’