Terraform : How lambda to refer s3 in terraform resource `aws_lambda_function`

2k views Asked by At

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"
}
1

There are 1 answers

0
Andrei Dascalu On BEST ANSWER

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_function resource 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_object resource 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 the aws_s3_object resource 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?