|
4 | 4 | [](https://www.codacy.com/app/marcelobern/serverless-loopback?utm_source=github.com&utm_medium=referral&utm_content=botbits/serverless-loopback&utm_campaign=Badge_Grade) |
5 | 5 | [](https://app.fossa.io/projects/git%2Bgithub.com%2Fbotbits%2Fserverless-loopback?ref=badge_shield) |
6 | 6 |
|
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/). |
8 | 8 |
|
9 | 9 | ## Overview |
10 | 10 |
|
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: |
14 | 12 |
|
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) |
16 | 15 |
|
17 | 16 | ## Using |
18 | 17 |
|
19 | 18 | [Create a *serverless* service](https://serverless.com/framework/docs/providers/aws/cli-reference/create) from this template by using the command: |
20 | 19 |
|
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>` |
22 | 21 |
|
23 | 22 | Or clone the git repo: |
24 | 23 |
|
25 | 24 | `git clone https://github.com/botbits/serverless-loopback` |
26 | 25 |
|
27 | | -## About Sample Provided |
| 26 | +and `cd examples` to start checking your favorite example! |
28 | 27 |
|
29 | | -The sample loopback application provided was created by following the process below: |
| 28 | +## Contributing |
30 | 29 |
|
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. |
91 | 31 |
|
92 | 32 | ## License |
93 | 33 |
|
|
0 commit comments