Sunday 15 October 2023

S3 cost estimation for multi-tenant bucket - storage

 Having a single S3 bucket to hold assets from multiple tenants is a common practice for many organizations. Each tenant has a dedicated prefix in the bucket, and here the story gets complicated.

In AWS, you can know the cost of the bucket but not of the single prefix. So how can you estimate the cost? My blog, which was posted on AWS, explains the steps that you can take to estimate the cost.

Follow it here.

I am working on the same concept for API calls and will post it as soon as I finish.

Tuesday 3 October 2023

Using Mongoose with Amazon Document DB

 We live in 2023, and unless it is absolutely necessary, no one calls the DB layer directly. We use the concept of ORM for every modern language and database.

The same is correct for MondoDB, popular document database. There is a project called "Mongoose" that creates the abstract layer above the database. The technology has been there for some time, and we know that it works fine.

But what about Amazon Document DB? If you are new to Document DB, you should know that this is Amazon proprietary technology, which doesn't run the Mongo engine but is based on the technology and concepts developed by Amazon in-house. DocumentDB emulates the Mongo API, and basically each tool and driver that you can use with MongoDB you can also use with DocumentDB. There are still some differences, but all major features are supported.

In this post, we will check if Mongoose can work with DocumentDB.

The code and instructions are based on two other documents:

1. Getting started with Mongoose and MongoDB

2.  Connect to DocumentDB from EC2.

I started by creating the DocumentDB database according to link 2 above.

My cluster looks like this:


It is very important to make sure that the database security group has port 27017 set in the security group inbound rule.




The DocumentDB is deployed into VPC, and there is no public access. It doesn't mean that it cannot be accessed by the internet, but the easiest way to access it is by creating EC2 in the same VPC. I created the one that runs Amazon Linux 3.



You need to make sure that EC2 has the role that allows access to DocumentDB.




and the role looks like


Now we are ready to write the application.



I created a Node application according to Link 1 above.

Don't forget to modify the package.json file and add the module type



You also need global-bundle.pem file since we use TLS connection to DocumentDB.

You can get it by running:

wget https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem

The content of the files:

db.js (note that the connection string is masked)
========================



import mongoose from "mongoose";

export default function connectDB() {
  const url = "mongodb://xxxaws.com:27017/?tls=true&tlsCAFile=global-bundle.pem&retryWrites=false";

  try {
    mongoose.connect(url, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      maxPoolSize: 30
    });
  } catch (err) {
    console.error(err.message);
    process.exit(1);
  }
  const dbConnection = mongoose.connection;
  dbConnection.once("open", (_) => {
    console.log(`Database connected: ${url}`);
  });

  dbConnection.on("error", (err) => {
    console.error(`connection error: ${err}`);
  });
  return;
}

Blog.js
======================
import mongoose from 'mongoose';
const { Schema, model } = mongoose;

const blogSchema = new Schema({
  title: String,
  slug: String,
  published: Boolean,
  author: String,
  content: String,
  tags: [String],
  createdAt: Date,
  updatedAt: Date,
  comments: [{
    user: String,
    content: String,
    votes: Number
  }]
});

const Blog = model('Blog', blogSchema);
export default Blog;

index.js

=========================

import express from "express";
import connectDB from "./config/db.js";
import Blog from './model/Blog.js';

const app = express();
const PORT = 3000;

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

connectDB();


// Create a new blog post object
const article = new Blog({
  title: 'Using mongoose with Amazon Document DB',
  slug: 'learnfrommike',
  published: true,
  content: 'Testing mongoose with Amazon Document DB',
  tags: ['Mongoose', 'Amazon DocumentDB'],
});

// Insert the article in our DocumentDB database
await article.save();

const firstArticle = await Blog.findOne({});
console.log(firstArticle);

// You can modify this function to return the Article object instead of
// printing it to the console
app.get("/", (request, response) => { response.send({ message: "Hello from an Express API!" }); }); app.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}`); });


If we did everything correctly, we should see the content of
"Article" object, being returned from DocumentDB.





The result is good. So Mongoose is compatible with DocumentDB.
Enjoy!