I am trying to get the list of variable using Terraform, below is my directory structure:
.
├── main.tf
├── path_modules
│ └── module_name
│ ├── main.tf
│ └── variables.tf
└── variables.tf
I put the variables in main.tf file as below:
module "module_name"
...
ssh_users = ["user1", "user2", "user3", "user4", "user5", "user6", "user7", "user8", "user9"]
ssh_keys = ["user1.pem.pub", "user2.pem.pub", "user3.pem.pub", "user4.pem.pub", "user5.pem.pub", "user6.pem.pub", "user7.pem.pub", "user8.pem.pub", "user9.pem.pub"]
Then I put code to get that variable in ./path_module/module_name/main.tf file as below:
resource "google_compute_instance" "module_name" {
...
metadata = {
count = length(var.ssh_keys)
ssh-keys = format("%s:%s", "${var.ssh_users[count.index]}", file("${path.module}/${var.ssh_keys[count.index]}"))
}
and after try to validate using terraform validate I got this error:
Error: Reference to "count" in non-counted context
on path_module/module_name/main.tf line number, in resource "google_compute_instance" "module_name":
line number: ssh-keys = format("%s:%s", "${var.ssh_users[count.index]}", file("${path.module}/${var.ssh_keys[count.index]}"))
The "count" object can be used only in "resource" and "data" blocks, and only
when the "count" argument is set.
Error: Reference to "count" in non-counted context
on path_module/module_name/main.tf line number, in resource "google_compute_instance" "module_name":
line number: ssh-keys = format("%s:%s", "${var.ssh_users[count.index]}", file("${path.module}/${var.ssh_keys[count.index]}"))
The "count" object can be used only in "resource" and "data" blocks, and only
when the "count" argument is set.
kindly please anybody have experience regarding this issue on Terraform can help me out