Reputation:
I have a followed directory structure:
| main.tf
| module.tf
| terraform.tf
| terraform.tfvars
\---modules
\---org_bd
\---resource
resource.tf
providers.tf
The module/org_bd/resrouce/provider.tf
contains:
provider "boundary" {
api_key = var.api_key
app_key = var.app_key
}
And these values are declared and defined inside ./terraform.tf
and ./terraform.tfvars
:
However, while trying to plan or validate it gives following error:
Error: Reference to undeclared input variable
on modules\org_bd\resrouce\providers.tf line 13, in provider " boundary":
13: api_key = var.api_key
An input variable with the name "api_key" has not been declared. This variable
can be declared with a variable "api_key" {} block.
As I understand, this is happening because the variable used inside the module are defined outside of it. But I believe its possible that you can provide values from outside to a module while calling it?
Upvotes: 1
Views: 1664
Reputation: 16100
Passing variables to modules is done at the point of invocation. For example (from a project I currently have open:
module "workers" {
source = "./modules/workers"
depends_on = [module.vpc.dns_record]
vpc = module.vpc.vpc
az = module.vpc.az
zone = module.vpc.zone
bastion_sg_id = module.bastion.bastion_sg_id
control_plane_sg_id = module.control_plane.control_plane_sg_id
name = var.name
owner = var.owner
project = var.project
env = var.env
workspace = var.workspace
key_name = var.key_name
public_key_path = var.public_key_path
private_key_path = var.private_key_path
instance_type = var.worker_type
aws_region = var.region
}
Apart from source
and depends_on
, which are built-in properties, on the left of the assignments are the input property names and on the right are the values.
Input variables are declared within the module in a file named something like variables.tf
, though, in my modules I call this input.tf
because the name is arbitrary and I think that input
is more meaningful and matches the common output.tf
that declares output properties.
This example also shows how to use the output of modules. These are normally declared in a file named output.tf
(or something like that). Also shown is the use of variables at this level, they are declared in my top-level input.tf
and I've provided values in variables.auto.tfvars
, which is a special filename recognised by terraform that is automatically loaded without having to specify a var-file option on the command-line.
Upvotes: 2