A Terraform module to create AWS Lambda zipfiles of Python projects
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
scripts
.gitignore
LICENSE
README.md
config.tf
main.tf
output.tf
variables.tf

README.md

Python-Lambda

This is a Terraform module implements the necessary moving parts to take a path that contains a Python project and a requirements.txt file, and will compile that path into a zip payload for deployment to AWS Lambda.

It requires that you have the following packages installed:

  • jq
  • pyenv
  • openssl
  • BSD md5
  • python2.7 and python3.6, selectable via pyenv
  • virtualenv, in the selected python runtime, installed via pip
  • terraform v0.11.2 or higher. This project may be usable with lower, but it is untested.

How It Works

This module makes extensive use of null_resources and temporary directories (from $TMPDIR) to construct a virtualenv and project directory that are zipped according to the AWS documentation.

Because it uses temporary directories extensively, it requires a user-provided output path to ensure that the written zip is not cleaned up during normal system maintenance.

Building a private virtualenv and project directory is used to perform python -m compileall, in order to create the appropriate .pyc files. This is done to ensure faster startup time for the Lambda function.

Payload zip files are written in the form of ${var.name}_{epoch}_payload.zip. This is done to provide a stable indicator of whether or not a file has been deleted, and if it needs to be re-created.

Usage


    module "zip_test" {
      source            = "github.com/Eiara/terraform_lambda_zip"
      name              = "test"
      project_path      = "${path.module}/lambda"
      output_path       = "${path.module}/output_path"
      runtime           = "python3.6"
      requirements_file = "${path.module}/lambda/requirements.txt"
    }
  • name: the name of this zip. used to construct the payload zip filename.
  • project_path: The path to your Python lambda project, not the .py file.
  • output_path: Where to write the final zip.
  • runtime: Optional. Defaults to python3.6.
  • requirements_file: Optional. Not providing a requirements.txt will still build a virtualenv, but will otherwise work normally.

Outputs

  • path: The path to the final zip. This will be in the form of ${var.name}_{epoch}_payload.zip, to allow for multiple uses of this module in a project
  • sha256: a base64sha256()-compatible sha256 representing the archive, for use in source_code_hash.
  • filename: The filename being exported.
  • md5: Suitable for using in the S3 bucket object etag.

License

This project is copyright 2018 Eiara Limited, and licensed under the terms of the MIT license.