This example shows how to use the serverless framework to run loopback3 (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 loopback to the MySQL database. The MySQL connection parameters are retrieved from lambda environment variables.
Check out this article for a more in-depth explanation of this sample.
The sample loopback application provided was created by following the process below:
- Using the command
npm run lb
and selecting:
- What kind of application do you have in mind?
api-server (A LoopBack API server with local User auth)
-
Adding a new MySQL database to
lib/server/datasources.json
. -
Adding the CoffeeShop model and initializing it with data:
lib/common/coffee-shop.json
: CoffeeShop model definitionlib/server/model-config.json
: add CoffeeShop model so it can be loadedlib/server/boot/create-sample-models.js
: initialize CoffeeShop model with data
The following steps can be used to customize this sample to your needs and then deploy:
-
Create your own loopback models with the command
npm run lb-model
-
The RDS
mySqlDb
inserverless.yml
is configured to be as low cost as possible (not suitable for production!) so feel free to customize it to your needs. -
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/
.
Note: An error might be observed the first time lambda runs after (re-)creating the RDS database as the CoffeeTable model table might not be initialized before your API is invoked. This error would look something like:
{
"error": {
"statusCode": 500,
"name": "Error",
"message": "ER_NO_SUCH_TABLE: Table 'MY_TEST_DB.CoffeeShop' doesn't exist",
"code": "ER_NO_SUCH_TABLE",
"errno": 1146,
"sqlMessage": "Table 'MY_TEST_DB.CoffeeShop' doesn't exist",
"sqlState": "42S02",
"index": 0,
"sql": "SELECT count(*) as \"cnt\" FROM `CoffeeShop` ",
"stack": "Error: ER_NO_SUCH_TABLE: Table 'MY_TEST_DB.CoffeeShop' doesn't exist\n at ..."
}
}
Retry after a few seconds and it all should work.
Once you are done with the sample environment, avoid unnecessary AWS charges by removing your serverless deployment with the command npm run sls-cleanup
.
If you run into a cleanup error similar to the one below, you will need to manually remove the CloudFormation stack by going to: https://console.aws.amazon.com/cloudformation or using the aws-cli.
Serverless Error ---------------------------------------
An error occurred: mySubnet2 - The subnet 'subnet-077e0f72824fe5dd3' has dependencies and cannot be deleted. (Service: AmazonEC2; Status Code: 400; Error Code: DependencyViolation; Request ID: XXX).
MIT © BotBitsSM