I am trying to create a lambda for each stage in my stages variable then pass in the arn of each role from my lambda_iam_role to each corresponding lambda, when I try applying it I get this, Could someone explain why please?
│ Error: Incorrect attribute value type
│
│ on modules/lambda/lambda.tf line 20, in resource "aws_lambda_function" "lambda_function":
│ 20: role = var.lambda_role_arn
│ ├────────────────
│ │ var.lambda_role_arn is a list of string
│
│ Inappropriate value for attribute
│ "role": string required.
Below is my configuration
iam.tf
variable stages {
type = list(string)
}
variable lambda_iam_role {
type = list(string)
}
data aws_iam_role lambda_iam_role {
for_each = { for names, stage in var.stages : stage => names }
name = element(var.lambda_iam_role, each.value)
}
output lambda_role_arn {
value = {
for stage, role_arn in data.aws_iam_role.lambda_iam_role :
stage =>{
role_name = role_arn.name
role_arn = role_arn.arn
}
}
}
Lambda.tf
variable stages {
type = list(string)
}
variable lambda_role_arn {
type = list(string)
description = "The ARN for the lambda IAM role"
}
resource aws_lambda_function lambda_function {
filename = local.lambda_zip_location
for_each = { for stage in var.stages : stage => "${stage}_data_lambda" }
function_name = each.value
handler = "index.lambda_handler"
role = var.lambda_role_arn
source_code_hash = data.archive_file.lambda-zip.output_base64sha256
runtime = "python3.9"
timeout = "90"
}
Main.tf
variable stages {
type = list(string)
default = "['a','b','c']
}
variable lambda_iam_role {
type = list(string)
default = "['login_role','logout_role','support_role']"
}
variable lambda_role_arn {
type = list(string)
description = "The ARN for the lambda IAM role"
}
module iam {
source = "./modules/iam"
environment = var.environment
stages = var.stages
lambda_iam_role = var.lambda_iam_role
}
module lambda{
source = "./modules/lambda"
lambda_iam_role = module.iam.lambda_role_arn
aws_region = var.aws_region
}
````Preformatted text`