How to achieve multiple files and directories in terraform

Viewed 5098
data "archive_file" "example" {
  type        = "zip"
  output_path = "${local.dest_dir}/hello_upload.zip"

  source_file = "${local.src_dir}/hello.py"
  source_dir = "${local.src_dir}/pytz"
  source_dir = "${local.src_dir}/pytz-2018.5.dist-info"
}

note that hello.py need to import pytz which is not included in Lambda that is why I want to upload the package.

when I run the above terraform I got error: "source_dir": conflicts with source_file. Then How can I upload both my lambda file hello.py and the package pytz which is a directory?

2 Answers

I had a similar issue when I wanted to add python lib defined by symbolic links (also known as "symlinks"). The archive provider of terraform is buggy on that case.

I walked around it by using a null_ressouce to complete the zip archive :

resource "null_resource" "add_my_lib" {
  provisioner "local-exec" {
    command = "zip -ur ./archive.zip /path/to/the/lib"
  }
}

Then don't forget to add a depends_on attribute to the resource using the archive.


resource "aws_s3_bucket_object" "my_lambda_layer" {
  ...

  depends_on = [null_resource.add_my_lib]
}

You might be able to use an external data source to copy all files to a temporary directory and then archive that directory.

Related