Skip to content

Commit e30cc37

Browse files
committed
Initial commit
0 parents  commit e30cc37

25 files changed

+11941
-0
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# http://editorconfig.org
4+
5+
root = true
6+
7+
[*]
8+
indent_style = space
9+
indent_size = 2
10+
end_of_line = lf
11+
charset = utf-8
12+
trim_trailing_whitespace = true
13+
insert_final_newline = true

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/lib/client/

.eslintrc.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"parserOptions": {
3+
"ecmaVersion": 2017
4+
},
5+
"env": {
6+
"node": true,
7+
"es6": true
8+
},
9+
"extends": "eslint:recommended",
10+
"rules": {
11+
"indent": [
12+
"error",
13+
4
14+
],
15+
"linebreak-style": [
16+
"error",
17+
"unix"
18+
],
19+
"quotes": [
20+
"error",
21+
"single"
22+
],
23+
"camelcase": [
24+
"error"
25+
],
26+
"semi": [
27+
"warn",
28+
"always"
29+
],
30+
"prefer-const": ["error", {
31+
"destructuring": "any",
32+
"ignoreReadBeforeAssign": false
33+
}],
34+
"strict": [
35+
"error",
36+
"global"
37+
]
38+
}
39+
}

.gitignore

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Serverless
2+
.serverless
3+
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
yarn-debug.log*
9+
yarn-error.log*
10+
11+
# Runtime data
12+
pids
13+
*.pid
14+
*.seed
15+
*.pid.lock
16+
17+
# Directory for instrumented libs generated by jscoverage/JSCover
18+
lib-cov
19+
20+
# Coverage directory used by tools like istanbul
21+
coverage
22+
23+
# nyc test coverage
24+
.nyc_output
25+
26+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
27+
.grunt
28+
29+
# Bower dependency directory (https://bower.io/)
30+
bower_components
31+
32+
# node-waf configuration
33+
.lock-wscript
34+
35+
# Compiled binary addons (https://nodejs.org/api/addons.html)
36+
build/Release
37+
38+
# Dependency directories
39+
node_modules/
40+
jspm_packages/
41+
42+
# TypeScript v1 declaration files
43+
typings/
44+
45+
# Optional npm cache directory
46+
.npm
47+
48+
# Optional eslint cache
49+
.eslintcache
50+
51+
# Optional REPL history
52+
.node_repl_history
53+
54+
# Output of 'npm pack'
55+
*.tgz
56+
57+
# Yarn Integrity file
58+
.yarn-integrity
59+
60+
# dotenv environment variables file
61+
.env
62+
63+
# next.js build output
64+
.next

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 BotBits (sm)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# serverless-loopback
2+
[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) <!-- Skipping tests for now [![Build Status](https://travis-ci.org/botbits/serverless-loopback.svg?branch=master)](https://travis-ci.org/botbits/serverless-loopback) --> [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/botbits/serverless-loopback/master/LICENSE) <!-- Codacy does not support any language in this project. [![Codacy Badge](https://api.codacy.com/project/badge/Grade/3988cecd1c154bb99c310fb77bbc1ae9)](https://www.codacy.com/app/marcelobern/serverless-loopback?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=botbits/serverless-loopback&amp;utm_campaign=Badge_Grade) -->
3+
4+
A sample [loopback](https://loopback.io/) application deployed to AWS Lambda using [serverless](https://serverless.com/).
5+
6+
7+
## Overview
8+
9+
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+
<!--
11+
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+
13+
14+
## Using
15+
16+
[Create a *serverless* service](https://serverless.com/framework/docs/providers/aws/cli-reference/create) from this template by using the command:
17+
18+
`serverless create --template-url https://github.com/botbits/serverless-loopback`
19+
20+
Or clone the git repo:
21+
22+
`git clone https://github.com/botbits/serverless-loopback`
23+
24+
25+
## About Sample Provided
26+
27+
The sample loopback application provided was created by following the process below:
28+
29+
1. Using the command `npm run lb` and selecting:
30+
31+
- What kind of application do you have in mind? `api-server (A LoopBack API server with local User auth)`
32+
33+
2. Adding a new MySQL database to `lib/server/datasources.json`.
34+
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
40+
41+
42+
## Customizing & Deploying This Sample
43+
44+
The following steps can be used to customize this sample to your needs and then deploy:
45+
46+
<!-- 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+
1. Create your own loopback models with the command `npm run lb-model`
49+
50+
2. Deploy your project to AWS using the command `npm run sls-deploy`
51+
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`)
53+
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/`
55+
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:
57+
58+
```json
59+
{
60+
"error": {
61+
"statusCode": 500,
62+
"name": "Error",
63+
"message": "ER_NO_SUCH_TABLE: Table 'MY_TEST_DB.CoffeeShop' doesn't exist",
64+
"code": "ER_NO_SUCH_TABLE",
65+
"errno": 1146,
66+
"sqlMessage": "Table 'MY_TEST_DB.CoffeeShop' doesn't exist",
67+
"sqlState": "42S02",
68+
"index": 0,
69+
"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)"
71+
}
72+
}
73+
```
74+
75+
Retry after a few seconds and it all should work.
76+
77+
78+
## Cleaning Up The Sample
79+
80+
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+
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/).
83+
84+
```
85+
Serverless Error ---------------------------------------
86+
87+
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).
88+
```
89+
90+
91+
## License
92+
93+
MIT © [BotBits<sup>SM</sup>](https://github.com/botbits)

lambda-wrapper.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const serverless = require ('serverless-http');
4+
5+
const app = require('./lib/server/server');
6+
7+
exports.handler = serverless(app);

lib/.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "loopback"
3+
}

lib/.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
*.csv
2+
*.dat
3+
*.iml
4+
*.log
5+
*.out
6+
*.pid
7+
*.seed
8+
*.sublime-*
9+
*.swo
10+
*.swp
11+
*.tgz
12+
*.xml
13+
.DS_Store
14+
.idea
15+
.project
16+
.strong-pm
17+
coverage
18+
node_modules
19+
npm-debug.log

lib/.yo-rc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"generator-loopback": {}
3+
}

0 commit comments

Comments
 (0)