0

我正在尝试引用我使用 npm pack 创建并为 lambda 模块构建的现有源文件。当我尝试使用local_existing_package时,我收到来自 lambda 模块的错误,就好像它试图从 s3 容器与本地查找 source.zip 文件一样。这个错误是由 lambda 模块本身产生的

错误:

│   12:   s3_key            = var.s3_existing_package != null ? lookup(var.s3_existing_package, "key", null) : (var.store_on_s3 ? var.s3_prefix != null ? format("%s%s", var.s3_prefix, replace(local.archive_filename, "/^.*//", "")) : replace(local.archive_filename, "/^\\.//", "") : null)
│     ├────────────────
│     │ local.archive_filename is null

生成的 lambda 模块中的代码:

locals {
  archive_filename    = element(concat(data.external.archive_prepare.*.result.filename, [null]), 0)
  archive_was_missing = element(concat(data.external.archive_prepare.*.result.was_missing, [false]), 0)

  # Use a generated filename to determine when the source code has changed.
  # filename - to get package from local
  filename    = var.local_existing_package != null ? var.local_existing_package : (var.store_on_s3 ? null : local.archive_filename)
  was_missing = var.local_existing_package != null ? !fileexists(var.local_existing_package) : local.archive_was_missing

  # s3_* - to get package from S3
  s3_bucket         = var.s3_existing_package != null ? lookup(var.s3_existing_package, "bucket", null) : (var.store_on_s3 ? var.s3_bucket : null)
  s3_key            = var.s3_existing_package != null ? lookup(var.s3_existing_package, "key", null) : (var.store_on_s3 ? var.s3_prefix != null ? format("%s%s", var.s3_prefix, replace(local.archive_filename, "/^.*//", "")) : replace(local.archive_filename, "/^\\.//", "") : null)
  s3_object_version = var.s3_existing_package != null ? lookup(var.s3_existing_package, "version_id", null) : (var.store_on_s3 ? element(concat(aws_s3_bucket_object.lambda_package.*.version_id, [null]), 0) : null)

我引用 ^^ 的模块定义为:

module "my_lambda_fn" {
  source  = "terraform-aws-modules/lambda/aws"
  version = "~>2.17.0"

  function_name = "my-api-fn"
  description   = "Function for example"
  handler       = "src/index.handler"
  runtime       = "nodejs14.x"
  timeout       = 10
  memory_size   = 512
  publish       = true
  cloudwatch_logs_retention_in_days = 60
  attach_tracing_policy = true
  tracing_mode          = "Active"
  # using prepackaged zip vs source
  #https://registry.terraform.io/modules/terraform-aws-modules/lambda/aws/latest#lambda-functions-with-existing-package-prebuilt-stored-locally
  # prebuilt packaging
  create_package         = false
  local_existing_package = "${path.module}/../source.zip"


  store_on_s3 = true
  s3_bucket   = module.s3_bucket.s3_bucket_id

我在这个模块上查看了来自 github 的示例,它看起来与推荐的内容相匹配。

谁能提供一个如何链接源文件的例子或指出我缺少什么?这是使用 terraform 0.13.4 构建的

4

1 回答 1

0

在深入研究 lambda 资源和文档后,问题是我引用了一个 s3 容器来将打包的代码上传到

  store_on_s3 = true
  s3_bucket   = module.s3_bucket.s3_bucket_id

使用本地源为 lambda 部署代码时,aws 会将代码上传到一个私有目录,该目录无法查看,但 lambda 可以访问。从技术上讲,这是 lambdas 的自然部署。我错误地使用了模块,认为我需要上传不需要的 source.zip。删除 s3 引用解决了该问题。我注意到的一个变化是以这种方式发布到处理程序的路由现在需要包名称,所以现在处理程序路径是source/src/index.handler

于 2021-09-28T00:35:48.920 回答