I am trying to use a map variable(which has 2 lambda names). Also, I want to pass a local variable inside the key values, as shown in the example below.However, I get an error as variable not allowed here. Any suggestions/advice?
variables.tf:
variable "lambdas" {
type = map(string)
default = {
"lambda1_name-${local.global_suffix}" = "lambda_function1",
"lambda2_name-${local.global_suffix}" = "lambda_function2"
}
}
locals{
global_suffix = "${var.env}-${var.project}${var.branch_hash}"
}
main.tf:
resource "aws_lambda_function" "main" {
for_each = var.lambdas
function_name = each.key
handler = "${each.value}.${var.handler}"
filename = "${path.module}/modules/lambda-main/${each.value}.zip"
source_code_hash = data.archive_file.init[each.key].output_base64sha256
role = module.lambda_iam_role.arn
runtime = "python3.6"
memory_size = "2048"
timeout = "900"
tags = local.tags
description = "${var.project} Lambda Function"
}
I am trying to use one lambda resource block to create 2 lambda functions(hence using the map variable)
Your variable default values can't by dynamic. They must be static values. Thus, instead of having var.lambdas, in your case it would be better to use locals:
variable "lambdas" {
type = map(string)
default = {
"lambda1_name" = "lambda_function1",
"lambda2_name" = "lambda_function2"
}
}
locals {
lambdas = {for key, value in var.lambdas: "${key}-${local.global_suffix}" => value}
}
Them you would:'
resource "aws_lambda_function" "main" {
for_each = local.lambdas
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With