【问题标题】:Deploy multiple lambda functions using terraform使用 terraform 部署多个 lambda 函数
【发布时间】:2019-04-12 20:31:46
【问题描述】:

不要因为这个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.

【问题讨论】:

  • 您的 Terraform 代码是什么样的?运行 terraform planterraform apply 时遇到的确切错误是什么?
  • 这是个好问题,我会更新我的帖子。
  • 这是一个很好的问题,我也在努力解决这个问题。我有 20 个 Lambda 函数要部署。我能想到的唯一方法是使用 vars 作为type=map,然后在映射中列出每个单独的 Lambda 函数配置,然后在 tf 脚本中使用 count=??${count.index} 循环遍历映射。不过还没试过,我还在努力。
  • @Geet Choubey 你找到方法了吗?
  • @dege 是的,它起作用了。

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


【解决方案1】:

我基本上为每个 lambda 创建一个目录,并为 policy.json、ssm_parameters.json 等工件使用命名约定。

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

【讨论】:

  • 这是我要遵循的第二种方法,但由于 lambda 的数量较少,我只能使用多个块来部署它。不过还是谢谢。
【解决方案2】:

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

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

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

}

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

【讨论】:

    猜你喜欢
    • 2023-02-21
    • 2021-04-08
    • 2022-01-04
    • 2023-03-09
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 2021-10-05
    • 2021-04-18
    相关资源
    最近更新 更多