microsoft / azure-pipelines-terraform

Azure Pipelines tasks for installing Terraform and running Terraform commands in a build or release pipeline.
MIT License
95 stars 59 forks source link

Terraform apply fails when deploying tfplan from artifactory in release pipeline #212

Closed judif closed 3 weeks ago

judif commented 4 months ago

Hi all,

Trying to release a Terraform plan from a PR build deployed to the artifactory in another pipeline resuluts in an error. Plan and release pipeline yml files as well as the error are attached below.

plan.yml template file

Executed on PR

...

    - task: TerraformTaskV4@4
      displayName: 'Initialise Terraform' # init needs to be installed at every stage
      inputs:
        provider: 'azurerm'
        command: init
        workingDirectory: '$(System.DefaultWorkingDirectory)/infrastructure' # where the Terraform code is at
        # fetched from variables (azure-pipeline.yml)
        backendServiceArm: $(ServiceConnectionName)
        backendAzureRmResourceGroupName: '$(bk-rg-name)'  
        backendAzureRmStorageAccountName: '$(bk-str-account-name)'  
        backendAzureRmContainerName: '$(bk-container-name)-${{parameters.env}}'  
        backendAzureRmKey: '${{parameters.env}}$(bk-key)'

    - task: TerraformTaskV4@4
      displayName: 'Validate Terraform'
      inputs:
        provider: 'azurerm'
        command: 'validate'   

    - task: TerraformTaskV4@4
      displayName: 'Plan Terraform'
      inputs:
        provider: 'azurerm'
        command: 'plan'
        workingDirectory: '$(System.DefaultWorkingDirectory)/infrastructure'
        environmentServiceNameAzureRM: $(ServiceConnectionName)
        commandOptions: '-lock=false -var-file="vars/${{parameters.env}}.tfvars" -out=$(System.DefaultWorkingDirectory)/infrastructure/terraform.tfplan'
        # var file = selecting the tfvars for each environment
        # out = creating the plan file to the infrastructure folder and call it terraform.tfplan

    - task: CopyFiles@2
      displayName: 'Moving Terraform Code to artifact staging'
      inputs:
        Contents: 'infrastructure/**'
        TargetFolder: '$(Build.ArtifactStagingDirectory)'
        # Plan and apply are on different machines
        # plan state should thus be saved on a file
        # it will be then be loaded by apply

    - task: PublishBuildArtifacts@1
      displayName: 'Making artifact available to apply stage'
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: 'output-${{parameters.env}}'
        publishLocation: 'Container'

release.yml template file

Executed after merge

......

  strategy:
    runOnce: ## RunOnce vs Canary Deployment
      deploy:
        steps:
          - checkout: self 
          - task: DownloadPipelineArtifact@2
            inputs:
              buildType: 'specific'
              project: '98a312d7-ff8d-4036-996d-2c1de81e5be2'
              definition: ${{ parameters.build_definition_id }} # the pipeline id == infrastructure-setup/_build?definitionId=2
              artifactName: 'output-${{ parameters.env }}'
              buildVersionToDownload: 'latest'
              targetPath: '$(Pipeline.Workspace)'    

          - task: TerraformInstaller@1 # 1st/ install terraform
            displayName: 'Install Terraform' # install always need to be installed at every stage
            inputs:
              terraformVersion: 'latest'

          - task: TerraformTaskV4@4
            displayName: 'Apply Terraform' # init needs to be installed at every stage
            inputs:
              provider: 'azurerm'
              command: 'apply'
              workingDirectory: '$(Pipeline.Workspace)/output-${{ parameters.env }}/infrastructure'
              environmentServiceNameAzureRM: $(ServiceConnectionName)
              commandOptions: '-lock=true -lock-timeout=5m $(Pipeline.Workspace)/output-${{ parameters.env }}/infrastructure/terraform.tfplan'

Error Message

##[section]Starting: Apply Terraform
==============================================================================
Task         : Terraform
Description  : Execute terraform commands to manage resources on AzureRM, Amazon Web Services(AWS) and Google Cloud Platform(GCP)
Version      : 4.227.24
Author       : Microsoft Corporation
Help         : [Learn more about this task](https://aka.ms/AAf0uqr)
==============================================================================
[command]C:\hostedtoolcache\windows\terraform\1.7.4\x64\terraform.exe providers
[command]C:\hostedtoolcache\windows\terraform\1.7.4\x64\terraform.exe apply -auto-approve -lock=true -lock-timeout=5m D:\a\1/output-prd/infrastructure/terraform.tfplan
##[error]Error: There was an error when attempting to execute the process 'C:\hostedtoolcache\windows\terraform\1.7.4\x64\terraform.exe'. This may indicate the process failed to start. Error: spawn C:\hostedtoolcache\windows\terraform\1.7.4\x64\terraform.exe ENOENT
##[section]Finishing: Apply Terraform
mericstam commented 4 months ago

hi, These errors are often due to syntax errors with the command options. I would suggest to remove them and adding them back one by one to see where it fails.

Br Manuel

mericstam commented 3 weeks ago

closing due to inactivity