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.com%2Fbotbits%2Fserverless-loopback.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

package-lock.json renamed to examples/lb3-express/package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

serverless.yml renamed to examples/lb3-express/serverless.yml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,31 @@ resources:
9393

9494
# The "Outputs" that your AWS CloudFormation Stack should produce. This allows references between services.
9595
Outputs:
96-
RdsDBInstanceIdentifier:
96+
DbInstanceIdentifier:
9797
Description: The DB Name for the (MySQL) RDS created
9898
Value:
9999
Ref: mySqlDb
100+
LoopbackPostman:
101+
Description: The baseUrl value to configure for your Postman collection
102+
Value:
103+
'Fn::Join':
104+
- ''
105+
- - 'https://'
106+
- Ref: 'ApiGatewayRestApi'
107+
- '.execute-api.'
108+
- Ref: 'AWS::Region'
109+
- '.'
110+
- Ref: 'AWS::URLSuffix'
111+
- '/${self:provider.stage}/api'
112+
LoopbackApiExplorer:
113+
Description: The URL to directly access the loopback API explorer
114+
Value:
115+
'Fn::Join':
116+
- ''
117+
- - 'https://'
118+
- Ref: 'ApiGatewayRestApi'
119+
- '.execute-api.'
120+
- Ref: 'AWS::Region'
121+
- '.'
122+
- Ref: 'AWS::URLSuffix'
123+
- '/${self:provider.stage}/api/explorer/'

examples/lb4-express/.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright IBM Corp. 2020. All Rights Reserved.
2+
// Node module: @loopback/example-express-composition
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
module.exports = {
7+
extends: ['@loopback/eslint-config'],
8+
};

examples/lb4-express/.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
*.json

examples/lb4-express/.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"bracketSpacing": false,
3+
"singleQuote": true,
4+
"printWidth": 80,
5+
"trailingComma": "all",
6+
"arrowParens": "avoid"
7+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"editor.rulers": [80],
3+
"editor.tabCompletion": "on",
4+
"editor.tabSize": 2,
5+
"editor.trimAutoWhitespace": true,
6+
"editor.formatOnSave": true,
7+
8+
"files.exclude": {
9+
"**/.DS_Store": true,
10+
"**/.git": true,
11+
"**/.hg": true,
12+
"**/.svn": true,
13+
"**/CVS": true,
14+
"dist": true,
15+
},
16+
"files.insertFinalNewline": true,
17+
"files.trimTrailingWhitespace": true,
18+
19+
"typescript.tsdk": "./node_modules/typescript/lib"
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "Watch and Compile Project",
8+
"type": "shell",
9+
"command": "npm",
10+
"args": ["--silent", "run", "build:watch"],
11+
"group": {
12+
"kind": "build",
13+
"isDefault": true
14+
},
15+
"problemMatcher": "$tsc-watch"
16+
},
17+
{
18+
"label": "Build, Test and Lint",
19+
"type": "shell",
20+
"command": "npm",
21+
"args": ["--silent", "run", "test:dev"],
22+
"group": {
23+
"kind": "test",
24+
"isDefault": true
25+
},
26+
"problemMatcher": ["$tsc", "$eslint-stylish", "$eslint-compact"]
27+
}
28+
]
29+
}

0 commit comments

Comments
 (0)