Deploying AWS Lambda layers with Python
Learn to easily deploy a Lambda Layer via AWS CDK and Python.

AWS Lambda is an event-driven solution that enables developers to run code in a serverless manner. There is no need to provision or manage infrastructure to run your code.
With AWS Lambda, you can automatically respond to code execution requests at any scale. The service is ideal for file processing, stream processing, web applications, mobile backends and even machine learning use cases.
To create an AWS Lambda function, first you need to package your function code into a deployment package. As of today, Lambda supports two types of deployment packages: container images and .zip file archives.
In many cases, when creating our Lambda functions, we need to package additional libraries and dependencies that are required by our application to work as intended. For this purpose, the AWS Lambda service allows developers to create a Lambda Layer.
A Lambda layer is package that can contain additional dependencies or data. As per the AWS website, a layer can contain libraries, a custom runtime, data, or configuration files.
In this post, we will explore how to deploy AWS Lambda layers using the Cloud Development Kit (CDK). We will use Python as the programming language for the AWS Lambda function and we will take advantage of the AWS CDK to easily deploy our AWS service.
The Cloud Development Kit
AWS CDK is an open source software development framework to define cloud application resources via familiar programming languages. It provides high-level components (constructs) that preconfigure cloud resources with proven defaults, enabling the provision of resources in a safe, repeatable manner through AWS CloudFormation.
Installing AWS CDK
Prerequisites
To set up AWS CDK properly, we will need:
- An AWS account and credentials.
- Node.js.
- Python 3.6 or later.
- Python package installer, pip, and virtual environment manager, virtualenv.
Install the AWS CDK using the following Node Package Manager command.
npm install -g aws-cdkTo verify the correct installation and print the version number of the AWS CDK, run the following command:
cdk --versionInitialize AWS CDK
Let’s start using AWS CDK by creating the App.
cd ~
mkdir aws-lambda-layer-cdk
cd aws-lambda-layer-cdkcdk init app --language pythonImportant: You need an empty directory in order to initialize cdk and make sure you have provided your AWS credentials via the AWS CLI.
The name of the project folder will be used by cdk init to name various elements of the project. This includes: classes, subfolders, and files.
Another step after initializing the project is to activate the project’s virtual environment.
source .venv/bin/activateThe template includes a batch file,
source.bat, that allows the same command to be used on Windows. For Windows execute.venv\Scripts\activate.bat.
The next step is to install the app’s standard dependencies:
python -m pip install -r requirements.txtAs we need to create a Lambda function, we can proceed to install the AWS Construct Library module for AWS Lambda by executing:
python -m pip install aws-cdk.aws-lambdaWith this steps completed, we have installed and prepared the fundamental building blocks for our CDK application.
Creating the project structure
Now it is time to create our folder structure. I prefer to rename the directory and content automatically created by CDK. Make sure you are in the main directory:
cd ~/aws-lambda-layer-cdkFind the directory by the same name aws-lambda-layer-cdk and rename it to LambdaStack.
Modify the file aws_lambda_layer_cdk_stack.py and add the code below:


