1
votes

We have several developers, each deploying a lambda function (from their own computer) via terraform to a shared account. Terraform state files are in an S3 bucket for consistency. As we're deploying from different computers, the function zipfile may not exist locally. How can I make sure the function zip is always created?

I'm using the following code in a terraform module

resource "null_resource" "zipfile" {
  depends_on = [null_resource.code_dependencies]
  provisioner "local-exec" {
    command = "cd ${var.source_dir} && zip -r function.zip * -x *.zip"
  }
}

resource "aws_lambda_function" "function" {
  depends_on = [null_resource.zipfile]
  filename         = "${var.source_dir}/function.zip"
  function_name    = var.function_name
  role             = aws_iam_role.lambda_function_role.arn
  handler          = var.handler
  source_code_hash = filesha256("${var.source_dir}/function.zip")
  runtime = var.runtime
}
1

1 Answers

2
votes

One way is to use triggers argument of null_resource. Setting a unique value such as the timestamp would trigger the command every time. The downside is that it will run even if the there is no change in the source.

resource "null_resource" "zipfile" {
  provisioner "local-exec" {
     command = "cd ${var.source_dir} && zip -r ${local.filename} * -x *.zip"
  }

  triggers = {
     always_run = "${timestamp()}"
  }
}