I have a Python script that sends an email to multiple recipients. I'd like to set up my recipients with Terraform and pass them into my Lambda function as environment variables. I have set up SES as separate module.
Lambda function:
def lambda_handler(event, context):
email_to = os.environ["EMAIL"]
# script that sends an email is here
Lambda module main.tf:
resource "aws_lambda_function" "test_lambda" {
filename = var.filename
function_name = "test_name"
role = "arn:aws:iam::123:role/lambda_role"
handler = var.handler
source_code_hash = var.source_code_hash
runtime = var.runtime
timeout = var.timeout
kms_key_arn = var.kms_key_arn
memory_size = var.memory
dynamic "environment" {
for_each = var.env_variables != null ? var.env_variables[*] : []
content {
variables = environment.value
}
}
}
SES module main.tf:
resource "aws_ses_email_identity" "email" {
email = var.email
}
SES module outputs.tf:
output "email" {
description = "Emails."
value = aws_ses_email_identity.email.email
}
main.tf
module "lambda_zip" {
source = "../modules/lambda_zip"
handler = "lambda_function.lambda_handler"
runtime = "python3.8"
filename = "setup.zip"
env_variables = {
EMAIL = module.ses[*].email
}
}
module "ses" {
source = "../modules/ses"
for_each = toset(["[email protected]", "[email protected]"])
email = each.value
}
output "email" {
value = module.ses.email
}
With the current setup I get this error:
│ Error: Unsupported attribute
│
│ on main.tf line 24, in module "lambda_zip":
│ 24: EMAIL = jsonencode(module.ses[*].email)
│
│ This object does not have an attribute named "email".
╵
╷
│ Error: Unsupported attribute
│
│ on main.tf line 40, in output "email":
│ 40: value = module.ses.email
│ ├────────────────
│ │ module.ses is object with 2 attributes
│
│ This object does not have an attribute named "email".
How can I register new emails with SES and also pass them into the Python function?
CodePudding user response:
If you want to pass all emails at once, it it should be:
env_variables = {
EMAIL = jsonencode(values(module.ses)[*].email)
}
In your function then, you have to process the EMAIL as json data structure.