My lambda node source code is inside the s3 bucket as a zip file
I want that source to be uploaded while executing the aws_lambda_function
resource "aws_lambda_function" "terraform_lambda_func" {
s3_bucket = var.bucket_name
s3_key = "${var.zip_file_name}.zip"
function_name = var.lambdaFunctionName
role = aws_iam_role.okta-iam-v1.arn
handler = "index.handler"
runtime = "nodejs16.x"
}
Wanting it doesn't cut it because that's now the way the relantionship between a lambda and its code works.
What the
aws_lambda_functionresource does is is to say: "there is a lambda function and its code is there in that S3 bucket".Because updating the file in the bucket doesn't automatically update the code that lambda, this resource doesn't have a way to reference new file content directly.
To do so, you need an
aws_s3_objectresource that is able to upload a new file to lambda.To trigger the actual update of the lambda, you also need to pass the file hash to the
aws_lambda_function. Since theaws_s3_objectresource expors a source_hash property, you can link them as such.See How to update aws_lambda_function Terraform resource when ZIP package is changed on S3?