Search code examples
terraformterraform-provider-azure

is there any way to get terraform provider version from the azure blob container


I was reading about the network mirror protocol in terraform and I wanted to fetch the providers from the azure storage container.

For testing I have manually added one provider version in azure storage container like below. enter image description here

And in terraform config file I have added installation path like below. I cropped the URL path of blob file to the container level otherwise the path would look like https://AAAA.blob.core.windows.net/test/terraform-provider-azurerm_v3.52.0_x5

.terraformrc file
provider_installation {
  network_mirror {
    URL    = "https://AAAA.blob.core.windows.net/"
  }
}

I have set below environment variable as well for above file TF_CLI_CONFIG_FILE="/mnt/c/Users/bbbb/Desktop/.terraformrc"

when I run terraform INIT i am getting below error

Could not retrieve the list of available versions for provider hashicorp/azurerm: failed to query provider mirror https://AAAA.blob.core.windows.net/ for
│ registry.terraform.io/hashicorp/azurerm: server returned unsuccessful status 400

Any inputs would be helpful


Solution

  • The directory structure you've created inside your storage container does not match the layout Terraform expects for a network mirror. For this to work you will need to lay out the objects in your container in a way that causes the Azure Blob Storage service to match the behavior Terraform expects.

    There is some relevant guidance on this in the documentation section Provider Mirror as a Static Website.

    That documentation describes using the terraform providers mirror command to generate a directory structure that will, when uploaded into a typical static website hosting system, cause it to implement the network mirror protocol.

    To use it, make a new working directory and place in it just a versions.tf file with the following content:

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

    In the directory containing that new file, run the following commands:

    mkdir provider-mirror
    terraform providers mirror provider-mirror
    

    After this command succeeds, the provider-mirror subdirectory should contain a directory structure including both some .json index files and the main distribution package (a .zip file) for the hashicorp/azurerm provider.

    If you upload that whole nested directory structure into your blob storage container, preserving the directory hierarchy, then your blob storage container's website should then implement the provider mirror protocol.

    Terraform requires that the .json index files included in the mirror have a Content-Type of application/json, so if uploading to the container does not automatically set that Content-Type then you may need to set it yourself manually for each file. One way to achieve that would be to call Set Blob Properties with the x-ms-blob-content-type header set to application/json.