[英]Terraform - How to pass environment variables to sub modules in terraform
[英]How to pass a list of values into Lambda environment variables with Terraform
我有一个 Python 脚本,它向多个收件人发送 email。 我想用 Terraform 设置我的收件人,并将它们作为环境变量传递到我的 Lambda function 中。 我已将 SES 设置为单独的模块。
Lambda function:
def lambda_handler(event, context):
email_to = os.environ["EMAIL"]
# script that sends an email is here
Lambda模块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模块main.tf:
resource "aws_ses_email_identity" "email" {
email = var.email
}
SES 模块输出.tf:
output "email" {
description = "Emails."
value = aws_ses_email_identity.email.email
}
主程序
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(["email1@example.com", "email2@example.com"])
email = each.value
}
output "email" {
value = module.ses.email
}
使用当前设置我得到这个错误:
│ 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".
如何使用 SES 注册新电子邮件并将它们传递到 Python function?
如果你想一次传递所有的电子邮件,它应该是:
env_variables = {
EMAIL = jsonencode(values(module.ses)[*].email)
}
在您的 function 中,您必须将 EMAIL 处理为 json 数据结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.