Skip to content

Commit faf6e60

Browse files
authored
Added Outputs to serverless.yml & improved documentation (#1)
### Deployment - Added RDS DBInstanceIdentifier to CloudFormation Outputs in `serverless.yml` ### Documentation - Rectified lambda cleanup - Added optional item for RDS customization - Added FOSSA badge - Added reference to article - Cleaned up Codacy issues on README.md
1 parent e30cc37 commit faf6e60

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

README.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# 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) -->
2+
[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com)
3+
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/botbits/serverless-loopback/master/LICENSE)
4+
[![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)
5+
[![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)
36

47
A sample [loopback](https://loopback.io/) application deployed to AWS Lambda using [serverless](https://serverless.com/).
58

6-
79
## Overview
810

911
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.
1012
<!--
1113
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/). -->
1214

15+
Check out [this article](https://medium.com/smac-4u/serverless-loopback-9ff0d6fa539d) for a more in-depth explanation of this sample.
1316

1417
## Using
1518

@@ -21,39 +24,39 @@ Or clone the git repo:
2124

2225
`git clone https://github.com/botbits/serverless-loopback`
2326

24-
2527
## About Sample Provided
2628

2729
The sample loopback application provided was created by following the process below:
2830

29-
1. Using the command `npm run lb` and selecting:
31+
1. Using the command `npm run lb` and selecting:
3032

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)`
3234

33-
2. Adding a new MySQL database to `lib/server/datasources.json`.
35+
2. Adding a new MySQL database to `lib/server/datasources.json`.
3436

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:
4038

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
4142

4243
## Customizing & Deploying This Sample
4344

4445
The following steps can be used to customize this sample to your needs and then deploy:
4546

4647
<!-- 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)]).
4748
-->
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).
4952

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`
5154

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`)
5356

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/`
5558

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:
5760

5861
```json
5962
{
@@ -67,27 +70,25 @@ The following steps can be used to customize this sample to your needs and then
6770
"sqlState": "42S02",
6871
"index": 0,
6972
"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 ..."
7174
}
7275
}
7376
```
7477

7578
Retry after a few seconds and it all should work.
7679

77-
7880
## Cleaning Up The Sample
7981

8082
Once you are done with the sample environment, avoid unnecessary AWS charges by removing your serverless deployment with the command `npm run sls-cleanup`.
8183

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/).
8385

84-
```
86+
```shell
8587
Serverless Error ---------------------------------------
8688

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).
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).
8890
```
8991

90-
9192
## License
9293

9394
MIT © [BotBits<sup>SM</sup>](https://github.com/botbits)

serverless.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,22 @@ resources:
7878
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-rds-database-instance.html
7979
Type: AWS::RDS::DBInstance
8080
Properties:
81-
AllocatedStorage: '5'
81+
AllocatedStorage: '5' # minimal size for this sample
8282
BackupRetentionPeriod: 0 # disabling backups for this sample
83-
DBInstanceClass: 'db.t2.micro'
83+
DBInstanceClass: 'db.t2.micro' # minimal size for this sample
8484
DBName: '${self:custom.DB.${self:provider.stage}.NAME}' # creating a database for this sample
8585
DBSubnetGroupName: { 'Ref' : 'myDBSubnetGroup' }
8686
Engine: 'mysql'
8787
MasterUsername: '${self:custom.DB.${self:provider.stage}.USERNAME}' # this will show in clear text in CloudFormation
8888
MasterUserPassword: '${self:custom.DB.${self:provider.stage}.PASSWORD}' # this will show in clear text in CloudFormation
89-
MultiAZ: false
89+
MultiAZ: false # disabling for this sample
9090
PubliclyAccessible: false
9191
VPCSecurityGroups:
9292
- { 'Fn::GetAtt' : ['myVPC', 'DefaultSecurityGroup'] }
93+
94+
# The "Outputs" that your AWS CloudFormation Stack should produce. This allows references between services.
95+
Outputs:
96+
RdsDBInstanceIdentifier:
97+
Description: The DB Name for the (MySQL) RDS created
98+
Value:
99+
Ref: mySqlDb

0 commit comments

Comments
 (0)