aws-lambdaterraform-provider-aws

Package Lambda and common util functions through Terraform


I have a series of lambda functions that use some common util functions specific to my environment. These lambdas are written in python, and typical folder structure looks like

 src/
     lambda_fn1_dir/filename1.py
                  /filename2.py
     lambda_fn2_dir/filenameA.py
                   /filenameB.py

     codelib/helpder_util_fn.py

I import helper_util_fn into my lambda functions and run the lambda. Function is deployed using Terraform. What's the best way to define archive_file to point to my source definition in the aws_lambda_function resource in such a way that I can also package codelib into the zip file.

I can use archive_file resource with source_path as explained here. Can I use source_dir attribute if I am more inclined to just group at the directory level. How to handle this scenario in terraform.


Solution

  • Yes, you can but this use case is best suited for AWS Lambda Layers: https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html

    You can create a Layer with all your helper files and package them using terraform archive_file. Then you can config your lambdas for using the same layer.