multi-service & mono-repo serverless application model with dynamodb-local and layers support
This repo includes 4 + 1 (Labmda Functions as a separate services) separate services as follows;
- storage-s3 (AWS S3 Bucket),
- dynamodb (AWS DynamoDB),
- cognito (AWS Cognito),
- api-gateway (AWS RestApi ApiGateway),
- api (AWS Lambda Functions)
- users (AWS Lambda Functions)
- notes (AWS Lambda Functions)
$ npm install
$ npm install serverless -g
- In order to see debug logs, before running the deploy command, export the serverless debug environment variable as follows
$ export SLS_DEBUG=*
- In order to deploy the whole mutli-service app
$ STAGE=<stage_name> npm run deploy
- In order to update the whole mutli-service app
$ STAGE=<stage_name> npm run deploy
- In order to remove the whole mutli-service app
$ STAGE=<stage_name> npm run remove
- In order to deploy a lambda service
$ STAGE=<stage_name> npm run deploy-api-<service_name>
- In order to deploy the whole lambda services
$ STAGE=<stage_name> npm run deploy-api
- In order to deploy the whole resources which includes S3 storage, DynamoDB, Cognito and Api GateWay services
$ STAGE=<stage_name> npm run deploy-resources
- Be careful when using
remove
command which will completely delete targeted stack - See the
package.json
for the corresponding service stack names
- First intialize mock tables from
mock-<table_name>-table.json
under./test/mock
folder. The field names and types should coincide with the table definition inresources/dynamodb/resources/tables.yml
file. - And then run the following commands for the corresponding tests
# For example, in order to test users service
$ npm run test-users
# Or, in order to test campaigns service
$ npm run test-notes
- In order to be able to test lambda services with dynamodb-local, you need to replace all
aws-sdk
dynamodb instances withserverless-dynamodb-client
dynamodb instances.serverless-dynamodb-client
library will automatically will initialize the corresponding aws credentials and dynamodb instances for you.
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
// The above dynamodb instances should be replaced with below
const dynamoDbClient = require('serverless-dynamodb-client');
const dynamoDb = dynamoDbClient.doc;
- Check the environment variables for the table names from
<lambda_service>/serverless.yml
file and use accordingly in your code. - Libraries which are used through aws layers service should be imported from
/opt/<layer_name>/<file_name>
For example, let's say the layer name islib
and the file that you want to import isawsomeLibrary.js
, then
const { foo, baz } = require('/opt/lib/awsomeLibrary');