This project provides an example of how to create and deploy an AWS Lambda Layer using the AWS Cloud Development Kit (CDK) in Python.
AWS Lambda Layers allow you to share libraries and dependencies across multiple Lambda functions. This example demonstrates how to:
- Define a Lambda Layer using AWS CDK (Python).
- Package and deploy the layer to AWS.
- Use the layer in a Lambda function.
aws-lambda-layer-example/
βββ lambda_layer/ # Directory for Lambda layer dependencies
βββ cdk/ # AWS CDK Infrastructure
βββ requirements.txt # Python dependencies for CDK
βββ cdk.json # CDK Configuration
βββ README.md # This documentation
βββ LICENSE
- AWS CDK installed globally:
npm install -g aws-cdk
- AWS CLI configured with credentials:
aws configure
- Python 3.8+ installed
- Node.js 16+ (for AWS CDK)
- Virtual Environment setup (optional but recommended)
Clone the repository:
git clone https://github.com/your-repo/aws-lambda-layer-example.git
cd aws-lambda-layer-exampleCreate a virtual environment and install dependencies:
python3 -m venv .venv
source .venv/bin/activate # On Windows use .venv\Scripts\activate
pip install -r requirements.txtInstall AWS CDK dependencies:
cdk bootstrapThis command generates a CloudFormation template for your AWS infrastructure.
cdk synthDeploy the stack that creates the Lambda Layer:
cdk deploy LambdaLayerStackOnce deployed, note the Layer ARN from the output.
Deploy the Lambda function using the Layer ARN:
cdk deploy LambdaFunctionStack --parameters LayerArn="arn:aws:lambda:region:account-id:layer:LayerName:Version"Alternatively, modify app.py to automatically retrieve the layer ARN from LambdaLayerStack.
If you make changes to the layer, update it with:
cdk deploy LambdaLayerStackThen, update functions that use it:
cdk deploy LambdaFunctionStackaws lambda invoke --function-name MyLambdaFunction output.json
cat output.jsonaws logs tail /aws/lambda/MyLambdaFunction --followTo delete the deployed resources and avoid AWS charges:
cdk destroyThis project is licensed under the MIT License. See the LICENSE file for details.
For issues or contributions:
- Open an issue or pull request in this repository.
- Visit AWS Lambda Documentation.
- Contact us at info@zerodotfive.com.