首页 > 解决方案 > 使用 terraform 部署多个 lambda 函数

问题描述

由于此SO Answer,请勿将此标记为重复

我有一个“aws_lambda_function”资源,它工作正常。

现在我想部署另一个 lambda 函数,我尝试使用不同的处理程序和别名复制整个块,但它会引发错误。有没有其他方法可以做到这一点。

提前致谢。

更新

这是地形代码:

resource "aws_lambda_function" "api_service" {
  function_name = "${substr("${local.api_artifact_name}", 0, min(64, length(local.api_artifact_name)))}"

  # Artifacts bucket
  s3_bucket = "${local.artifacts_bucket_name}"
  s3_key    = "${module.artifact-upload.artifact_key}"

  # "index" is the filename within the zip file (main.js) and "handler"
  # is the name of the property under which the handler function was
  # exported in that file.
  handler = "index.api"

  runtime = "nodejs8.10"
  role    = "${module.api-service-iam.iam_role_arn}"

  # Optional, but ensures that things don't constantly refresh during local development
  source_code_hash = "${base64sha256(file("${local.api_dist_dir}"))}"

  environment {
    variables  =  {
      ...
    }
  }
}

现在资源api_service成功部署了一个 Lambda 函数,但我该如何部署,比如说,5 个这样的函数?

All these Lambda functions will be invoked by an API Gateway later.

标签: aws-lambdaterraformterraform-provider-aws

解决方案


所以基本上答案一直盯着我的脸。

我复制了整个资源块并进行了以下更改:

resource "aws_lambda_function" "lambda-1" {
  function_name = "lambda-1-${substr("${local.api_artifact_name}", 0, min(64, length(local.api_artifact_name)))}"

  # Artifacts bucket
  s3_bucket = "${local.artifacts_bucket_name}"
  s3_key    = "${module.artifact-upload.artifact_key}"

  # "index" is the filename within the zip file (main.js) and "handler"
  # is the name of the property under which the handler function was
  # exported in that file.
  handler = "lambda-1/index.api"

  runtime = "nodejs8.10"
  role    = "${module.api-service-iam.iam_role_arn}"

  # Optional, but ensures that things don't constantly refresh during local development
  source_code_hash = "${base64sha256(file("${local.api_dist_dir}"))}"

}

resource "aws_lambda_function" "lambda-2" {
  function_name = "lambda-2-${substr("${local.api_artifact_name}", 0, min(64, length(local.api_artifact_name)))}"

  # Artifacts bucket
  s3_bucket = "${local.artifacts_bucket_name}"
  s3_key    = "${module.artifact-upload.artifact_key}"

  # "index" is the filename within the zip file (main.js) and "handler"
  # is the name of the property under which the handler function was
  # exported in that file.
  handler = "lambda-2/index.api"

  runtime = "nodejs8.10"
  role    = "${module.api-service-iam.iam_role_arn}"

  # Optional, but ensures that things don't constantly refresh during local development
  source_code_hash = "${base64sha256(file("${local.api_dist_dir}"))}"

}

确保它们具有不同的函数名称


推荐阅读