☁️ Using external libraries in your Python AWS Lambda in AWS CDK

AWS CDK Logo
AWS CDK Logo

Are you including external libraries in your Python Lambda with AWS CDK? You’ll be looking to install your dependencies from your requirements.txt. How do you make sure these external libraries are available at runtime for your execution? By using @aws-cdk/AWS-lambda-python.

We’ll walk through what the requirements.txt is, what the CDK construct AWS-lambda-python is and do a complete 5 step example on how you can use it at the end of this post.

This level 100 post is part of the University Series of the NBTL blog.

AWS CDK Logo
AWS CDK enables you to build your cloud application without leaving your IDE. You can write your runtime code and define your AWS resources.

What is the requirements.txt?

If this isn’t your first Python project, you’ll have worked with and seen the requirements.txt. You use the file for specifying the required python packages for your project. It’s a list of dependencies the executable will need. You’ll find the requirements file at the root of your project. In the case of a Lambda, you’ll find the folder next to the handler.

When you open the file, you can find something like this:

requests==x.xx.x
boto3==x.xx.x

All of the listed packages will have a name and a version number. Why do the libraries need a version number, you might ask? These version numbers are required because you wouldn’t want your library to automatically update with every deployment and by that accidentally introducing a breaking change. By keeping track of the used numbers during development, you’ll prevent unexpected changes.

How does @aws-cdk/aws-lambda-python work?

The CDK construct is currently experimental but solves the issue of the external libraries in Lambda. It builds the Lambda function in a Docker container and pushes a build-out to Lambda. Because of this, you will need to have Docker installed.

import * as lambda from "@aws-cdk/aws-lambda"; 
import { PythonFunction } from "@aws-cdk/aws-lambda-python";  

new PythonFunction(this, 'MyFunction', {   
entry: '/path/to/my/function', // required   
index: 'my_index.py', // optional, defaults to 'index.py'   
handler: 'my_exported_func', // optional, defaults to 'handler'   runtime: lambda.Runtime.PYTHON_3_6, // optional, defaults to lambda.Runtime.PYTHON_3_7 
});

The CDK construct automatically looks for the requirements.txt in the same folder as the specified entry in terms of the requirements. During the CDK deployment, all the dependencies will be installed automatically.

Full example

To use the construct, you will need to have CDK installed on your machine. You can find the complete overview of pre-requisites here, but I’m assuming you have CDK installed for now.

  1. Create a new folder and initialise a new CDK project:
mkdir lambda-with-dependencies && cd lambda-with-dependencies
cdk init sample-app --language typescript

2. Install the experimental CDK construct

npm i -s @aws-cdk/aws-lambda-python

3. Import the CDK construct at the top of your new ./lib/project-name.ts

import { PythonFunction } from "@aws-cdk/aws-lambda-python";

4. Create a new object with the CDK construct pointing towards your Lambda code. The attributes are declared as the following:

Entry: The folder location where both your Python file and requirements.txt are.

Index: The actual python file you wish to execute with the Lambda

Handler: The function in your python file that gets executed

Runtime: The python version you wish to use.

const function = new PythonFunction(this, 'MyFunction', {
entry: './lambda/lambda-function', // required
index: 'python-file.py', // optional, defaults to 'index.py'
handler: 'handler', // optional, defaults to 'handler'
runtime: lambda.Runtime.PYTHON_3_8, // optional, defaults to lambda.Runtime.PYTHON_3_7
});

5. Execute synth or deploy to test and/or deploy your resources

cdk synth
cdk deploy

Conclusion

With the CDK deploy successfully deploying your resources to your AWS account, you will now have deployed a Lambda with external libraries as declared in your requirements.txt

This post is an example of our University Series. Other posts in the series include:

By Mart

Tutorials at nbtl.blog

2 comments

Leave a comment

Your email address will not be published. Required fields are marked *