A simple project to test AWS Lambda functions & Layers written in TypeScript
- Serverless framework
npm install -g serverless
Open a terminal and run these commands:
cd layers
# enter the layers foldernpm run build:all
# install all dependencies and transpile TypeScript code into JScd ../test-layer
# enter the lambda function foldernpm run first-start
# install dependencies and run the lambda function locally using serverless offline plugin
layers
the folder that contains all layers:express-pg-moment
an example layer that bundles 3rd parties modules- ExpressJs
- validator
- cors
- MomentJs
- PostgreSQL client (pg)
- Serverless HTTP
- ExpressJs
logging
an example of a custom layer that logs messages
test-layer
a simple lambda function that uses the two layers listed above
To test the Lambda function locally just type:
cd test-layer
- (
npm install
if you have just cloned the repo) npm run start
- it simply calls
sls offline start
- it simply calls
To deploy the lambda that uses the layers using serverless just type:
cd test-layer
npm run deploy -- --stage dev
- it simply calls
npm install
sls deploy
- it simply calls
To deploy all layers using serverless just type:
cd layers
npm run deploy -- --stage dev
it builds all layers, and it deploys them- it does the following for every layer:
npm run install
npm run build
- it deploys all the layers
sls deploy
- it does the following for every layer:
Layer must be exported wit TitleCase and LayerExport suffix: LoggingLayerExport
Layer name must be written with TitleCase and LambdaLayer suffix: LoggingLambdaLayer
in lambda layers serverless.yml
:
sercice: layers
...
resources:
Outputs:
LoggingLayerExport:
Value:
Ref: LoggingLambdaLayer
Export:
Name: LoggingLambdaLayer-${self:provider.stage}
in lambda function serverless.yml
:
functions:
functionName:
layers:
- ${cf:layers-${self:provider.stage}.LoggingLayerExport}
where layers
is the service name and LoggingLayerExport
is the resource name
Function layers must be added in package.json
as devDependencies
, in this way won't be deployed.
To prevent errors, custom dependencies must be excluded in the serverless.yml
file
in package.json
:
{
"devDependencies": {
"express": "^4.17.1",
"logging": "file:../layers/logging/dist/nodejs/node_modules/logging"
}
}
in serverless.yml
:
package:
exclude:
- node_modules/logging
please note the difference from 3rd parties dependency (express) and custom dependency (logging)
- ExpressJS is just added to devDependencies
- Loggin is added to devDependencies and excluded in the package section
As the AWS Layers doc, a NodeJs layer is deployed on nodejs/node_modules
folder.
It's better to compile the TypeScript layer in Javascript and deploy only the compiled version.
This is why I've chosen the following folder structure for a layer written in TypeScript:
logging
the root folderdist
contains the compiled code (JavaScript) and follows the folder structure specified in the AWS documentationnodejs
node_modules
logging
- LAYER COMPILED CODE
src
contains the source folder (TypeScript)node_modules
contains dependencies to compile and manage the deploymentpackage.jon
contains scripts to compile and deploy the layertsconfig.json
manage the compilation process
Only the content of the dist
folder will be deployed, as configured in the path
field in serverless.yml
:
layers:
logging:
path: logging/dist
description: Layer with a simple Logger
compatibleRuntimes:
- nodejs12.x
- Integrating Lambda Layers into your Node.js Lambdas using pre-configured templates
- NodeJS Runtime Environment with AWS Lambda Layers
- Lambda Layers & Node & require the layer code & sls invoke local
- How to deploy multiple micro-services under one API domain with Serverless
- How to use multiple runtimes in a single serverless microservice
- When to use Lambda Layers
- Get Started With AWS, Serverless, and TypeScript
- How I package TypeScript lambdas for AWS
- Part 2 — Create Lambda Layers with Serverless Framework and Offline support
- StackOverflow: Can I import typescript types from a Lambda Layer?
- Lambda Layers — Tips & Tricks
- Sharing Code among Lambdas using Lambda Layers