Azure CosmosDB is Too Expensive for Experimenting. Alternative: MongoDB Atlas

Background

Recently I’ve been experimenting with Azure Functions, and I’ve got to the point where I wanted to play with Functions interacting with a storage layer. Azure CosmosDB was the obvious choice. I went through the process of setting up a database and turning all the performance dials down to their minimum settings. Nonetheless, after four days playing with my experimental DB I realised that it was going to cost me more than £20 (UK) per month to keep my toy DB running. I needed an alternative.

MongoDB Atlas

MongoDB Atlas (https://www.mongodb.com/cloud/atlas) is the hosted DB service from the people behind MongoDB. For my purposes it is attractive as it provides a free tier for up to 500MB of storage (https://www.mongodb.com/cloud/atlas/pricing).

It set up an account and downloaded the Compass DB management tool to work with the data. One snag I ran into was that if you had a password with special characters in it then this needed to be URI encoded before entering into the Compass login screen. This held me up for a good couple of hours as the error message back from Compass was the cryptic, “Missing delimiting slash between hosts and options”.

Finally I had my database running and had entered some test data:

2018-05-04 08_22_47-MongoDB Compass Community - sleepsuntil-shard-00-00-vyqwg.mongodb.net_27017_slee

Connecting Azure Functions to MongoDB Atlas

I pulled my connection string information over from Atlas and stored it in the application keys (See my previous post on storing API keys for Azure Functions).

Now I needed to open up the Atlas firewall to allow inbound connections from Azure. This is non-trival since Azure will allocate an outbound IP from Functions from any of the range of IP addresses for their whole data centre. See the Microsoft article explaining outbound IP addresses. I’m hosting in “UK West” and at the time of writing the data centre had 24 different IP ranges. Given the fact that I only have toy data in my DB I decided to allow access from all IPs. If you have a real world example you will need to implement some process to lock this down some more.

With this setup complete I now have some working code, see below, to show Azure Functions connecting to MongoDB Atlas…and it is free!

module.exports = function (context, req) {
    context.log("Starting Atlas example");
    const mongoClient = require("mongodb").MongoClient;

    function opendb() {
        const url = process.env["atlasurl"];
        context.log("Attempting to connect"); 
        const db_promise =  mongoClient.connect(url);
        return db_promise;
    }

    function readdata(db) {
        context.log("Accessing sleepsuntil");
        let dbo = db.db("sleepsuntil");
        context.log("Got DBO " + dbo);
        let query = { key: "example1" };
        context.log("Starting query");
        let results = dbo.collection("testing").findOne(query);
        context.log("Got results");
        return results;
    }

    opendb()
        .then((db) => {
            context.log("Return from open was: " + db);
            return readdata(db);
        })
        .then((results) => {
            context.log("Read: " + JSON.stringify(results));
            context.res.body = `
            <table>
            <tr><td>_id<td>${results._id}</tr>
            <tr><td>key<td>${results.key}</tr>
            <tr><td>value<td>${results.value}</tr>
            </table>`;
            context.res.headers = { "Content-Type": "text/html" };

            context.done();
        })
        .catch((msg) => {
            context.log("Error caught: " + msg);
            context.done();
        });
};

One thought on “Azure CosmosDB is Too Expensive for Experimenting. Alternative: MongoDB Atlas

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s