我有以下地形代码,通过Infrastructure/Lambda/main.tf执行-Infrastructure文件夹是我项目的根模块:
// 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
我是不是错过了一些显而易见的东西?
[由于第一个答案不够而编辑]
您创建的资源应该取决于数据结果。因此,您可以尝试在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
[...]
}