Reputation: 23
I'm working on e-commerce project (SOA architecture). There are multiple applications (NodeJS express/mongoose) with their own APIs that communicate each others for example: - app.1 for product management - app.2 for cart management - app.3 for user management ecc ecc...
Now I want to move these applications in AWS Lambda and migrate them to a FaaS architecture but it's not clear for me how multiple function will share common models.
For example: app1 at this time has his own model for Product:
productModel.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const productSchema = new Schema({
insDate: {
type: Schema.Types.Date,
required: true,
default: Date.now()
},
description: {
type: Schema.Types.String,
required: true,
trim: true
},
price: {
type: Schema.Types.Number,
required: true
}
});
const Product = mongoose.model('products', productSchema);
module.exports = Product;
which it's used in the controller to fetch or update data:
productController.js- inside method getData
Product.find()
.then(products=> {
res.send(products);
})
.catch(err => {
console.log('err', err);
});
productController.js - inside method updateData
var product = new Product({
description: req.body.description,
price: req.body.price,
});
product.save(function (err) {
if (err) {
console.log(err);
return;
}
});
Now my goal is to create two AWS Lambda functions:
1) the first to fetch data
2) the second to update data
How can I do that? My idea is to create two different nodejs projects (and repositories) which have both express and mongoose libraries installed.
The first one has only the method to fetch data in his controller and the second one has only the method do update data, right?
Both functions share the same ProductModel, which is the best way to do that? 1) replicate the productModel on both projects -> I don't think that it's a good maintainable idea. 2) create a monolithic repository which has only a single model with multiple functions which are deployed on multiple aws lambda instances -> I don't think that it's a good idea to create a single monolithic repository 3) create a single library for productModel and share it as a dependency for both projects -> is it good? I think that this approach is good for handling model changes on one single point and update both projects. 4) any other solution?
Thanks for your help
Upvotes: 1
Views: 809
Reputation: 87
For the model you could use a lambda layer which you invoke. https://docs.aws.amazon.com/de_de/lambda/latest/dg/configuration-layers.html
Upvotes: 1