I have a repo with my terraform module where i create a zip file using archive file which i then use as a filesource for a lambda
data "archive_file" "example" {
type = "zip"
output_path = "./files/example.zip"
source {
content = templatefile("${path.module}/files/example.py", {})
filename = "example.py"
}
}
resource "aws_lambda_function" "example_lambda" {
filename = data.archive_file.example.output_path
source_code_hash = data.archive_file.example.output_base64sha256
etc...
}
And in this repo example.py file exists and the file path is ./files/example.zip
Then in my project repo i call the module:
module "my_example_module" {
source = "git::https://github.com/my-module-repo"
project_name = var.project_name
etc...
}
But when i try to apply this it will error saying it can't find the zip file but i think it is looking in my project's directory rather than the module's directory:
module.example.aws_lambda_function.example_lambda: Creating...
Error: reading ZIP file (./files/example.zip): open ./files/example.zip: no such file or directory
Is ${path.module} not correct to use here? How do i get the terraform apply to look in the module for the zip file?
I tried hard typing the path just to see if it would get past the error but don't think i was putting in the right path to a directory in a module - would it be like module.my_example_module../files/example.py
?
EDIT
So i think i understand the problem a bit more now. I changed the code a bit so the output path uses path.module:
data "archive_file" "example" {
type = "zip"
output_path = "${path.module}/files/example.zip"
source {
content = templatefile("${path.module}/files/example.py", {})
filename = "example.py"
}
}
and now when i call the module in my project it is looking for the path: ".terraform/modules/my_example_module/files/example.zip"
which is correct.
but then because the lambda function in the module is set to use the output_path
from the data archive block it will also look in ".terraform/modules/my_example_module/files/example.zip"
in the module repo which wouldn't exist (unless terraform just creates this these directories if they don't already exist?)
So now the question is, how do I:
- Have my project, where i call the module, look for
".terraform/modules/my_example_module/files/example.zip"
- But then have the module just look for
./files/example.zip
My project file structure is:
project
│ README.md
│
└───terraform
│ └───files
│ │ lambda.tf
│ │ main.tf
│ │ ...