- Install Serverless Framework
- Create a Serverless project
- Create a Lambda Function
- Deploy the Lambda Function
- Test the Lambda Function
- Exclude the node_modules from Lambda Function
- Deploy Lambda Function again
- Create Lambda Layer Serverless Project
- Modify Lambda Function to use Lambda Layer
- Cleanup
AWS Lambda Layers let you keep your deployment package small, which makes development easier. You can avoid errors that can occur when you install and package dependencies with your function code. A Lambda layer is an archive containing additional code, such as libraries, dependencies, or even custom runtimes. When you include a layer in a function, the contents are extracted to the /opt directory in the execution environment. You can include up to five layers per function, which count towards the standard Lambda deployment size limits.
Serverless Framework Open Source lets you develop and deploy serverless applications to AWS. The Serverless Framework CLI is the simplest way to develop infinitely scalable, pay-per-execution serverless applications. Following the AWS best practices, we will handle our application and the layers into two independent Serverless projects called:
- lambda-service (will contain the Node function source code)
- lambda-layer (will contain the lambda layers)
Install AWS Cloud9 Environment using the instructions here.
Inside AWS Cloud9 environment, open a shell terminal and run the following:
npm install -g npm
npm i -g serverless
sls create --help
mkdir serverless
cd serverless
sls create -t aws-nodejs -n lambda-service -p lambda-service
Replace handler.js with:
'use strict';
const moment = require("moment");
const dateNow = moment().format("MMM Do YY");
module.exports.hello = async event => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: dateNow
}),
};
};
Install the moment module.
cd lambda-service
npm init -y
npm i moment --save
Open serverless.yml under lambda-service and replace it with:
service: lambda-service
provider:
name: aws
apiGateway:
shouldStartNameWithService: true
runtime: nodejs12.x
stage: dev
region: us-east-1
profile: default
functions:
hello:
handler: handler.hello
events:
- http:
method: get
path: hello
Inside the lambda-service directory, run the following commands:
sls package
sls deploy
Open the http api gateway endpoint in the browser, and the REST Api will return the current date in the MMM Do YY
format.
Replace the service: lambda-service
with the following code inside serverless.yml file:
service: lambda-service
package:
exclude:
- node_modules/**
sls package
sls deploy
Open the http api gateway endpoint in the browser, and this time you will get internal error
.
Inside the
serverless
directory, run the following commands:
sls create -t aws-nodejs -n lambda-layer -p lambda-layer
Replace the contents of serverless.yml under lambda-layer as:
service: lambda-layer
provider:
name: aws
runtime: nodejs12.x
stage: dev
region: us-east-1
profile : default
layers:
MomentLayer:
path: moment_layer
description: "Moment Dependencies"
Open a shell prompt under lambda-layer directory
mkdir moment_layer && cd moment_layer
npm init -y
npm i moment --save
cd ../
sls package
sls deploy
Note down the AWS urn of the lambda layer that gets created.
Open serverless.yml under lambda-service directory.
Replace it with:
service: lambda-service
package:
exclude:
- node_modules/**
provider:
name: aws
apiGateway:
shouldStartNameWithService: true
runtime: nodejs12.x
stage: dev
region: us-east-1
profile: default
functions:
hello:
handler: handler.hello
events:
- http:
method: get
path: hello
environment:
NODE_PATH: "./:/opt/node_modules"
layers:
- arn:aws:lambda:us-east-1:xxxxxxxxxxx:layer:MomentLayer:1
Replace the ARN of lambda layer created in previous step.
Open shell prompt in lambda-service
directory
sls package
sls deploy
Open the http api gateway endpoint in the browser, and the REST Api will return the current date in the MMM Do YY
format.
Inside the lambda-service
directory, run:
sls remove
Inside the lambda-layer
directory, run:
sls remove
Finally using the AWS Management Console, remove the AWS Cloud9 environment.
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.