Skip to content

Commit fd23112

Browse files
authored
Moved lb3 to examples and added lb4-express (#15)
1 parent e4a483d commit fd23112

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+18374
-70
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Loopback4
2+
dist
3+
tsconfig.tsbuildinfo
4+
15
# Serverless
26
.serverless
37

README.md

Lines changed: 8 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,30 @@
44
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/3988cecd1c154bb99c310fb77bbc1ae9)](https://www.codacy.com/app/marcelobern/serverless-loopback?utm_source=github.com&utm_medium=referral&utm_content=botbits/serverless-loopback&utm_campaign=Badge_Grade)
55
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.51.al%2Fbotbits%2Fserverless-loopback.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.51.al%2Fbotbits%2Fserverless-loopback?ref=badge_shield)
66

7-
A sample [loopback](https://loopback.io/) application deployed to AWS Lambda using [serverless](https://serverless.com/).
7+
A collection of sample [loopback](https://loopback.io/) applications deployed to [AWS Lambda](https://aws.amazon.com/lambda/) using [serverless](https://serverless.com/).
88

99
## Overview
1010

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.
12-
<!--
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/). -->
11+
The following examples are available:
1412

15-
Check out [this article](https://medium.com/smac-4u/serverless-loopback-9ff0d6fa539d) for a more in-depth explanation of this sample.
13+
- [Loopback 3 (using express)](examples/lb3-express/README.md)
14+
- [Loopback 4 (via express)](examples/lb4-express/README.md)
1615

1716
## Using
1817

1918
[Create a *serverless* service](https://serverless.com/framework/docs/providers/aws/cli-reference/create) from this template by using the command:
2019

21-
`serverless create --template-url https://github.com/botbits/serverless-loopback`
20+
`serverless create --template-url https://github.com/botbits/serverless-loopback/examples/<SELECT_EXAMPLE>`
2221

2322
Or clone the git repo:
2423

2524
`git clone https://github.com/botbits/serverless-loopback`
2625

27-
## About Sample Provided
26+
and `cd examples` to start checking your favorite example!
2827

29-
The sample loopback application provided was created by following the process below:
28+
## Contributing
3029

31-
1. Using the command `npm run lb` and selecting:
32-
33-
- What kind of application do you have in mind? `api-server (A LoopBack API server with local User auth)`
34-
35-
2. Adding a new MySQL database to `lib/server/datasources.json`.
36-
37-
3. Adding the CoffeeShop model and initializing it with data:
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
42-
43-
## Customizing & Deploying This Sample
44-
45-
The following steps can be used to customize this sample to your needs and then deploy:
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)]).
48-
-->
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).
52-
53-
3. Deploy your project to AWS using the command `npm run sls-deploy`
54-
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`)
56-
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/`
58-
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:
60-
61-
```json
62-
{
63-
"error": {
64-
"statusCode": 500,
65-
"name": "Error",
66-
"message": "ER_NO_SUCH_TABLE: Table 'MY_TEST_DB.CoffeeShop' doesn't exist",
67-
"code": "ER_NO_SUCH_TABLE",
68-
"errno": 1146,
69-
"sqlMessage": "Table 'MY_TEST_DB.CoffeeShop' doesn't exist",
70-
"sqlState": "42S02",
71-
"index": 0,
72-
"sql": "SELECT count(*) as \"cnt\" FROM `CoffeeShop` ",
73-
"stack": "Error: ER_NO_SUCH_TABLE: Table 'MY_TEST_DB.CoffeeShop' doesn't exist\n at ..."
74-
}
75-
}
76-
```
77-
78-
Retry after a few seconds and it all should work.
79-
80-
## Cleaning Up The Sample
81-
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`.
83-
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/).
85-
86-
```shell
87-
Serverless Error ---------------------------------------
88-
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).
90-
```
30+
Please feel free to contribute your examples of using loopback4 with other web frameworks under the `examples` folder.
9131

9232
## License
9333

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/lb3-express/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# examples/lb3-express
2+
3+
This example shows how to use the [serverless framework](https://www.serverless.com/framework/docs/providers/aws/) to run loopback3 (with express) in [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/lambda-nodejs.html).
4+
5+
## Overview
6+
7+
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.
8+
<!--
9+
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/). -->
10+
11+
Check out [this article](https://medium.com/smac-4u/serverless-loopback-9ff0d6fa539d) for a more in-depth explanation of this sample.
12+
13+
## About Sample Provided
14+
15+
The sample loopback application provided was created by following the process below:
16+
17+
1. Using the command `npm run lb` and selecting:
18+
19+
- What kind of application do you have in mind? `api-server (A LoopBack API server with local User auth)`
20+
21+
2. Adding a new MySQL database to `lib/server/datasources.json`.
22+
23+
3. Adding the CoffeeShop model and initializing it with data:
24+
25+
- `lib/common/coffee-shop.json`: CoffeeShop model definition
26+
- `lib/server/model-config.json`: add CoffeeShop model so it can be loaded
27+
- `lib/server/boot/create-sample-models.js`: initialize CoffeeShop model with data
28+
29+
## Customizing & Deploying This Sample
30+
31+
The following steps can be used to customize this sample to your needs and then deploy:
32+
33+
<!-- 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)]).
34+
-->
35+
1. Create your own loopback models with the command `npm run lb-model`
36+
37+
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).
38+
39+
3. Deploy your project to AWS using the command `npm run sls-deploy`
40+
41+
4. From the serverless `Stack Outputs`, retrieve `LoopbackApiExplorer` to access the loopback4 API explorer (it should look something like `https://API_GATEWAY_ID.execute-api.AWS_REGION.amazonaws.com/SERVERLESS_STAGE/api/explorer/`). You should end up with an URL similar to `https://XXXXXXXXXX.execute-api.us-east-1.amazonaws.com/dev/api/explorer/`.
42+
43+
*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:
44+
45+
```json
46+
{
47+
"error": {
48+
"statusCode": 500,
49+
"name": "Error",
50+
"message": "ER_NO_SUCH_TABLE: Table 'MY_TEST_DB.CoffeeShop' doesn't exist",
51+
"code": "ER_NO_SUCH_TABLE",
52+
"errno": 1146,
53+
"sqlMessage": "Table 'MY_TEST_DB.CoffeeShop' doesn't exist",
54+
"sqlState": "42S02",
55+
"index": 0,
56+
"sql": "SELECT count(*) as \"cnt\" FROM `CoffeeShop` ",
57+
"stack": "Error: ER_NO_SUCH_TABLE: Table 'MY_TEST_DB.CoffeeShop' doesn't exist\n at ..."
58+
}
59+
}
60+
```
61+
62+
Retry after a few seconds and it all should work.
63+
64+
## Cleaning Up The Sample
65+
66+
Once you are done with the sample environment, avoid unnecessary AWS charges by removing your serverless deployment with the command `npm run sls-cleanup`.
67+
68+
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/).
69+
70+
```shell
71+
Serverless Error ---------------------------------------
72+
73+
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).
74+
```
75+
76+
## License
77+
78+
MIT © [BotBits<sup>SM</sup>](https://github.com/botbits)
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)