tibordp / terraform-hcloud-dualstack-k8s

Terraform module for a dual-stack Kubernetes cluster on Hetzner Cloud
https://registry.terraform.io/modules/tibordp/dualstack-k8s/hcloud
MIT License
34 stars 12 forks source link

The argument "token" is required, but was not set #12

Closed laukaichung closed 3 years ago

laukaichung commented 3 years ago

I just installed the module but I can't get it to work. It says the argument token is required.

╷ │ Error: Missing required argument │ │ The argument "token" is required, but was not set. ╵

I have to add an hcloud provider block in .terraform/modules/k8s/main.tf to make the error go away.

terraform {
  required_providers {
    hcloud = {
      source  = "hetznercloud/hcloud"
      version = ">= 1.26.0"
    }
  }
}

locals {
  all_nodes = concat(module.master, module.worker)
}

######HERE#######
provider "hcloud" {
  token = var.hcloud_token
}

Here's my execution file.

variable "hcloud_token" {}

terraform {
  required_providers {
    hcloud = {
      source = "terraform-providers/hcloud"
     version = "1.26.0"
    }
  }
  required_version = ">= 0.13"
}

provider "hcloud" {
  token = var.hcloud_token
}

resource "hcloud_ssh_key" "key" {
  name       = "key"
  public_key = file("~/.ssh/id_rsa.pub")
}

module "k8s" {
  source             = "tibordp/dualstack-k8s/hcloud"
  version            = "0.5.0"
  name               = "k8s"
  hcloud_token       = var.hcloud_token
  hcloud_ssh_key     = hcloud_ssh_key.key.id
  location           = "hel1"
  master_server_type = "cx31"
  worker_server_type = "cx31"
  worker_count       = 2
}

output "kubeconfig" {
  value = module.k8s.kubeconfig
}

input.tfvars

hcloud_token = "123...."
terraform plan -var-file=input.tfvars
tibordp commented 3 years ago

The way I understand, the provider configuration should implicitly flow to the child modules, so it should not be required to pass the Hetzner API token to the module. The reason it is there as a variable is because it is also needed to configure the Cloud Controller Manager after the cluster is bootstrapped.

https://www.terraform.io/docs/language/modules/develop/providers.html#implicit-provider-inheritance

Could it be a Terraform version thing? I tested this with 1.15. I also noticed that yours says source = "terraform-providers/hcloud" not source = "hetznercloud/hcloud".

You could also try explicitly passing a provider to the module in to see if this helps. If it works I can update the examples with this.

module "k8s" {
  source             = "tibordp/dualstack-k8s/hcloud"
  version            = "0.5.0"
  name               = "k8s"
  hcloud_token       = var.hcloud_token
  hcloud_ssh_key     = hcloud_ssh_key.key.id
  location           = "hel1"
  master_server_type = "cx31"
  worker_server_type = "cx31"
  worker_count       = 2

  providers = {
    hcloud = hcloud
  }
}
laukaichung commented 3 years ago

Thanks, I'll test it later. Apart from that, I'm able to install it without any issues.

laukaichung commented 3 years ago

You are right! It should be hetznercloud/hcloud, not terraform-providers/hcloud.