Azure DevOps: Could not retrieve the list of available versions for provider

Good Day,
im experimenting eith Terraform to deploy some ressources in Azure an Running in an Problem, im not able to solve.

I’ve build the following script:

trigger:

- main

pool:
  vmImage: 'ubuntu-latest'

stages:
- stage: Init
  displayName: 'Stage Terraform Init'
  jobs:
  - job: TerraformInitJob
    displayName: Terraform Init
    steps:
      - task: TerraformTaskV4@4
        inputs:
          provider: 'azurerm'
          command: 'init'
          backendServiceArm: 'HVAzureDevOpsPipeline'
          backendAzureRmResourceGroupName: 'rgHVAzureDevOpsPipeline'
          backendAzureRmStorageAccountName: 'sadevopspipeinecache'
          backendAzureRmContainerName: 'azuredevopscache'
          backendAzureRmKey: 'tf/terraform.tfstate'

If i Run the Pipeline, in the initialisation of Terraform, im getting the following error:

│ Error: Failed to query available provider packages

│ Could not retrieve the list of available versions for provider

│ hashicorp/azuredevops: provider registry registry.terraform.io does not

│ have a provider named Terraform Registry

│ Did you intend to use microsoft/azuredevops? If so, you must specify that

│ source address in each module which requires that provider. To see which

│ modules are currently depending on hashicorp/azuredevops, run the following

│ command:

│ terraform providers

##[error]Error: The process ‘/opt/hostedtoolcache/terraform/1.9.2/x64/terraform’ failed with exit code 1

Maybe someone can give an hint or an usefull info where my error here is.
Best Regards.

This seems to indicate that Terraform is having an issue with your providers.

To start, can you please show us your terraform{} block and any provider {} blocks from your Terraform module. This should enable further help.

Thanks

Hi,
thanks for the fast answer. I think you mean the parts from the .tf Script. You can see there below. Im still experimenting with it, so im thankfull for Tipps.

terraform {
required_providers {
azurerm = {
}
}
}
#Constructors
provider “azurerm” {
features {}
}

Thanks for this. So I can see this is valid so the issue must be somewhere else. Possibly related to the TerraformTaskV4@4 pipeline step as I can see that is also requiring a provider parameter.

