0
votes

I am currently stuck at the init point in the Azure DevOps Pipeline. So I successfully install Terraform, but in the next step it already fails.

pool:
  name: Azure Pipelines
  vmImage: ubuntu-latest

variables:
  - name: terraform-working-directory
value: '$(System.DefaultWorkingDirectory)/terraform/'

stages :
  - stage: terraform
    jobs:
      - deployment: deploy_terraform
        continueOnError: false
        environment: 'dev'
        strategy:
          runOnce:
            deploy:
              steps:
                - task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-installer.TerraformInstaller@0
                  displayName: 'install'
                  inputs:
                    terraformVersion: '0.14.11'
                - task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
                  displayName: 'init'
                  inputs:
                    command: 'init'
                    workingDirectory: $(terraform-working-directory)
                    backendType: 'self-configured'

Terraform file:

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "=2.63.0"
    }
  }

  backend "azurerm" {
    resource_group_name = "xxx"
    storage_account_name = "xxx"
    container_name = "xxx"
    key = "terraform.tfstate"
  }
}

It is currently really basic and just in the beginning. The error message I receive is the following:

/opt/hostedtoolcache/terraform/0.14.11/x64/terraform init -backend-config=storage_account_name=xxx -backend-config=container_name=xxx -backend-config=key=xxx -backend-config=resource_group_name=xxx -backend-config=arm_subscription_id=xxx -backend-config=arm_tenant_id=*** -backend-config=arm_client_id=*** -backend-config=arm_client_secret=***

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

Finishing: terraform init

I've locally started the terraform init and apply, which worked fine

1

1 Answers

2
votes

Please make sure you have correct working directory. The message is misleading. The issue is in fact becuase terraform cannot find your tf files.

Please add this step to check if you have correct directory

- bash: ls $(terraform-working-directory)

You use deployment job and here repository is not dowloaded out of the box. Please add - checkout: self at the very beginning of the job.