Hello guys,
I’m writing lambda module and want to use local-exec provisioner to create folder, move some files to the folder and execute npm install but the commands are not recognized by Terraform. I have the following module:
modules/lambda/main.tf
resource "null_resource" "lambda_dependencies" {
provisioner "local-exec" {
command = "mkdir -p ./lambda | cp index.js ./lambda | cd ./lambda | npm install --legacy-peer-deps"
}
}
data "archive_file" "payload_zip" {
type = "zip"
source_dir = "./lambda"
output_path = "./payload.zip"
depends_on = [null_resource.lambda_dependencies]
}
resource "aws_lambda_function" "payload" {
function_name = "${var.function_name}"
filename = data.archive_file.payload_zip.output_path
role = aws_iam_role.payload.arn
handler = "${var.lambda_handler}"
runtime = "${var.compatible_runtimes}"
depends_on = [aws_iam_role_policy_attachment.attach_iam_policy_to_iam_role]
source_code_hash = data.archive_file.payload_zip.output_base64sha256
publish = true
}
Below is my lambda/main.tf for the lambda:
provider "aws" {
region = "us-west-2"
}
module "lambda" {
source = "../modules/lambda"
iam_role_name = "tf-lambda-role"
iam_policy_name = "tf-lambda-policy-name"
function_name = "tf-lambda"
lambda_handler = "index.handler"
compatible_runtimes = "nodejs14.x"
publish = true
}
When I run Terraform plan I see the error below:
Planning failed. Terraform encountered an error while generating this plan.
╷
│ Error: Archive creation error
│
│ with module.lambda.data.archive_file.payload_zip,
│ on ../modules/lambda/main.tf line 55, in data "archive_file" "payload_zip":
│ 55: data "archive_file" "payload_zip" {
│
│ error creating archive: error archiving directory: could not archive missing directory: ./lambda