This example shows how to use the serverless framework to run loopback4 (with express) in AWS Lambda.
A RDS (MySQL) database is created, along with all necessary virtual AWS infrastructure (VPC, subnets, DBSubnetGroup) to connect a lambda function running loopback4 to the MySQL database. The MySQL connection parameters are retrieved from lambda environment variables.
The sample loopback4 application provided was created by following the process below:
-
Create an Express Application with LoopBack REST API by following this tutorial. We followed the shortcut by running the command
lb4 example express-composition
. -
Remove the
file
parameter from the memory datasource to avoid lambda errors. -
Create the following files required to deploying it all to AWS Lambda using the serverless framework:
-
src/lambda-wrapper.js
: this file usesserverless-http
to wrap the loopback4 application via express. -
serverless.yml
: our serverless service definition. Here is where we create all AWS resources (including Lambda, and API Gateway).
-
-
Update your
tsconfig.json
file to include"allowJs": true
in yourcompilerOptions
section to ensire thesrc/lambda-wrapper.js
file is included in the loopback4 build. -
Install the
serverless-http
module and its dependencies with commandnpm install serverless-http
. -
Install the
serverless
module and its dependencies with commandnpm install -D serverless
. -
As we planed to troubleshoot locally, we installed the
serverless-offline
module and its dependencies with commandnpm install -D serverless-offline
. -
In order to ensure your loopback4 application is built prior to deploying it to AWS Lambda or running it locally we included the following scripts in the
package.json
file:
"presls-deploy": "npm run build",
"sls-deploy": "serverless deploy --verbose",
"sls-cleanup": "serverless remove --verbose",
"presls-offline": "npm run build",
"sls-offline": "serverless offline",
Please note that serverless-http
also supports other web frameworks. The loopback support indicated is for @loopback-rest
. Please feel free to contribute your examples of using loopback4 with other web frameworks under the examples
folder.
The following steps can be used to customize this sample to your needs and then deploy:
-
Create your own loopback models, datasources, repositories, and controllers.
-
Deploy your project to AWS using the command
npm run sls-deploy
-
From the serverless
Stack Outputs
, retrieveLoopbackApiExplorer
to access the loopback4 API explorer (it should look something likehttps://API_GATEWAY_ID.execute-api.AWS_REGION.amazonaws.com/SERVERLESS_STAGE/api/explorer/
). You should end up with an URL similar tohttps://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/dev/api/explorer/
. -
From the lopback4 API explorer download the
openapi.json
file and import it into Postman. After importing, make sure to edit the imported collection and change thebaseUrl
variable to the value of the serverlessLoopbackPostman
Stack Outputs
. This will make sure Postman works with your serverless API Gateway rather than the loopback4 only path.
Once you are done with the sample environment, avoid unnecessary AWS charges by removing your serverless deployment with the command npm run sls-cleanup
.
While the serverless deployment works fine, invoking the API through API explorer never reaches API Gateway as CloudFront responds with Code 403:
Response body
{
"message": "Forbidden"
}
Response headers
content-length: 24
content-type: application/json
date: Sun14 Jun 2020 14:14:09 GMT via: 1.1 0dfe6f02dbba7c39906cae47653ae6b3.cloudfront.net (CloudFront)
x-amz-apigw-id: OOYDwFjZiYcFSAA=
x-amz-cf-id: 3SvwvE27qb1KIrS34rgRRwet-QOkfZyFeJOIMUbHMgi6dA5-BgBEeg==
x-amz-cf-pop: DEN50-C2
x-amzn-errortype: ForbiddenException
x-amzn-requestid: 31a0c024-6ffe-4c3c-b9d9-adfca9add4b0
x-cache: Error from cloudfront
x-firefox-spdy: h2
MIT © BotBitsSM