Working with Terraform can present challenges, particularly when faced with non-detailed error messages. During our experience deploying a Terraform pipeline, we encountered a couple of issues that may resonate with others. Initially, we encountered an error with the code "##[error]Error: There was an error when attempting to execute the process '/usr/local/bin/terraform'. This may indicate the process failed to start. Error: spawn /usr/local/bin/terraform ENOENT." Additionally, challenges arose during the deployment of Terraform configuration files, particularly when incorporating directories for better organization of the deployment process.

Problem

When we would try to deploy the following (we used the Starter pipeline configuration in DevOps that included an initialize and the ability to create the plan and deploy tasks):

The pipeline attempted to execute the following YAML code:

Code sample by Cloudaen
trigger: none

variables:
  serviceConnection: 'ARMTest'
pool:
  vmImage: ubuntu-latest


- task: TerraformTaskV4@4
  inputs:
    provider: 'azurerm'
    command: 'init'
    backendServiceArm: 'ARMTest'
    backendAzureRmResourceGroupName: 'project-terraform'
    backendAzureRmStorageAccountName: 'terraformstorage'
    backendAzureRmContainerName: 'tfstate'
    backendAzureRmKey: 'tf/terraform.tfstate'
    workingDirectory: '$(System.DefaultWorkingDirectory)'
  displayName: 'Terraform Init'
- task: TerraformTaskV4@4
  displayName: Terra Plan
  inputs:
    provider: 'azurerm'
    command: 'plan'
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
    environmentServiceNameAzureRM: $(serviceConnection)
- task: TerraformTaskV4@4
  displayName: Terra Apply
  inputs:
    provider: 'azurerm'
    command: 'apply'
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
    environmentServiceNameAzureRM: $(serviceConnection)

We would receive the following error:

Code sample by Cloudaen
##[error]Error: There was an error when attempting to execute the process '/usr/local/bin/terraform'. This may indicate the process failed to start. Error: spawn /usr/local/bin/terraform ENOENT.

Solution

As mentioned earlier, we wanted to organize our Terraform (.tf) files within a directory named terraform. However, the Starter pipeline initially faced challenges in comprehending this directory structure. The resolution involved ensuring that the workingDirectory parameter in the Init task explicitly included "workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'".

Once this was added the YAML config looked like:

Code sample by Cloudaen
trigger: none

variables:
  serviceConnection: 'ARMTest'
pool:
  vmImage: ubuntu-latest


- task: TerraformTaskV4@4
  inputs:
    provider: 'azurerm'
    command: 'init'
    backendServiceArm: 'ARMTest'
    backendAzureRmResourceGroupName: 'project-terraform'
    backendAzureRmStorageAccountName: 'terraformstorage'
    backendAzureRmContainerName: 'tfstate'
    backendAzureRmKey: 'tf/terraform.tfstate'
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
  displayName: 'Terraform Init'
- task: TerraformTaskV4@4
  displayName: Terra Plan
  inputs:
    provider: 'azurerm'
    command: 'plan'
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
    environmentServiceNameAzureRM: $(serviceConnection)
- task: TerraformTaskV4@4
  displayName: Terra Apply
  inputs:
    provider: 'azurerm'
    command: 'apply'
    workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
    environmentServiceNameAzureRM: $(serviceConnection)

Summary

Azure DevOps and Terraform stand out as invaluable tools for achieving consistent infrastructure deployment. Paying attention to details, such as leveraging Starter pipelines, proves crucial, especially when encountering errors like ENOENT. This guide aims to provide assistance in addressing such challenges, offering insights into potential issues that may be hard to understand at first glance.