带有 archive_file 的 Terraform lambda 函数

问题描述 投票:0回答:2

我有以下通过 Infrastructure/Lambda/main.tf 执行的 terraform 代码 - 基础设施文件夹是我项目的根模块:

// Create lambda function
resource "aws_lambda_function" "scheduled_lambda" {
  function_name = "test-schedule-lambda"
  role          = aws_iam_role.iam_for_lambda_scheduler.arn
  runtime       = var.runtime
  filename      = "${path.root}/Lambda/test.zip"
  handler       = "index.lambda_handler"
  // Ensures archive is created prior to the lambda function
  depends_on = [
    data.archive_file.zip_the_python_code
  ]
}

// Create archive file to be used in lambda function
data "archive_file" "zip_the_python_code" {
  type        = "zip"
  source_dir  = "${path.root}/Lambda/scripts"
  output_path = "${path.root}/Lambda/test.zip"
}

当我尝试从管道运行

apply
阶段时,我收到以下错误:

unable to load "./Lambda/test.zip": open ./Lambda/test.zip: no such file or directory
with module.potentium_lambdas.aws_lambda_function.scheduled_lambda,
on Lambda/main.tf line 153, in resource "aws_lambda_function" "scheduled_lambda":

所以它在抱怨

aws_lambda_function.scheduled_lambda resource
和我提供给它的文件。我不明白这一点,因为数据资源上的
output_path
(没有失败)与 lambda 函数
filename
参数完全相同。仅供参考,以下项目的完整树:

├── Backup
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── Containers
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── Databases
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── Lambda
│   ├── main.tf
│   ├── outputs.tf
│   ├── scripts
│   │   └── test.py
│   └── variables.tf
├── Networks
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── Security
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── Storage
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
├── main.tf
├── terraform.tfvars
└── variables.tf

我错过了一些明显的东西吗?

aws-lambda azure-devops terraform terraform-provider-aws
2个回答
0
投票

[已编辑,因为第一个答案不够]

您创建的资源应该取决于数据结果。因此,您可以尝试使用 lambda 依赖的资源中的数据输出:

// Create archive file to be used in lambda function
data "archive_file" "zip_the_python_code" {
  type        = "zip"
  source_dir  = "${path.root}/Lambda/scripts"
  output_path = "${path.root}/Lambda/test.zip"
}
// Ugly wrapper - temporary resource
resource "local_file" "lambda" {
  source = data.archive_file.zip_the_python_code.output_path
  filename = "${path.root}/local_lambda_zip"
}

resource "aws_lambda_function" "scheduled_lambda" {
  [...]
  filename = local_file.lambda.filename
  [...]
}

0
投票

问题可能出在

archive_file
数据源上。该文档表示
archive_file
资源已被弃用并改用数据源。然而,GitHub 上有一个未解决的问题表明情况并非如此。如果您发现在应用阶段找不到存档文件,请尝试使用该资源。另请参阅此相关问题

© www.soinside.com 2019 - 2024. All rights reserved.