5 Ways to Share Code Between Lambda Functions

How to Share Code Between Lambda Functions?

Chameera Dulanga
Bits and Pieces

--

In recent years, serverless architecture has radically transformed how we develop and deploy applications. Today, nearly every organization utilizing cloud services has at least one serverless deployment.

Source: https://www.datadoghq.com/state-of-serverless-2022/

This paradigm shift offers numerous benefits, including reduced operational overhead, cost optimization, and improved scalability. However, managing code in a serverless environment can present unique challenges, particularly when sharing code across multiple Lambda functions.

To help you navigate this issue, we’ve compiled five ways to share code between Lambda functions.

1. Lambda Layers

Lambda Layers revolutionize code sharing in AWS Lambda by enabling developers to separate common dependencies from their function code. All you have to do is create a layer and define it in a serverless.yml file. Once deployed, a Lambda layer can be referenced in various Lambda functions using its ARN, allowing up to five layers per function.

How to share code with Lambda Layers?

You can use the AWS console to create and deploy lambda layers directly. However, the recommended approach for production applications is to use the Serverless Framework or AWS SAM CLI (serverless application model).

Prerequisites

Step 01 — Create serverless service

Navigate to the backend folder and run the following command to create a service named layers.

serverless create --template aws-nodejs --path layers

Step 02: Creating a Lambda Layer

Create a folder named logging within the layers directory, and update the layers/serverless.yml file with the code below.

service: layers
provider:
name: aws
runtime: nodejs12.x
layers:
logging:
path: logging

Note: Since we are using Node.js as the runtime, we need to maintain the folder structure as below to ensure other Lambda functions can access the code within the logging layer.

logging/
nodejs/
node_modules/
logging/
index.js

Step 04 — Deploying the lambda layer

Run the following command inside the layers to deploy the layer.

serverless deploy --stage dev

Step 05 — Using the layer in other lambdas

Now, you can add this layer to other services using the arn. All you need to do is update the serverless.yml of other services like below:

service: todos
provider:
name: aws
runtime: nodejs12.x
functions:
todos:
handler: handler.todos
layers:
- arn:aws:lambda:us-east-1:885121665536:layer:logging:9
events:
- http:
path: todos
method: get

You can find a complete guide with detailed instructions on creating AWS Lambda Layers here.

2. Bit Components

Bit components are an innovation from Bit.dev that simplifies code sharing and maintenance across projects. For applications, libraries, UI components, or Lambda functions, Bit Components offer an ideal solution for sharing code among various projects.

Here are some core features of a bit component.

  • Share your code with any project or any team.
  • Update one component without messing up others.
  • A central hub (Bit cloud) for all your components.
  • Build and test your components on their own to make sure they’re solid.

Figure: A Bit Scope that showcases code sharing in AWS Lambda

As shown above, the Bit scope has a format-date utility function that's being used across multiple places. For instance, it's being used in the React Component - time-display and the AWS Lambda Function - lambda-function.

Bit is able to understand that components that are being used up the component tree and is able to smartly sync changes up the tree when it detects a change. It does this by using its CI Server — Ripple CI to automatically propagate changes up the tree.

How to Share Code with Bit?

Prerequisites

Step 01: Create and Export a Bit Component

First, you need to create and export a component into Bit cloud. Bit Cloud is a component repository similar to GitHub, where you push your components and share them with others instead of using raw code.

You can follow this guide to create a Bit component from scratch. For this example, I will use an already created Node component in Bit.cloud.

Step 02: Using the Component in a Lambda Function

Import the component in the handler code of AWS Lambda. The below example shows how you can use it in an AWS Lambda app.

You can find a complete guide with detailed instructions on sharing code between AWS Lambda using Bit here.

3. NPM Packages

NPM modules are a highly effective way to share code across multiple AWS Lambda functions. This approach particularly benefits Node.js environments, where NPM is the cornerstone for managing packages and dependencies. With NPM modules, developers can easily reuse utility functions, custom middleware, or configuration settings across various Lambda functions.

How to Share Code with NPM Modules?

The process typically involves creating a private NPM module, publishing it, and including it in your Lambda functions.

