Hi,
I’m trying to use the following module to create an AWS network load balancer. Some of the values that are needed to be passed to the module are already in variables, for example, this is the parameters of that module:
module "network_lb" {
source = "../modules/nlb"
nlb_config = var.test_nlb_config
forwarding_config = var.test_forwarding_config
tg_config = var.test_tg_config
}
# variables.tf
variable "test_nlb_config" {
default = {
name = "test-nlb"
internal = "false"
environment = "test"
subnet = <subnet_id>
nlb_vpc_id = <vpc_id>
}
}
variable "test_tg_config" {
default = {
name = "test-nlb-tg"
target_type = "instance"
health_check_protocol = "TCP"
tg_vpc_id = <tg_creation_vpc_id>
target_id1 = <one of instance_id/ip/arn>
}
}
variable "test_forwarding_config" {
default = {
80 = "TCP"
443 = "TCP" # and so on }
}
As you can see, all values are strings, but for example, I already have variables for vpc_id, and subnet id values. But If instead of those hardcoded values I put those variables I get this error:
on variables.tf line 21, in variable “test_tg_config”:
21: tg_vpc_id = “${data.aws_vpc.selected.id}”
Variables may not be used here.
How can I pass such variables to a modules ?
Thanks.
When you declare a variable, you can’t pass references into it as the variables are initialzed before any parsing is done.
So, you need to use the locals construct instead as that is evaluated later in the plan/apply process.
Ok, testing this but I must be doing something wrong as I still get the same error. I added to the variables.tf file the following:
# variables.tf
locals {
vpc_id = "${data.aws_vpc.selected.id}"
subnet_id = "${aws_subnet_ids.default_subnet.id}"
environment = "${var.environment}"
targets = "${aws_instance.worker.*.id[count.index]}"
}
variable "test_nlb_config" {
default = {
name = "staging-nlb"
internal = "false"
environment = local.environment
subnet = local.subnet_id
nlb_vpc_id = local.vpc_id
}
}
variable "test_tg_config" {
default = {
name = "staging-nlb-tg"
target_type = "instance"
health_check_protocol = "TCP"
tg_vpc_id = local.vpc_id
target_id1 = local.targets
}
}
But I still get the same error:
Error: Variables not allowed
on variables-nlb.tf line 16, in variable "test_nlb_config":
16: environment = local.environment
Variables may not be used here.
So I suppose I’m declaring the locals parameter in the wrong place ? I’m very confused with this.
You are still passing references inside the variable declaration, and not inside the local definition - it should be the other way around.
When you declare the default value of a variable, you cannot pass any references, only hardcoded values.
variable "environment" {
default = "test"
}
(when you provide a default value, you don’t need to typecast)
But when you define a local, you can reference variables, resources and other locals.
locals {
test_nlb_config = {
name = "staging-nlb"
internal = "false"
environment = var.environment
subnet = data.aws_subnet.default_subnet.id
nlb_vpc_id = data.aws_vpc.selected.id
}
}
(only use string interpolation when required, not when only passing references)
And then use that as parameter when instantiating the module
module "network_lb" {
source = "../modules/nlb"
nlb_config = local.test_nlb_config
}
2 Likes