I have a null_resource executing a docker command which creates a lambda zip file (it is the only way, since i need some compiled libraries that I would normally get with apt-get or yum install) - hence not using other terraform provided modules for creating lambda files:
resource "null_resource" "install_dependencies" {
provisioner "local-exec" {
command = "docker run -w ${path.cwd}/lambda_folder/ foot.bar.docker >> ${path.cwd}/lambda_folder/lambda.zip"
}
}
in order then to upload the lambda file with aws_lambda_function resource, i need to use local_file datasource:
data "local_file" "lambda_file" {
filename = "${path.module}/lambda_folder/lambda.zip"
depends_on = [
null_resource.install_dependencies
]
}
uploading lambda resource:
resource "aws_lambda_function" "database_rotation" {
vpc_config {
subnet_ids = data.aws_subnet_ids.subnet_ids.ids
security_group_ids = [data.aws_security_group.rotation_sg.id]
}
filename = data.local_file.lambda_file.content
function_name = "foo_bar_lambda"
role = aws_iam_role.iam_for_lambda.arn
handler = "lambda_function.lambda_handler"
runtime = "python3.8"
timeout = 120
environment { variables = var.env_vars }
depends_on = [null_resource.install_dependencies]
}
This all works fine on a first run - before i have any state of null_resource and data.local_file.lambda_file - but once my initial deployment was successful, terraform will fail with following error:
Error: open ./lambda_folder/lambda.zip: no such file or directory
on main.tf line 62, in data "local_file" "lambda_file":
62: data "local_file" "lambda_file" {
Which makes sense - null_resource did not run and did not create lambda.zip file.
My question is, how to get around this ? How to make sure that either data.local_file.lambda_file is not triggered every time or if null_resource is executed every time i run terraform and thus creating/uploading the same lambda.zip file
I am a bit lost and i would appreciate any help. Thank you!