Prerequisites

  • Node.js and npm installed.
  • Access to an npm registry (public or private).

Step 01 — Create and Publish an NPM Module

Navigate to your project directory and create a new NPM package:

mkdir my-shared-module
cd my-shared-module
npm init -y

Then, create a index.js file and add your shared functionality:

// index.js
module.exports = {
logEvent: function(event) {
console.log('Event:', JSON.stringify(event, null, 2));
}
};

Publish this package to npm (make sure to update the version in package.json for each new release):

npm publish

Step 02 — Include the NPM Module in Your Lambda Functions

Install the shared module in your Lambda function’s directory.

npm install [my-shared-module]

Then, import and use the shared code in your Lambda function:

// handler.js
const shared = require('my-shared-module');

exports.handler = async (event) => {
shared.logEvent(event);
// Lambda function logic
};

Finally, use the AWS CLI to deploy your Lambda function with the new dependency.

4. AWS S3

Using Amazon S3 to share code among AWS Lambda functions offers a scalable solution for handling larger dependencies or integrating external build processes. This method involves storing your shared code or libraries in an S3 bucket and accessing them from your Lambda functions.

How to Share Code with an S3 Bucket?

Sharing code via an S3 bucket involves uploading the shared code to S3 and then referencing it in your Lambda functions. This method can be integrated into your CI/CD pipeline for automated deployment.

Prerequisites

  • An AWS account with access to S3 and Lambda services.
  • AWS CLI installed and configured.

Step 01 — Prepare Your Shared Code

Compress your code into a ZIP file to prepare it for upload.

zip -r shared-code.zip path/to/your/code

Step 02 — Upload to an S3 Bucket

Use the AWS CLI to upload your ZIP file to the S3 bucket:

aws s3 cp shared-code.zip s3://your-s3-bucket-name/

Step 03 — Reference in Lambda Functions

Modify your Lambda function to download and extract the shared code from S3 at runtime or during the deployment phase.

import boto3
import zipfile
import os

s3_client = boto3.client('s3')
s3_client.download_file('your-s3-bucket-name', 'shared-code.zip', '/tmp/shared-code.zip')

with zipfile.ZipFile('/tmp/shared-code.zip', 'r') as zip_ref:
zip_ref.extractall('/tmp')

Once extracted, the shared code can be used like any other local file or library in your Lambda function.

5. Microservices and API Endpoints

Implementing Microservices and API Endpoints is a dynamic approach to share functionality among AWS Lambda functions. This method involves encapsulating common code into separate Lambda functions, which are exposed as APIs through Amazon API Gateway. Other Lambda functions can then invoke these APIs to utilize shared functionalities.

How to Share Code with Microservices and API Endpoints?

Prerequisites

  • Familiarity with AWS Lambda and Amazon API Gateway.
  • AWS CLI or AWS Management Console access.

Step 01 — Create a Microservice Lambda Function

Create a Lambda function that contains the shared code or logic. This function will act as your microservice.

exports.handler = async (event) => {
const sharedFunctionalityResult = performSharedTask(event);
return sharedFunctionalityResult;
};

function performSharedTask(input) {
// Shared logicreturn `Processed: ${input}`;
}

Step 02 — Set Up API Gateway

Set up an API Gateway to expose your Lambda function as an HTTP endpoint. Then, deploy your API to make it accessible over the internet.

Step 03 — Invoke from Other Lambda Functions

Other Lambda functions can now invoke this API to use the shared functionality.

const axios = require('axios');

exports.otherLambdaHandler = async (event) => {
const response = await axios.get('https://api-endpoint-url/resource');
// Use the response from the microservice
};

Conclusion

This article explored 5 effective strategies for sharing code between AWS Lambda functions, each serving distinct scenarios and architectural needs. For example, NPM modules are suitable for Node.js environments, while S3 buckets are suitable for handling larger dependencies. Similarly, specialized tools like Bit combine the best of all to provide an excellent framework for sharing and managing codes, libraries, and even UI components.

I invite you to try out these approaches in your next serverless applications to witness the difference firsthand.

Thank you for reading.

--

--