So I can try and reproduce the error you are seeing can you please edit your original post: Remove the blockquote formatting > and add code block formatting (use 3x backtick ``` on the line before and the line after your code block) on your YAML pipeline code. This will preserve the plain formatting of the YAML and allow it to be copied and pasted without the special characters that the block quote formatting has introduced.

Thanks

Hello,
i’ve edited the Post. Hope this ist correct, so you can copy correctly.

Hi @StefanKaiserHV,

Unfortunately I have not been able to reproduce your issue. Below is my Pipeline YAML, the only additional parameter I have used is to set the working directory to the subdirectory of my Repo in which my Terraform module resides and I have, of course, updated the other values to reflect my pipeline service connenction:

trigger:
- none

pool:
  vmImage: ubuntu-latest

stages:
- stage: Init
  displayName: 'Stage Terraform Init'
  jobs:
  - job: TerraformInitJob
    displayName: Terraform Init
    steps:
      - task: TerraformTaskV4@4
        inputs:
          provider: 'azurerm'
          command: 'init'
          backendServiceArm: 'Visual Studio Enterprise Subscription – MPN2'
          backendAzureRmResourceGroupName: 'tfstate-mpn2-eun'
          backendAzureRmStorageAccountName: 'tfstatempn2eun'
          backendAzureRmContainerName: 'azuredevopscache'
          backendAzureRmKey: 'tf/terraform.tfstate'
          workingDirectory: 'Terraform'

When running this is the output of the task, which looks as expected:


==============================================================================
/usr/local/bin/terraform init -backend-config=storage_account_name=tfstatempn2eun -backend-config=container_name=azuredevopscache -backend-config=key=tf/terraform.tfstate -backend-config=resource_group_name=tfstate-mpn2-eun -backend-config=subscription_id=*** -backend-config=tenant_id=*** -backend-config=client_id=*** -backend-config=oidc_token=*** -backend-config=use_oidc=true
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of olivr/gpg from the dependency lock file
- Reusing previous version of hashicorp/local from the dependency lock file
- Reusing previous version of hashicorp/random from the dependency lock file
- Reusing previous version of hashicorp/azurerm from the dependency lock file
- Installing hashicorp/random v3.6.0...
- Installed hashicorp/random v3.6.0 (signed by HashiCorp)
- Installing hashicorp/azurerm v3.97.1...
- Installed hashicorp/azurerm v3.97.1 (signed by HashiCorp)
- Installing olivr/gpg v0.2.1...
- Installed olivr/gpg v0.2.1 (self-signed, key ID 6FC310A284A0E356)
- Installing hashicorp/local v2.5.1...
- Installed hashicorp/local v2.5.1 (signed by HashiCorp)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
Terraform has made some changes to the provider dependency selections recorded
in the .terraform.lock.hcl file. Review those changes and commit them to your
version control system if they represent changes you intended to make.

╷
│ Warning: Missing backend configuration
│ 
│ -backend-config was used without a "backend" block in the configuration.
│ 
│ If you intended to override the default local backend configuration,
│ no action is required, but you may add an explicit backend block to your
│ configuration to clear this warning:
│ 
│ terraform {
│   backend "local" {}
│ }
│ 
│ However, if you intended to override a defined backend, please verify that
│ the backend configuration is present and valid.
│ 
╵
Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Finishing: TerraformTaskV4

Can you post the full output of the Pipeline task as I have done - It may provide some further clues.

Thanks

Hello,

thanks for the Infos. I’ve tested it on the fly and get some interessting results. I try to complete my test tomorror and give you an extendet answer. Hope this is ok.
Best Regards

Hi, it has take some time, but i can give you an Status.
Like you reccomended, I’ve added the “Working directory” Entry in the pipeline Skript. To reduce the complexity Ive createt the Folder “terraform_test” and included there an simple .tf file:

erraform {
  required_providers {
    azurerm = {
    }
  }
}
#Constructors
provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "UK South"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-vnet"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  address_space       = ["172.16.0.0/16"]
}

resource "azurerm_subnet" "example" {
  name                 = "example-subnet"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["172.16.1.0/24"]
}

My Pipeline Skript has the following Content:

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

stages:
- stage: Init
  displayName: 'Stage Terraform Init'
  jobs:
  - job: TerraformInitJob
    displayName: 'Terraform Init'
    steps:
    - script: |
        rm -rf .terraform
        rm -f terraform.lock.hcl
      displayName: InitCleanup
    - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
      inputs:
        terraformVersion: 'latest'
    - task: TerraformTaskV4@4
      inputs:
        provider: 'azurerm'
        command: 'init'
        backendServiceArm: 'AzureDevOpsPipeline'
        backendAzureRmResourceGroupName: 'rgAzureDevOpsPipeline'
        backendAzureRmStorageAccountName: 'sadevopspipeinecache'
        backendAzureRmContainerName: 'azuredevopscache'
        backendAzureRmKey: 'tf/terraform.tfstate'
        workingDirectory: 'terraform_test'
        
- stage: Validate
  displayName: 'Stage Terraform Validate'
  jobs:
  - job: TerraformValidate
    displayName: 'Validating TF File'
    steps:
    - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
      inputs:
        terraformVersion: 'latest'
    - task: TerraformTaskV4@4
      inputs:
        provider: 'azurerm'
        command: 'validate'
        workingDirectory: 'terraform_test'

- stage: Plan
  displayName: 'Stage Terraform Plan'
  jobs:
  - job: TerraformPlanJob
    displayName: 'Terraform Plan'
    steps:
    - task: TerraformTaskV4@4
      inputs:
        provider: 'azurerm'
        command: 'plan'
        environmentServiceNameAzureRM: 'AzureDevOpsPipeline'
        workingDirectory: 'terraform_test'

By executing the Pipeline. the installation Task is completed correctly

Starting: TerraformInstaller
==============================================================================
Task         : Terraform tool installer
Description  : Find in cache or download a specific version of Terraform and prepend it to the PATH
Version      : 0.202.26
Author       : Microsoft Corporation
Help         : [Learn more about this task](https://aka.ms/AAf1a0p)
==============================================================================
Getting latest Terraform version.
Downloading: https://releases.hashicorp.com/terraform/1.9.3/terraform_1.9.3_linux_amd64.zip
Extracting archive
/usr/bin/unzip /home/vsts/work/_temp/terraform-1.9.3-d370e402-5d2c-4395-9546-7eb613447297.zip
Archive:  /home/vsts/work/_temp/terraform-1.9.3-d370e402-5d2c-4395-9546-7eb613447297.zip
  inflating: LICENSE.txt             
  inflating: terraform               
Caching tool: terraform 1.9.3 x64
Prepending PATH environment variable with directory: /opt/hostedtoolcache/terraform/1.9.3/x64
Verifying Terraform installation...
/opt/hostedtoolcache/terraform/1.9.3/x64/terraform version
Terraform v1.9.3
on linux_amd64
+ provider registry.terraform.io/hashicorp/azurerm v3.93.0
+ provider registry.terraform.io/hashicorp/local v2.5.1
Finishing: TerraformInstaller

But in the Init Stage im getting the following Output:


Version      : 4.241.26
Author       : Microsoft Corporation
Help         : [Learn more about this task](https://aka.ms/devlabs/ado/tf/task/v4)
==============================================================================
##[debug]Resource Utilization warnings is disabled, switch "DISABLE_RESOURCE_UTILIZATION_WARNINGS" variable to "true" if you want to enable it
##[debug]Using node path: /home/vsts/agents/3.241.0/externals/node16/bin/node
##[debug]agent.TempDirectory=/home/vsts/work/_temp
##[debug]loading inputs and endpoints
##[debug]loading INPUT_PROVIDER
##[debug]loading INPUT_COMMAND
##[debug]loading INPUT_WORKINGDIRECTORY
##[debug]loading INPUT_OUTPUTTO
##[debug]loading INPUT_OUTPUTFORMAT
##[debug]loading ENDPOINT_AUTH_SYSTEMVSSCONNECTION
##[debug]loading ENDPOINT_AUTH_SCHEME_SYSTEMVSSCONNECTION
##[debug]loading ENDPOINT_AUTH_PARAMETER_SYSTEMVSSCONNECTION_ACCESSTOKEN
##[debug]loading SECRET_SYSTEM_ACCESSTOKEN
##[debug]loaded 9
##[debug]Agent.ProxyUrl=undefined
##[debug]Agent.CAInfo=undefined
##[debug]Agent.ClientCert=undefined
##[debug]Agent.SkipCertValidation=undefined
##[debug]check path : /home/vsts/work/_tasks/TerraformTaskV4_fe504acc-6115-40cb-89ff-191386b5e7bf/4.241.26/node_modules/azure-pipelines-tasks-artifacts-common/module.json
##[debug]adding resource file: /home/vsts/work/_tasks/TerraformTaskV4_fe504acc-6115-40cb-89ff-191386b5e7bf/4.241.26/node_modules/azure-pipelines-tasks-artifacts-common/module.json
##[debug]system.culture=en-US
##[debug]check path : /home/vsts/work/_tasks/TerraformTaskV4_fe504acc-6115-40cb-89ff-191386b5e7bf/4.241.26/task.json
##[debug]adding resource file: /home/vsts/work/_tasks/TerraformTaskV4_fe504acc-6115-40cb-89ff-191386b5e7bf/4.241.26/task.json
##[debug]system.culture=en-US
##[debug]provider=azurerm
##[debug]command=validate
##[debug]workingDirectory=/home/vsts/work/1/s/terraform_test
##[debug]commandOptions=undefined
##[debug]which 'terraform'
##[debug]found: '/usr/local/bin/terraform'
##[debug]which '/usr/local/bin/terraform'
##[debug]found: '/usr/local/bin/terraform'
##[debug]/usr/local/bin/terraform arg: validate
##[debug]exec tool: /usr/local/bin/terraform
##[debug]arguments:
##[debug]   validate
/usr/local/bin/terraform validate
##[debug]Agent environment resources - Disk: / Available 21175.00 MB out of 74244.00 MB, Memory: Used 609.00 MB out of 6921.00 MB, CPU: Usage 58.47%
╷
│ Error: Missing required provider
│ 
│ This configuration requires provider
│ registry.terraform.io/hashicorp/azurerm, but that provider isn't available.
│ You may be able to install it automatically by running:
│   terraform init
╵
##[debug]Exit code 1 received from tool '/usr/local/bin/terraform'
##[debug]STDIO streams have closed for tool '/usr/local/bin/terraform'
##[debug]task result: Failed
##[error]Error: The process '/usr/local/bin/terraform' failed with exit code 1
##[debug]Processed: ##vso[task.issue type=error;source=TaskInternal;]Error: The process '/usr/local/bin/terraform' failed with exit code 1
##[debug]Processed: ##vso[task.complete result=Failed;]Error: The process '/usr/local/bin/terraform' failed with exit code 1
Finishing: TerraformTaskV4

Maybe there is some logic i dond understand, so im Happy with every information.

Ahh ha! I can see what the issue is here.

In short - this is an Azure Devops issue, not a Terraform issue:

In Azure DevOps Jobs are run on a pipeline agent. If you are using Microsoft managed pipeline agents then each job will be assigned a different and newly created pipeline agent. The detail is here: Jobs in Azure Pipelines - Azure Pipelines | Microsoft Learn but the pertinent points are:

You can organize your pipeline into jobs. Every pipeline has at least one job. A job is a series of steps that run sequentially as a unit. In other words, a job is the smallest unit of work that can be scheduled to run.

And

Agent pool jobs (eg. Microsoft Hosted Agent Pools)

These are the most common type of jobs and they run on an agent in an agent pool.

  • When using Microsoft-hosted agents, each job in a pipeline gets a fresh agent.

If I summarise your YAML pipeline:

stage: Init

  • job: TerraformInitJob
    steps:
    • script (init clean up)
    • task: (TerraformInstaller)
    • task: TerraformTaskV4@4 (INIT)

stage: Validate

  • job: TerraformValidate
    steps:
    • task: TerraformInstaller@0
    • task: TerraformTaskV4@4 (Validate)

stage: Plan

  • job: TerraformPlanJob
    steps:
    • task: TerraformTaskV4@4 (PLAN)

What is currently happening is that your first job (TerraformInitJob) is executing its 3 tasks. The last of which is the init. And then the job finishes and the agent is disposed of.

Your next job (TerraformValidate) then starts on a new pipeline agent which is back to the baseline config. As this will not include any of the data/files that are created/download by an init, such as the providers, the command errors. You have, correctly, run an initial installer task here to ensure the required terraform version is installed, but not run an init step.

Your 3rd job, should the pipeline get there, would have exactly the same issue. As there is no installer job it would use whatever version ins pre-installed on the agent (if there is one) and as there is no init task, it would error with a similar message to your validate.

Excluding the creation and use of self-hosted pipeline agents which can persist files/data across jobs. You can can approach this is a few different ways, two of which are:

  1. Keep your current pipeline structure and do an install and init in every job
  2. Combine some/all of your stages and jobs and ensure that each job still has the install and init.

My simple approach is as follows (but there are many different approaches)

In the ‘deployment’ pipeline you have just 2 stages - a plan stage and an apply stage.

The plan stage does (in a single job):

  • install
  • init
  • plan (saving to a plan file)
  • Store plan as pipeline artifact

The Apply stage does (in a single job):

  • install
  • init
  • retrieve pipeline artifact
  • Apply (using plan file)

A terraform plan runs an implicit validate anyway - so if the module does not validate the plan stage will error.
If you don’t want the apply to run without a review of the plan:
The Apply stage could either use a ‘manual validation’ task in an additional job inserted to run first, or use the ‘deployment job’ job type, to associate the run to an environment, with validations and approvals set on the environment.

You could also create a separate CI pipeline to run a validate when the module is checked-in/merged shifting that check ‘left’ and hopefully ensuring the code base is valid prior to any deployment pipeline being run.

Hope that helps

Happy Terraforming

1 Like

Hi,
your Post has opend my eyes. I haden’t get in mind, that i need everything in one Step.

In germany i would use the Phase “Wie schuppen von den Augen” like dandruff from the eyes.

Im Thanksfull for your help in this case and wish you an happy Weekend.

My .tf script throws me still some errors, but can continue and thats whats important.

For others, who hafe maybe the same Problem, i’ve rebuild the Pipeline Skript like follows:


trigger: none

pool:
  vmImage: 'ubuntu-latest'

stages:
- stage: TerraformExe
  displayName: 'Stage Terraform Init'
  jobs:
  - job: TerraformInitJob
    displayName: 'Terraform Init'
    steps:
    - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
      displayName: 'Terraform Install'
      inputs:
        terraformVersion: 'latest'
    - task: TerraformTaskV4@4
      displayName: 'Terraform Init'
      inputs:
        provider: 'azurerm'
        command: 'init'
        backendServiceArm: 'HVAzureDevOpsPipeline'
        backendAzureRmResourceGroupName: 'rgAzureDevOpsPipeline'
        backendAzureRmStorageAccountName: 'sadevopspipelinecache'
        backendAzureRmContainerName: 'azuredevopscache'
        backendAzureRmKey: 'tf/terraform.tfstate'
        workingDirectory: 'script_dir'
  
    - task: TerraformTaskV4@4
      displayName: 'Terraform Validate'
      inputs:
        backendServiceArm: 'AzureDevOpsPipeline'
        provider: 'azurerm'
        command: 'validate'
        workingDirectory: 'script_dir'
    - task: TerraformTaskV4@4
      displayName: 'Terraform Plan'
      inputs:
        provider: 'azurerm'
        command: 'plan'
        environmentServiceNameAzureRM: 'AzureDevOpsPipeline'
        workingDirectory: 'script_dir'

1 Like