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 init failure on Azure Devops Pipeline #209

Open WXRK opened 5 months ago

WXRK commented 5 months ago

Dear gurus,

i have been working on fixing this issue with no luck and i hope i could have some advise here, thank you in advance. I have a simple yaml file below:

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: vmImage: ubuntu-latest

steps:

=============================================================

whenever i run the pipeline, the error fails during terraform init:

image

/opt/hostedtoolcache/terraform/1.7.1/x64/terraform init -backend-config=storage_account_name=aztfdostor -backend-config=container_name=aztfdoblob -backend-config=key=aztfdoblob.state -backend-config=resource_group_name=AZTFDO-Storage -backend-config=subscription_id=bf8d4281-83dd-42b3-b885-13278df4489f -backend-config=tenant_id=ebd0b70e-75c8-4904-b36c-5979c95af1e2 -backend-config=client_id= -backend-config=client_secret=

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

Please can someone advise me where could i be wrong?

dcarvalh04 commented 5 months ago

Same error here with terraform 1.7.2

EDIT: well, got it working by retyping the task with the built-in configurator.

jaredfholgate commented 5 months ago

Dear gurus,

i have been working on fixing this issue with no luck and i hope i could have some advise here, thank you in advance.

I have a simple yaml file below: 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: vmImage: ubuntu-latest

steps:

  • task: TerraformInstaller@1 inputs: terraformVersion: 'latest'
  • task: TerraformTaskV4@4 inputs: provider: 'azurerm' command: 'init' workingDirectory: '$(System.DefaultWorkingDirectory)/terraform-AzureDevOps/TFAZDO' backendServiceArm: 'Azure subscription 1(1)(bf8d4281-83dd-42b3-b885-13278df4489f)' backendAzureRmResourceGroupName: 'AZTFDO-Storage' backendAzureRmStorageAccountName: 'aztfdostor' backendAzureRmContainerName: 'aztfdoblob' backendAzureRmKey: 'aztfdoblob.state'
  • task: TerraformTaskV4@4 inputs: provider: 'azurerm' command: 'validate' workingDirectory: '$(System.DefaultWorkingDirectory)/terraform-AzureDevOps/TFAZDO'
  • task: TerraformTaskV4@4 inputs: provider: 'azurerm' command: 'apply' workingDirectory: '$(System.DefaultWorkingDirectory)/terraform-AzureDevOps/TFAZDO' environmentServiceNameAzureRM: 'Azure subscription 1(bf8d4281-83dd-42b3-b885-13278df4489f)'

=============================================================

whenever i run the pipeline, the error fails during terraform init:

image

/opt/hostedtoolcache/terraform/1.7.1/x64/terraform init -backend-config=storage_account_name=aztfdostor -backend-config=container_name=aztfdoblob -backend-config=key=aztfdoblob.state -backend-config=resource_group_name=AZTFDO-Storage -backend-config=subscription_id=bf8d4281-83dd-42b3-b885-13278df4489f -backend-config=tenant_id=ebd0b70e-75c8-4904-b36c-5979c95af1e2 -backend-config=client_id= -backend-config=client_secret= ##[error]Error: There was an error when attempting to execute the process '/opt/hostedtoolcache/terraform/1.7.1/x64/terraform'. This may indicate the process failed to start. Error: spawn /opt/hostedtoolcache/terraform/1.7.1/x64/terraform ENOENT

Please can someone advise me where could i be wrong?

This is not an error I have seen before.

I see you are using different Service Connections for init and apply. Is that by design?

There is nothing obviously wrong with the init command shown. If you can trigger the pipeline with diagnostics set, it may provide more insights. You can also set the TF_LOG env var to TRACE if the issue is with the Terraform CLI. See here: https://developer.hashicorp.com/terraform/internals/debugging

WXRK commented 5 months ago

Hi Dcarvalho and Jared, Thank you for replying. I am still newbie and self learning on terraform. so:

@dcarvalh04 could you elaborate further on the technical steps? @jaredfholgate it is not by design, i was just simply creating steps and thank you for spotting my mistake. I shall try to align them both.

dcarvalh04 commented 5 months ago

Hi Dcarvalho and Jared, Thank you for replying. I am still newbie and self learning on terraform. so:

@dcarvalh04 could you elaborate further on the technical steps? @jaredfholgate it is not by design, i was just simply creating steps and thank you for spotting my mistake. I shall try to align them both.

I was having the same exact issue as you did, even tried a different ubuntu version and a different terraform version. The way I solved it is still mysterious since I haven't done much beside rewriting the yaml pipeline using the built-in UI instead of copy-pasting from the sample code:

image

Using this "settings" I rewrote my whole config using the built-in editor and then it worked. Either something wrong on my yaml copy paste either my variables...Can't tell for sure

WXRK commented 4 months ago

i am still not having luck despite manually rewriting the terraform init command settings. Would you mind sharing your yaml file with me? (i might be asking too much).

dcarvalh04 commented 4 months ago
trigger:
  branches:
    include:
      - "*"
pool:
  vmImage: ubuntu-latest
variables:
  - group: <YOUR_GROUP_VAR>
  - name: PLAN
    value: plan.tfplan
jobs:
  - job: terraform
    displayName: "Deploy with Terraform"
    steps:
      - checkout: self
        displayName: Checkout Repository
      - task: Cache@2
        displayName: "TF cache"
        inputs:
          key: '"terraform | $(Agent.OS) | "$(Build.BuildNumber)" | $(Build.SourceVersion)"'
          path: .terraform
      - task: TerraformInstaller@1
        displayName: Install Terraform
        inputs:
          terraformVersion: "latest"
      - task: TerraformTaskV4@4
        displayName: Terraform Init
        inputs:
          provider: "azurerm"
          command: "init"
          backendServiceArm: "$(SERVICE_CONNECTION)"
          backendAzureRmResourceGroupName: "$(BACKEND_AZURE_RESOURCE_GROUP_NAME)"
          backendAzureRmStorageAccountName: "$(BACKEND_AZURE_STORAGE_ACCOUNT_NAME)"
          backendAzureRmContainerName: "$(BACKEND_AZURE_STORAGE_ACCOUNT_CONTAINER_NAME)"
          backendAzureRmKey: "terraform.tfstate"
        env:
          ARM_USE_AZUREAD: true
      - task: TerraformTaskV4@4
        displayName: Lint Terraform
        inputs:
          provider: "azurerm"
          command: "custom"
          customCommand: "fmt"
          commandOptions: "-check=true -diff=true"
          outputTo: "console"
          environmentServiceNameAzureRM: "$(SERVICE_CONNECTION)"
      - task: TerraformTaskV4@4
        displayName: Terraform Validate
        inputs:
          provider: "azurerm"
          command: "validate"
      - task: TerraformTaskV4@4
        displayName: Terraform Plan
        inputs:
          provider: "azurerm"
          command: "plan"
          commandOptions: "-out=$(PLAN)"
          environmentServiceNameAzureRM: "$(SERVICE_CONNECTION)"
        env:
          ARM_USE_AZUREAD: true
      - task: TerraformTaskV4@4
        displayName: Terraform Apply
        condition: and (succeeded(),eq(variables['Build.SourceBranch'], 'refs/heads/main'))
        inputs:
          provider: "azurerm"
          command: "apply"
          commandOptions: "-input=false $(PLAN)"
          environmentServiceNameAzureRM: "$(SERVICE_CONNECTION)"
        env:
          ARM_USE_AZUREAD: true

I used a Variable Group, check Referencing Variable Group in Azure Devops Pipeline