Search code examples
terraformproxmox

Terraform custom provider


I'm new to Terraform and I'd like to try it out by setting up my new lab. The lab hosted on remote proxmox hypervisor, and my terraform is running on docker container. I downloaded the proxmox provider from github (https://github.com/andrexus/terraform-provider-proxmox).

If got it right, I need to write a terraform config file that specifies the provider location inside container ($HOME/.terraformrc) with the following instruction:

providers {
  proxmox = "/path/to/provider/directory"
}

and then I wrote a main.tf in order to validate the terraform provider:

provider "proxmox" {
  host = "https://myproxmux:port"

I tried to to set up the provider path with url and local path. I also tried to specify the path in terraform init command with --plugin-dir=path/to/dir but I get the following error:

2020/12/02 19:53:00 [INFO] Terraform version: 0.13.5
2020/12/02 19:53:00 [INFO] Go runtime version: go1.14.7
2020/12/02 19:53:00 [INFO] CLI args: []string{"/usr/local/bin/terraform", "init"}
2020/12/02 19:53:00 [DEBUG] Attempting to open CLI config file: /root/.terraformrc
2020/12/02 19:53:00 Loading CLI configuration from /root/.terraformrc
2020/12/02 19:53:00 [DEBUG] checking for credentials in "/root/.terraform.d/plugins"
2020/12/02 19:53:00 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2020/12/02 19:53:00 [DEBUG] will search for provider plugins in /root/.terraform.d/plugins
2020/12/02 19:53:00 [TRACE] getproviders.SearchLocalDirectory: /root/.terraform.d/plugins is a symlink to /root/.terraform.d/plugins
2020/12/02 19:53:00 [WARN] local provider path "/root/.terraform.d/plugins/proxomx/proxmox/config.go" contains invalid type "config.go"; ignoring
2020/12/02 19:53:00 [WARN] local provider path "/root/.terraform.d/plugins/proxomx/proxmox/resource_storage.go" contains invalid type "resource_storage.go"; ignoring
2020/12/02 19:53:00 [WARN] local provider path "/root/.terraform.d/plugins/proxomx/proxmox/resource_vm.go" contains invalid type "resource_vm.go"; ignoring
2020/12/02 19:53:00 [DEBUG] ignoring non-existing provider search directory /root/.local/share/terraform/plugins
2020/12/02 19:53:00 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2020/12/02 19:53:00 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2020/12/02 19:53:00 [INFO] CLI command args: []string{"init"}

Initializing the backend...

Initializing provider plugins... Finding latest version of hashicorp/proxmox...
2020/12/02 19:53:00 [TRACE] Meta.Backend: no config given or present on disk, so returning nil config
2020/12/02 19:53:00 [TRACE] Meta.Backend: backend has not previously been initialized in this working directory
2020/12/02 19:53:00 [DEBUG] New state was assigned lineage "bbde6bfd-55be-d7fa-f473-6aa043f492ca"
2020/12/02 19:53:00 [TRACE] Meta.Backend: using default local state only (no backend configuration, and no existing initialized backend)
2020/12/02 19:53:00 [TRACE] Meta.Backend: instantiated backend of type
2020/12/02 19:53:00 [DEBUG] checking for provisioner in "."
2020/12/02 19:53:00 [DEBUG] checking for provisioner in "/usr/local/bin"
2020/12/02 19:53:00 [DEBUG] checking for provisioner in "/root/.terraform.d/plugins"
2020/12/02 19:53:00 [INFO] Failed to read plugin lock file .terraform/plugins/linux_amd64/lock.json: open .terraform/plugins/linux_amd64/lock.json: no such file or directory
2020/12/02 19:53:00 [TRACE] Meta.Backend: backend does not support operations, so wrapping it in a local backend
2020/12/02 19:53:00 [TRACE] backend/local: state manager for workspace "default" will:

Error: Failed to install provider

Error while installing hashicorp/proxmox: provider registry registry.terraform.io does not have a provider named registry.terraform.io/hashicorp/proxmox

So, I can see that my terraformrc file is red but I don't understand why my provider is not used and what I do wrong...

Any ideas ? :) Thanks in advance for your replies!


Solution

  • Unfortunately this provider seems to not have been updated for recent versions of Terraform, and so it would be compatible only with Terraform v0.11 or earlier.

    Providers compatible with the latest Terraform versions (at the time of writing) are published in the Terraform Registry. The particular "proxmox" provider you were aiming to use is not published there, but at the time I'm writing this comment there are three other providers of type proxmox known to the registry, each one of them unfortunately a little different than the others. I'm not familiar with any of these providers or with Proxmox, so I can't say which of these would be best to choose.

    Once you do choose which one to use though, the good news is that on modern Terraform you don't have to do any manual installation steps, because you can specify these registry-published modules directly in your Terraform configuration using Provider Requirements:

    terraform {
      required_providers {
        proxmox = {
          source = "username/proxmox"
        }
      }
    }
    

    The meaning of the above is to declare that when you use the provider name proxmox elsewhere in your module you want Terraform to understand that to mean the username/proxmox provider. I've used "username" here as a generic placeholder for any one of the available Proxmox providers; if you select the "Use Provider" button at the top right of the registry detail pages you can see the specific source address to use for each of them.

    After adding a declaration like the above to your module, you should be able to run terraform init in the root module directory and see Terraform install the selected module from the registry. You can then write a provider "proxmox" block to configure the provider and declare resources belonging to it in the usual way.


    (The .terraformrc setting providers that you showed in your question is obsolete and has been replaced with the new registry-based installation mechanism. Installing directly from the registry is the easy, automatic case, but if you have more complex requirements there are some other alternatives you can specify in .terraformrc, documented under Provider Installation.)