我已经尝试使用terraform部署1个lambda。问题是-我计划仅使用当前创建的代码部署lambda,如果我需要使用相同的代码但具有不同的环境变量来部署第二,第三和第四rmbda,该怎么办?请参见下面的示例方案
LAMBDA-1 = hello-Earth
ENV VARS:
KMS-KEY-1 = data.aws_kms_key.key1.id
KMS-KEY-2 = data.aws_kms_key.key2.id
ENV = DEV
LAMBDA-2 = hello-Mars
ENV VARS:
KMS-KEY-1 = data.aws_kms_key.key1.id
KMS-KEY-2 = data.aws_kms_key.key2.id
ENV = SQA
VARIETY = HOT
SIZE = LARGE
LAMBDA-3 = hello-Neptune
ENV VARS:
KMS-KEY-1 = data.aws_kms_key.key1.id
KMS-KEY-2 = data.aws_kms_key.key2.id
ENV = UAT
VARIETY = COLD
SIZE = MEDIUM
DISTANCE = FAR
LAMBDA-4 = hello-Pluto
ENV VARS
ENV = PROD
SIZE = SMALL
DISTANCE = FAR
aws_lambda_function资源具有environment argument。您可以在其中定义环境变量。
因此您的lambda函数资源将是相同的,但具有不同的环境参数,如下所示:
resource "aws_lambda_function" "hello-Earth" {
filename = "lambda_function_payload.zip"
function_name = "hello-Earth"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.test"
source_code_hash = "${filebase64sha256("lambda_function_payload.zip")}"
runtime = "nodejs8.10"
environment {
variables = {
KMS-KEY-1 = data.aws_kms_key.key1.id
KMS-KEY-2 = data.aws_kms_key.key2.id
ENV = "UAT"
VARIETY = "COLD"
SIZE = "MEDIUM"
DISTANCE = "FAR"
}
}
}
resource "aws_lambda_function" "hello-Mars" {
filename = "lambda_function_payload.zip"
function_name = "hello-Mars"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.test"
source_code_hash = "${filebase64sha256("lambda_function_payload.zip")}"
runtime = "nodejs8.10"
environment {
variables = {
KMS-KEY-1 = data.aws_kms_key.key1.id
KMS-KEY-2 = data.aws_kms_key.key2.id
ENV = "SQA"
VARIETY = "HOT"
SIZE = "LARGE"
}
}
}