Prerequisite
We assume the reader has installed the npm package serverless
globally. Make sure you can run sls
or serverless
at your terminal.
Python Flask
Template Repo
How to test Locally and Deploy
-
yarn
to installserverless-wsgi
andserverless-python-requirements
-
Now if you run
sls wsgi serve
, a flask api server should be up and running. It also provides hot reload for code changes. -
After your api implement are done, you can
sls deploy
to deploy your application. -
You can
sls remove
to undo everything.
Size Reduction for Python Lambdas
You can reduce the size by modifying serverless.yml
.
-
Add
custom.pythonRequirements.noDeploy
. -
By default
serverless
will copy compiled binary from your virtual environment. -
If you add
autopep8
innoDeploy
, make sure to remove that fromrequirements.txt
as well becauseserverless
will make a copy ofrequirements.txt
and copy compiled binary accordingly. -
dockerizePip: true
is necessary for packagePILLOW
because the compiled binary in windows is not compatible with linux. -
Layers! I haven't tried to Flask yet, we may add
noDeploy
in the list once we find suitable lambda layers available in our region:provider: name: aws runtime: python3.8 layers: - arn:aws:lambda:us-east-1:xxxxxxxxxxxxx:layer:xxxxx:mylayer1 - arn:aws:lambda:us-east-1:xxxxxxxxxxxxx:layer:xxxxx:mylayer2
The list of layers available at our region can be found in here.
Nodejs Express (ts)
Template Repo
How to test Locally and Deploy
-
yarn
to installserverless-http
serverless-offline
serverless-plugin-common-excludes
serverless-plugin-typescript-express
-
To run locally we run
yarn start
, which just use traditionalnodemon
andts-node src/app.ts
. -
We run
yarn deploy
to deploy our app using our~/.aws
credentials. -
We run
yarn remove
tosls remove
everything according to cloudformation record.
Size Reduction for Nodejs Lambdas
Compared to python we have much fewer things to modify in serverless.yml
as we don't have such options. The best thing we may try is:
package: patterns: - src/** # include only files from ./src/**/* - "!node_modules/some-package/**" # exclude files from ./node_modules/some-package/**/*
and add layers
in provider option, but I don't have such convenient resource yet.
Manual Bug Fix before Deployment
Before deployment, a manual bug fix must be held on our own. From this thread, we need to modify
node_modules/serverless/bin/serverless.js
which in my case my serverless.js
is held at the following absolute path
C:\Users\user\AppData\Local\Yarn\Data\global\node_modules\serverless\bin\serverless.js
in that file we add the following line right after use strict;
:
require("../../graceful-fs/graceful-fs").gracefulify(require("fs"));