地图变量中的局部变量
Local variables inside map variables
我正在尝试使用映射变量(它有 2 个 lambda 名称)。另外,我想在键值内传递一个局部变量,如示例 below.However 所示,我收到一个错误,因为此处不允许使用变量。任何 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"
}
我正在尝试使用一个 lambda 资源块来创建 2 个 lambda 函数(因此使用 map 变量)
您的变量默认值不能动态。它们必须是静态值。因此,在您的情况下,与其使用 var.lambdas
,不如使用 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}
}
你会:'
resource "aws_lambda_function" "main" {
for_each = local.lambdas
....
}
我正在尝试使用映射变量(它有 2 个 lambda 名称)。另外,我想在键值内传递一个局部变量,如示例 below.However 所示,我收到一个错误,因为此处不允许使用变量。任何 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"
}
我正在尝试使用一个 lambda 资源块来创建 2 个 lambda 函数(因此使用 map 变量)
您的变量默认值不能动态。它们必须是静态值。因此,在您的情况下,与其使用 var.lambdas
,不如使用 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}
}
你会:'
resource "aws_lambda_function" "main" {
for_each = local.lambdas
....
}