Front-End Web & Mobile
How to use Lambda layers with the Amplify CLI
This post was written by Rene Brandel, Senior Product Manager, AWS
In this guide you will learn how to create, deploy and leverage Lambda layers & the Amplify CLI to reuse code & assets across serverless functions.
AWS Amplify enables mobile & web developers to build full stack serverless apps. The Amplify CLI helps developers to create backend resources through a guided workflow.
Today, we are announcing the general availability of Lambda layer support in Amplify CLI. A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. Lambda layer provides a few major benefits for your full stack serverless codebase:
- Re-use your code! Your Lambda functions can leverage these layers to reuse shared code & assets across functions. A developer’s dream – no more messy code duplication!
- Faster deployments! Iterating on your Lambda function will be significantly faster because it can be independently updated from the layer which usually contains the bulk of large static content!
Amplify CLI provides a guided creation, update and deployment process for Lambda layer designed for NodeJS, and Python.
What we’ll learn:
- Use Amplify CLI to set up a new Lambda layer with a node module
- Add a Lambda layer to a Lambda function
What we’ll build:
- A Lambda layer that packages the Moment.js library (a time utility library)
- A Lambda function that uses this layer to access Moment.js to generate a timestamp as a response
Pre-requisites:
- Install the latest Amplify CLI version
- Open terminal and run
npm install -g @aws-amplify/cli
to update to the latest Amplify CLI.
- Open terminal and run
- Amplify CLI is already configured
- If you haven’t configured the Amplify CLI yet, follow this guide on our documentation page.
1. Setup your environment
First, create your project directory. If you have an existing Amplify project, you can skip to the next section.
Open your Terminal and create a project directory by running the following command.
Next change directory into the newly created project
In the project directory initialize Amplify by running
For this example, accept all of the default values. The selection & output should look something like this.
2. Create your Lambda layer
Now that you’ve got your project setup you can add your first Lambda layer. This is where you’ll place all the shared code & assets for your Lambda functions. To add your first layer run the following command in your Terminal:
Choose Lambda layer (shared code & resource used across functions)
Now you can provide a name for your layer. For this project, select NodeJS (using the space bar) and then follow the remaining defaults.
Next, you can configure who has access to your Lambda layer in addition to the current account. If you don’t want to set up any additional permissions, you can hit Enter to skip.
The layer is setup! In the next step you’ll add a library to your layer.
3. Add node modules to your lambda layer
Next, change into the new directory:
For NodeJS projects, Amplify CLI automatically places a package.json file in the nodejs folder. This allows you to add node_modules to your layer by running npm install <your modules>
. For this project, install the moment package and leverage in our Lambda function.
Run the following command to install moment:
Then, change back into the top level directory.
4. Create a Lambda function and use it with your layer
Now that you’ve got your Lambda layer created, let’s add it to a Lambda function to unlock all the benefits of Lambda layers. To create a new Lambda function run amplify add function. Make sure to create a NodeJS – Hello World function.
Make sure to answer Yes to configuring a Lambda layer and use the space bar to select the newly created layer.
When prompted to edit the local Lambda function now, choose Yes.
Now, edit the function code to use the moment node module. Replace the Lambda function file with the content below:
You can still import or require node_modules in the same way as if they are bundled directly within your function.
This level of abstraction makes it super easy for you to migrate your existing functions to start using Lambda layers, since there aren’t any code changes.
Now let’s wrap this up and go back to your Terminal to complete the process of creating a Lambda function.
5. Push your function and layer to the cloud & test them!
Now that you’ve setup your Lambda function and layer, you can push it to the cloud. To do that, run:
amplify push -y
Once the Lambda layer & function are deployed, you can test it inline within the Lambda Console by running amplify console function
. Select your function in the Lambda Console and run a test!
Note:
amplify mock function <function-name>
will not work for functions that use layers.
6. ? Success
After running Test, you can see the response contains the timestamp generated by moment’s .format()
function.
Next steps
This is how easy it is to start using Lambda layers in your full stack serverless application with Amplify CLI! Review the Lambda layers capability in full by checking out the documentation.
If you have any feedback or enhancement requests, please create an issue in the Github repository.