Using AWS Lambda Layers for Importing Third-Party Libraries: JavaScript & Python Examples

Using AWS Lambda Layers for Importing Third-Party Libraries: JavaScript & Python Examples

In this blog post, we'll explore how to use AWS Lambda layers to share common libraries across multiple Lambda functions. We'll focus on two popular libraries as examples: Axios for JavaScript and Requests for Python.

What Are Lambda Layers?

Lambda layers provide a convenient way to manage and share code, libraries, custom runtimes, and other function files for AWS Lambda functions. By separating shared dependencies from your main function code, you can keep your deployment packages smaller and more maintainable.

Layers can be used simultaneously by multiple functions, allowing you to maintain shared dependencies independently. You can create your own layers or use layers published by AWS and other AWS customers. Layers can be versioned, giving you greater control over the deployment process.

Adding the Axios Library (JavaScript)

To add the Requests library to a Lambda layer, follow these steps:

  1. Create a new directory on your local machine:
mkdir lambda-layer
cd lambda-layer
  1. Install the Axios library inside this directory:
npm init -y
npm install axios
  1. Create a ZIP archive containing the node_modules directory:
zip -r lambda-layer.zip node_modules
  1. Upload the ZIP archive as a new Lambda layer:
aws lambda publish-layer-version --layer-name axios-layer --zip-file fileb://lambda-layer.zip --compatible-runtimes nodejs14.x
  1. Add the layer to your Lambda function using the Layer ARN:
aws lambda update-function-configuration --function-name your-function-name --layers arn:aws:lambda:your-region:your-account-id:layer:axios-layer:1
  1. Now, you can use the Requests library inside your Lambda function without including it in your deployment package:
    import requests
const axios = require('axios');

exports.handler = async (event) => {
  try {
    const response = await axios.get('https://api.example.com/data');
    // Process the response and perform your desired operations
  } catch (error) {
    console.error(error);
  }
};

Adding the Requests Library (Python)

To add the Requests library to a Lambda layer, follow these steps:

  1. Create a new directory on your local machine:
mkdir lambda-layer-python
cd lambda-layer-python
  1. Create a 'python' subdirectory and install the Requests library inside it:
mkdir python
pip install requests -t python
  1. Create a ZIP archive containing the 'python' directory:
zip -r lambda-layer-python.zip python
  1. Upload the ZIP archive as a new Lambda layer:
aws lambda publish-layer-version --layer-name requests-layer --zip-file fileb://lambda-layer-python.zip --compatible-runtimes python3.8
  1. Add the layer to your Lambda function using the Layer ARN:
aws lambda update-function-configuration --function-name your-function-name --layers arn:aws:lambda:your-region:your-account-id:layer:requests-layer:1
  1. Now, you can use the Requests library inside your Lambda function without including it in your deployment package:
    import requests
def lambda_handler(event, context):
    try:
        response = requests.get('https://api.example.com/data')
        # Process the response and perform your desired operations
    except Exception as error:
        print(f"Error: {error}")

Conclusion

AWS Lambda layers provide a powerful way to share and manage dependencies like Axios (JavaScript) and Requests (Python) across multiple Lambda functions. By separating these shared libraries from your main function code, you can keep your deployment packages smaller and more maintainable. Using Lambda layers, you can easily update shared libraries without affecting the main function code, providing better control over the deployment process.