Search code examples
azure-devopsterraformazure-pipelines

##[warning]Can't find loc string for key: TerraformPlanFailed and ##[error]Error: TerraformPlanFailed 1-- Azure devops terraform pipeline


I am new azure devops terraform pipeline user. When I do a deploy test on plan step according some document,encouter following errors:


 Error: No configuration files
 
 The directory /home/guo/myagent/_work/1/s contains no Terraform
 configuration files.


/home/guo/myagent/_work/_tool/terraform/1.9.3/x64/terraform plan -detailed-exitcode

 Error: No configuration files
 
 Plan requires configuration to be present. Planning without a configuration
 would mark everything for destruction, which is normally not what is
 desired. If you would like to destroy everything, run plan with the
 -destroy option. Otherwise, create a Terraform configuration file (.tf
 file) and try again.

 ##[warning]Can't find loc string for key: TerraformPlanFailed
 ##[error]Error: TerraformPlanFailed 1
 Finishing: plan

The pipeline file:

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger: none

pool: default

variables:
  bkstrgrg: 'daily'
  bkstrg: 'saguo'
  bkcontainer: 'tfstate'
  bkstrgkey: 'main.tfstatefile.prefix'    #  define yourself. prefix terraform status file.

stages:
  - stage: tfvalidate
    jobs:
      - job: validate
        continueOnError: false
        steps:
        - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
          displayName: tfinstall
          inputs:
            terraformVersion: 'latest'
        - task: TerraformTaskV4@4
          displayName: init
          inputs:
            provider: 'azurerm'
            command: 'init'
            backendServiceArm: 'tf-demo'
            backendAzureRmResourceGroupName: '$(bkstrgrg)'
            backendAzureRmStorageAccountName: '$(bkstrg)'
            backendAzureRmContainerName: '$(bkcontainer)'
            backendAzureRmKey: '$(bkstrgkey)'
        - task: TerraformTaskV4@4
          displayName: validate
          inputs:
            provider: 'azurerm'
            command: 'validate'
  - stage: tfdeploy
    condition: succeeded('tfvalidate')
    dependsOn: tfvalidate
    jobs:
      - job: apply
        steps:
        - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
          displayName: tfinstall
          inputs:
            terraformVersion: 'latest'
        - task: TerraformTaskV4@4
          displayName: init
          inputs:
            provider: 'azurerm'
            command: 'init'
            backendServiceArm: 'tf-demo'
            backendAzureRmResourceGroupName: '$(bkstrgrg)'
            backendAzureRmStorageAccountName: '$(bkstrg)'
            backendAzureRmContainerName: '$(bkcontainer)'
            backendAzureRmKey: '$(bkstrgkey)'
        - task: TerraformTaskV4@4
          displayName: plan
          inputs:
            provider: 'azurerm'
            command: 'plan'
            environmentServiceNameAzureRM: 'tf-demo'
        - task: TerraformTaskV4@4
          inputs:
            provider: 'azurerm'
            command: 'apply'
            environmentServiceNameAzureRM: 'tf-demo'

The terraform validate and init step has been passed successfully.

It seems some terraform configure file missed on agent server working dir. I have create containers: 'tfstate' on storage account 'saguo' of 'daily' resource group.

Could somebody help to point the yaml file issue?

Thanks.

enter image description here enter image description here

enter image description here


Solution

  • Your pipeline was defined to reference the .tf template file under your repo root directory $(System.DefaultWorkingDirectory) for terraform deployment.

    The issue could be reproduced when your terraform template file was not there. Interestingly, when double checking the logs in your terraform init step, you could probably notice the message that Terraform initialized in an empty directory!.

    enter image description here

    Please add the workingDirectory property for all your terraform tasks, whose value should be the relative path to your template file under $(System.DefaultWorkingDirectory).

    Taking the file structure of my repo for example, the template RGEmptyBackend/RGEmptyBackend.tf was referenced for terraform deployment, therefore I configured those terraform tasks to be run in the workingDirectory of $(System.DefaultWorkingDirectory)/RGEmptyBackend

    ![enter image description here

    steps:
    - task: TerraformInstaller@1
      inputs:
        terraformVersion: 'latest'
    - task: TerraformTaskV4@4
      displayName: terraform init
      inputs:
        provider: 'azurerm'
        command: 'init'
        workingDirectory: '$(System.DefaultWorkingDirectory)/RGEmptyBackend'
        # commandOptions: '-backend-config=use_msi=false'
        backendServiceArm: '${{ parameters.azurermsvcconnection }}'
        backendAzureRmResourceGroupName: '$(ARM_RESOURCE_GROUP_NAME)'
        backendAzureRmStorageAccountName: '$(ARM_STORAGE_ACCOUNT_NAME)'
        backendAzureRmContainerName: '$(ARM_CONTAINER_NAME)'
        backendAzureRmKey: 'rg/rgemptybackend.tfstate'
    - task: TerraformTaskV4@4
      displayName: terraform validate
      inputs:
        provider: 'azurerm'
        command: 'validate'
        workingDirectory: '$(System.DefaultWorkingDirectory)/RGEmptyBackend'
    - task: TerraformTaskV4@4
      displayName: terrafor plan
      inputs:
        provider: 'azurerm'
        command: 'plan'
        workingDirectory: '$(System.DefaultWorkingDirectory)/RGEmptyBackend'
        commandOptions: '-input=false -var "RGNAME=${{ variables.TF_VAR_RGNAME }}-$(Build.BuildId)"'
        environmentServiceNameAzureRM: '${{ parameters.azurermsvcconnection }}'
    - task: TerraformTaskV4@4
      displayName: terraform apply
      inputs:
        provider: 'azurerm'
        command: 'apply'
        workingDirectory: '$(System.DefaultWorkingDirectory)/RGEmptyBackend'
        commandOptions: '-input=false -var "RGNAME=${{ parameters.TF_VAR_RGNAME }}-$(Build.BuildId)"'
        environmentServiceNameAzureRM: '${{ parameters.azurermsvcconnection }}'