You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](http://www.serverless.com)<!-- Skipping tests for now [](https://travis-ci.org/botbits/serverless-loopback) -->[](https://raw.githubusercontent.com/botbits/serverless-loopback/master/LICENSE)<!-- Codacy does not support any language in this project. [](https://www.codacy.com/app/marcelobern/serverless-loopback?utm_source=github.com&utm_medium=referral&utm_content=botbits/serverless-loopback&utm_campaign=Badge_Grade) -->
A sample [loopback](https://loopback.io/) application deployed to AWS Lambda using [serverless](https://serverless.com/).
5
8
6
-
7
9
## Overview
8
10
9
11
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.
10
12
<!--
11
13
For ease of management RDS (MySQL) username/password can be retrieved from [AWS SSM Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-paramstore.html) and used in `serverless.yml` [see section *Handling Secrets for Small Teams & Projects*]](https://serverless.com/blog/serverless-secrets-api-keys/). -->
12
14
15
+
Check out [this article](https://medium.com/smac-4u/serverless-loopback-9ff0d6fa539d) for a more in-depth explanation of this sample.
The sample loopback application provided was created by following the process below:
28
30
29
-
1. Using the command `npm run lb` and selecting:
31
+
1.Using the command `npm run lb` and selecting:
30
32
31
-
- What kind of application do you have in mind? `api-server (A LoopBack API server with local User auth)`
33
+
-What kind of application do you have in mind? `api-server (A LoopBack API server with local User auth)`
32
34
33
-
2. Adding a new MySQL database to `lib/server/datasources.json`.
35
+
2.Adding a new MySQL database to `lib/server/datasources.json`.
34
36
35
-
3. Adding the CoffeeShop model and initializing it with data:
36
-
37
-
-`lib/common/coffee-shop.json`: CoffeeShop model definition
38
-
-`lib/server/model-config.json`: add CoffeeShop model so it can be loaded
39
-
-`lib/server/boot/create-sample-models.js`: initialize CoffeeShop model with data
37
+
3. Adding the CoffeeShop model and initializing it with data:
40
38
39
+
-`lib/common/coffee-shop.json`: CoffeeShop model definition
40
+
-`lib/server/model-config.json`: add CoffeeShop model so it can be loaded
41
+
-`lib/server/boot/create-sample-models.js`: initialize CoffeeShop model with data
41
42
42
43
## Customizing & Deploying This Sample
43
44
44
45
The following steps can be used to customize this sample to your needs and then deploy:
45
46
46
47
<!-- 0. (*optional*) For ease of management add MySQL username/password to the AWS SSM Parameter Store (using [AWS Console(https://docs.aws.amazon.com/systems-manager/latest/userguide/param-create-console.html)] or [AWS CLI(https://docs.aws.amazon.com/systems-manager/latest/userguide/param-create-cli.html)]).
47
48
-->
48
-
1. Create your own loopback models with the command `npm run lb-model`
49
+
1. Create your own loopback models with the command `npm run lb-model`
50
+
51
+
2. The RDS `mySqlDb` in `serverless.yml` is configured to be as low cost as possible (not suitable for production!) so feel free to [customize it to your needs](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-rds-database-instance.html).
49
52
50
-
2. Deploy your project to AWS using the command `npm run sls-deploy`
53
+
3. Deploy your project to AWS using the command `npm run sls-deploy`
51
54
52
-
3. From the serverless `Stack Outputs`, retrieve `ServiceEndpoint` (it should look something like `https://API_GATEWAY_ID.execute-api.AWS_REGION.amazonaws.com/SERVERLESS_STAGE`)
55
+
4. From the serverless `Stack Outputs`, retrieve `ServiceEndpoint` (it should look something like `https://API_GATEWAY_ID.execute-api.AWS_REGION.amazonaws.com/SERVERLESS_STAGE`)
53
56
54
-
4. Access the loopback API explorer by adding `/api/explorer/` to the `ServiceEndpoint` retrieved in the previous step. You should end up with an URL similar to `https://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/dev/api/explorer/`
57
+
5. Access the loopback API explorer by adding `/api/explorer/` to the `ServiceEndpoint` retrieved in the previous step. You should end up with an URL similar to `https://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/dev/api/explorer/`
55
58
56
-
*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:
59
+
*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:
57
60
58
61
```json
59
62
{
@@ -67,27 +70,25 @@ The following steps can be used to customize this sample to your needs and then
67
70
"sqlState": "42S02",
68
71
"index": 0,
69
72
"sql": "SELECT count(*) as \"cnt\" FROM `CoffeeShop` ",
70
-
"stack": "Error: ER_NO_SUCH_TABLE: Table 'MY_TEST_DB.CoffeeShop' doesn't exist\n at Query.Sequence._packetToError (/var/task/node_modules/mysql/lib/protocol/sequences/Sequence.js:47:14)\n at Query.ErrorPacket (/var/task/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)\n at Protocol._parsePacket (/var/task/node_modules/mysql/lib/protocol/Protocol.js:278:23)\n at Parser.write (/var/task/node_modules/mysql/lib/protocol/Parser.js:76:12)\n at Protocol.write (/var/task/node_modules/mysql/lib/protocol/Protocol.js:38:16)\n at Socket.<anonymous> (/var/task/node_modules/mysql/lib/Connection.js:91:28)\n at Socket.<anonymous> (/var/task/node_modules/mysql/lib/Connection.js:502:10)\n at emitOne (events.js:116:13)\n at Socket.emit (events.js:211:7)\n at addChunk (_stream_readable.js:263:12)\n at readableAddChunk (_stream_readable.js:250:11)\n at Socket.Readable.push (_stream_readable.js:208:10)\n at TCP.onread (net.js:607:20)\n --------------------\n at Protocol._enqueue (/var/task/node_modules/mysql/lib/protocol/Protocol.js:144:48)\n at PoolConnection.query (/var/task/node_modules/mysql/lib/Connection.js:200:25)\n at runQuery (/var/task/node_modules/loopback-connector-mysql/lib/mysql.js:197:16)\n at executeWithConnection (/var/task/node_modules/loopback-connector-mysql/lib/mysql.js:239:7)\n at Handshake.onConnect (/var/task/node_modules/mysql/lib/Pool.js:64:7)\n at Handshake.<anonymous> (/var/task/node_modules/mysql/lib/Connection.js:502:10)\n at Handshake._callback (/var/task/node_modules/mysql/lib/Connection.js:468:16)\n at Handshake.Sequence.end (/var/task/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)\n at Handshake.Sequence.OkPacket (/var/task/node_modules/mysql/lib/protocol/sequences/Sequence.js:92:8)\n at Protocol._parsePacket (/var/task/node_modules/mysql/lib/protocol/Protocol.js:278:23)\n at Parser.write (/var/task/node_modules/mysql/lib/protocol/Parser.js:76:12)\n at Protocol.write (/var/task/node_modules/mysql/lib/protocol/Protocol.js:38:16)\n at Socket.<anonymous> (/var/task/node_modules/mysql/lib/Connection.js:91:28)\n at Socket.<anonymous> (/var/task/node_modules/mysql/lib/Connection.js:502:10)\n at emitOne (events.js:116:13)\n at Socket.emit (events.js:211:7)"
73
+
"stack": "Error: ER_NO_SUCH_TABLE: Table 'MY_TEST_DB.CoffeeShop' doesn't exist\n at ..."
71
74
}
72
75
}
73
76
```
74
77
75
78
Retry after a few seconds and it all should work.
76
79
77
-
78
80
## Cleaning Up The Sample
79
81
80
82
Once you are done with the sample environment, avoid unnecessary AWS charges by removing your serverless deployment with the command `npm run sls-cleanup`.
81
83
82
-
If you run into a cleanup error similar to the one below, you may try the command `npm run sls-cleanup` again of manually remove the CloudFormation stack by going to: https://console.aws.amazon.com/cloudformation or using the [aws-cli](https://aws.amazon.com/cli/).
84
+
If you run into a cleanup [error similar to the one below](https://forum.serverless.com/t/very-long-delay-when-doing-sls-remove-of-lambda-in-a-vpc/2535), you will need to manually remove the CloudFormation stack by going to: <https://console.aws.amazon.com/cloudformation> or using the [aws-cli](https://aws.amazon.com/cli/).
An error occurred: mySubnet2 - The subnet 'subnet-077e0f72824fe5dd3' has dependencies and cannot be deleted. (Service: AmazonEC2; Status Code: 400; Error Code: DependencyViolation; Request ID: efd336b9-5f92-4759-876d-81d3e1fffde5).
89
+
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).
0 commit comments