I am working on an AWS CodePipeline project where I want to build and deploy a Java Lambda function whenever there are changes to my source code repository. I have created Terraform configuration to deploy the Lambda function and upload the Java JAR file to it. This is working when I run Terraform locally, however when the CodePipeline executes, it fails with an error indicating it cannot find the target JAR file to upload to Lambda. I have tried using absolute paths, the path.module function, and other solutions for referencing the JAR file location but none have successfully allowed CodePipeline to locate the artifact.
The project structure consists of:
- A root directory with
main.tfcontaining a module block that calls the Terraform module code. - A
/terraform-moduledirectory withlambda.tfwhich contains the Lambda resource definition.
resource "aws_lambda_function" "java_lambda_function" {
runtime = "${var.lambda_runtime}"
filename = "${path.module}/../target/code.zip"
source_code_hash = "${data.archive_file.lambda_archive.output_base64sha256}"
function_name = "java_lambda_function"
handler = "${var.lambda_function_handler}"
timeout = 60
memory_size = 256
role = "${aws_iam_role.iam_role_for_lambda.arn}"
depends_on = ["aws_cloudwatch_log_group.log_group"]
}
data "archive_file" "lambda_archive" {
type = "zip"
source_file = "${path.module}/../target/handler-1.0-SNAPSHOT.jar"
output_path = "${path.module}/../target/code.zip"
}
My goal is to figure out the proper way to reference the JAR file location so that CodePipeline can locate it and successfully upload it to the Lambda function during deployment.
>Solution :
The JAR file would be git-ignored and built separately before running the Terraform code. The Terraform would then zip up the freshly built JAR and deploy it to Lambda. Check your target folder and JAR file, are they git-ignored? If yes, you can remove them and give it another try.
Refer to the Link.
Kindly, let me know if it works.