An AWS Lambda function using a Lambda layer

AWS Lambda

Create Lambda function with a Lambda layer

This sample project demonstrates how to deploy a Lambda Function that has a layer added to it.
This includes a sample directory structure that comprises the dependencies directory for the python packages and the src directory for the Lambda function business logic.
The Terraform code deploys two Lambda functions. Both Lambda functions attempt to import the mysql-connector-python package.
However, only one of the Lambda functions has the mysql-connector-python layer which allows it to import the package successfully.
This pattern tests both Lambda functions to illustrate the difference when there is a layer added to a Lambda function.
In addition, the Lambda function with the layer lists all files in the /opt directory which allows us to view the files that were uploaded by the layer.
One use case for Lambda layers is to have packages like mysql-connector-python shared across various Lambda functions that require connections to RDS MySQL.
Another use case could be to add certificates as layers that can then be used by Lambda Functions to encrypt a connection to a DB instance via SSL/TLS.
The path to these certificates in the /opt directory can be specified in the various connect commands.
Using layers reduces the size of uploaded deployment archives and makes it faster to deploy your code.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/lambda-layer-terraform

Deploy

terraform initterraform apply --auto-approve


Testing

See the GitHub repo for detailed testing instructions.

Cleanup

terraform destroy --auto-approve

Created by:

Glenn Chia Jin Wee

Glenn Chia Jin Wee

Glenn is an Associate Cloud Architect at AWS based in Singapore