Failed to load "tfplan" as a plan file

Hello ,
I am performing a very simple testing on TF + AzureDevops pipeline . the plan stage is success but the Deploy stage is failing with could not load 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 . 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-key2)”
        terraform validate
        terraform apply -auto-approve -input=false “tfplan”
        displayName: ‘Terraform Init and Apply’

The Terraform script is success when i combine both stage into one stage .

can anyone explain why the tfplan file from the azure storage container could not read by the different stage and job ? and what is the best practice to share the plan file across the stage and job ?

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

stages:
- stage: Plan
  displayName: 'Terraform Plan'
  jobs:
  - job: TerraformPlan
    displayName: 'Terraform Plan'
    steps:
    - task: TerraformInstaller@0
      inputs:
        terraformVersion: 'latest'

    - script: |
        terraform --version
        terraform init
        terraform plan -out=tfplan
         terraform apply -auto-approve tfplan
      displayName: 'Terraform Init and Plan'

Hi @keenweng2001,

I’m not familiar with AzureDevops pipeline, but adding the verbatim error message and details might provide some clues.

You should also correctly format the configuration that you’ve posted here to help others who may be familiar with the system more easily spot any possible problems. I don’t know how the arguments are passed from the configuration to the command, but the unnecessary quotes around the tfplan filename look suspicious, and you should not need -auto-approve or -input=false if you are applying a plan file.

i have remove the auto -approve and input=false and also the double quotes , but i am still facing the same error :

> 2024-09-04T01:47:02.7712343Z ##[section]Starting: Terraform Init and Apply
> 2024-09-04T01:47:02.7716805Z 
> 2024-09-04T01:47:02.7716924Z Task         : Command line
> 2024-09-04T01:47:02.7716987Z Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
> 2024-09-04T01:47:02.7717120Z Version      : 2.244.3
> 2024-09-04T01:47:02.7717179Z Author       : Microsoft Corporation
> 2024-09-04T01:47:02.7717248Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
> 2024-09-04T01:47:02.7717353Z ==============================================================================
> 2024-09-04T01:47:03.0355468Z Generating script.
> 2024-09-04T01:47:03.0355739Z ========================== Starting Command Output 
> 2024-09-04T01:47:03.0366788Z [command]/usr/bin/bash --noprofile --norc /home/vsts/work/_temp/70fec530-3242-4928-b711-19676b289f0a.sh
> 2024-09-04T01:47:03.0813565Z Terraform v1.9.5
> 2024-09-04T01:47:03.0813849Z on linux_amd64
> 2024-09-04T01:47:03.1104019Z e[31m╷e[0me[0m
> 2024-09-04T01:47:03.1104979Z e[31m│e[0m e[0me[1me[31mError: e[0me[0me[1mFailed to load "tfplan1" as a plan filee[0m
> 2024-09-04T01:47:03.1105683Z e[31m│e[0m e[0m
> 2024-09-04T01:47:03.1106119Z e[31m│e[0m e[0me[0mError: stat tfplan1: no such file or directory
> 2024-09-04T01:47:03.1106711Z e[31m╵e[0me[0m
> 2024-09-04T01:47:03.1132469Z 
> 2024-09-04T01:47:03.1186415Z ##[error]Bash exited with code '1'.
> 2024-09-04T01:47:03.1232675Z ##[section]Finishing: Terraform Init and Apply
- stage: Deploy
  displayName: 'Terraform Apply'
  jobs:
  - job: TerraformApply
    displayName: 'Terraform Apply'
    steps:
    - task: TerraformInstaller@0
      inputs:
        terraformVersion: 'latest'

    - script: |
        terraform --version
        terraform apply tfplan1
      displayName: 'Terraform Init and Apply'