Reputation: 97
I am trying to make terraform create a lambda layer and I am getting "Reference to undeclared resource" Heres the main.tf code in the src directory where I get the error
module "name" {
...
...
layer_arns = [aws_lambda_layer_version.mm_layer.arn]
...
...
}
Heres the lambda-layer.tf resource file inside a lambda-layer folder that I am trying to reference
locals {
region = var.region
}
resource "aws_lambda_layer_version" "mm_layer" {
layer_name = "common-layer"
...
...
compatible_runtimes = ["nodejs12.x"]
}
EDIT: Heres the declared module
module "lambda-layer" {
source = "./modules/lambda-layer"
region = module.common.region[module.common.region]
env = var.env
}
Not sure why it cannot find the reference. I clearly state it in resource that is it mm_layer, and I try to call it as "mm_layer"
Upvotes: 1
Views: 4211
Reputation: 28854
After determining that the declared resourced is in a declared module, and that the resource's exported attributes are absent from the module's declared outputs, we can solve this by declaring the exported attribute as an output for the declared module. This is because exported resource attributes exist only within the same config namespace, and must be accessed using the namespace nomenclature.
Inside the config for the lambda-layer
module source, you need to add a declared output:
output "mm_layer_arn" {
description = "arn for the mm lambda layer"
value = aws_lambda_layer_version.mm_layer.arn
}
You can similarly output the entire object of exported resource attributes by assigning aws_lambda_layer_version.mm_layer
to the value, but this is mostly useful if you need to access or otherwise iterate on that object.
This output will now be accessible from the namespace of the config where the module (in this situation lambda-layer
) is declared. Since module name
is declared in the same config as the lambda-layer
declaration, that module declaration can now access the output in the appropriate namespace:
module "name" {
...
layer_arns = [module.lambda-layer.mm_layer_arn]
...
}
You can read more about module output namespace in the documentation.
Upvotes: 2