Error: spawn terraform ENOENT when running Terraform in Azure DevOps Pipeline

A quick blog post to detail the simple fix of when you see the error: spawn terraform ENOENT when running your Terraform within an Azure DevOps Pipeline. Depending on which stages you have setup to run, you will notice this at the terraform init stage.

Error output from Azure DevOps Pipeline

Within the Azure DevOps pipeline, the full output of error from the stage terraform init :

[error]Error: There was an error when attempting to execute the process '/opt/hostedtoolcache/terraform/1.0.10/x64/terraform'. This may indicate the process failed to start. Error: spawn /opt/hostedtoolcache/terraform/1.0.10/x64/terraform ENOENT
Finishing: init

A sample of my terraform init stage

          - task: TerraformTaskV2@2
            displayName: 'init'
            inputs:
              provider: 'azurerm'
              command: 'init'
              backendServiceArm: '${{ variables.backendServiceArm }}'
              backendAzureRmResourceGroupName: '${{ variables.backendAzureRmResourceGroupName }}'
              backendAzureRmStorageAccountName: '${{ variables.backendAzureRmStorageAccountName }}'
              backendAzureRmContainerName: '${{ variables.backendAzureRmContainerName }}'
              backendAzureRmKey: '${{ variables.backendAzureRmKey }}' 
              workingDirectory: '$(System.DefaultWorkingDirectory)/terraform-acr/'

The fix

The fix is quite simple – thought i’d include it in a quick blog post as I am sure others may find this error 🙂

Reviewing my terraform init stage above against my folder setup – can you notice the issue?

Notice the issue? The working directory of terraform was incorrect.

Incorrect workingDirectory as above

workingDirectory: '$(System.DefaultWorkingDirectory)/terraform-acr/'

Correct workingDirectory location

workingDirectory: '$(System.DefaultWorkingDirectory)/terraform-aci/'

Thanks for reading, hopefully this blog post has helped if you have found this error within your Azure DevOps pipeline when running Terraform!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s