1. Prerequisite
We assume the reader has installed the npm package serverless globally. Make sure you can run sls or serverless at your terminal.
2. Python Flask
2.1. Template Repo
2.2. How to test Locally and Deploy
-
yarnto installserverless-wsgiandserverless-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 deployto deploy your application. -
You can
sls removeto undo everything.
2.3. Size Reduction for Python Lambdas
You can reduce the size by modifying serverless.yml.
-
Add
custom.pythonRequirements.noDeploy. -
By default
serverlesswill copy compiled binary from your virtual environment. -
If you add
autopep8innoDeploy, make sure to remove that fromrequirements.txtas well becauseserverlesswill make a copy ofrequirements.txtand copy compiled binary accordingly. -
dockerizePip: trueis necessary for packagePILLOWbecause the compiled binary in windows is not compatible with linux. -
Layers! I haven't tried to Flask yet, we may add
noDeployin 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:mylayer2The list of layers available at our region can be found in here.
3. Nodejs Express (ts)
3.1. Template Repo
3.2. How to test Locally and Deploy
-
yarnto installserverless-httpserverless-offlineserverless-plugin-common-excludesserverless-plugin-typescript-express
-
To run locally we run
yarn start, which just use traditionalnodemonandts-node src/app.ts. -
We run
yarn deployto deploy our app using our~/.awscredentials. -
We run
yarn removetosls removeeverything according to cloudformation record.
3.3. 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.
3.4. 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"));













