使用Terraform部署多个Lambda函数

时间:2018-11-09 09:20:30

标签: aws-lambda terraform terraform-provider-aws

由于此SO Answer

,请勿将其标记为重复

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

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

谢谢。

更新

这是Terraform代码:

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.

2 个答案:

答案 0 :(得分:1)

我基本上每个lambda创建一个目录,并为诸如policy.json,ssm_parameters.json之类的工件命名。

1)我使用外部数据源在目录中获取列表lambda函数,并获取每个Lambda所需的所有元数据 2)我使用count =“ N”来部署每个lambda资源。

答案 1 :(得分:0)

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

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

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}"))}"

}
  

确保它们具有不同的功能名称