Explore

Connecting your Heroku app to Atlas

Overcoming this weird IP Address issue!

March, 9th 2020

Technology

Connecting your Heroku app to Atlas

Heroku is a great PaaS tool for quickly and dependably deploying web apps. Because I'm working on a side project, I appreciate that Heroku has things like buildpacks that you can add to your dynos (Heroku server instances!) to quickly take care of most of the server configuration! It actually saved me a LOT of time in this particular project!

I've also decided I'd be using MongoDB hosted on Atlas in this case since it's quick to get started with, extremely flexible and honestly Mongodb is my favorite data store!

EDIT - Quick Note

Before moving ahead here, I'm assuming you have a project ready to go on Heroku and are in the process of trying to connect it to mongo db on Atlas. Later on down the line I think it would be helpful to write a tutorial on developing an API from scratch and then move into how one would host on Heroku.

Update - 11/18/2021: some people have written saying they're now having trouble with the fixie method I mention below. I'll be looking into a new solution for getting things working with fixie.

My Express App

The project I'm working on is a small API built using Express.js. Express is a light-weight, unopinionated framework for building Web Apps and APIs using Javascript.

I should, however, point out that while I'm using Express on this project, the tip I'm giving here is platform and language agnostic. This advice will work for your app whether it's built with Python, PHP, Java, Ruby or otherwise.

Heroku and Atlas not playing nice

I deployed my app to Heroku with no problem. It wasn't until I started testing registration and login that I found something had broken. When I took a look at the logs I could see errors telling me there was an issue with my mongo connection.

Not seeing eye to IP

It turns out Atlas requires you to supply it with a list of whitelisted ip addresses. That's when I realized I couldn't find an IP address for my Heroku app.

App has no IP InformationNo IP address in App Info

But why though?

It's no surprise to know that underneath Heroku's awesome interface they're actually making use of Amazon Web Services. A lot of companies do this though. Give all of your major dev ops tasks to Amazon since they're solving the problem for themselves anyway.

The issue only arises when you realize that every redeploy on Heroku creates a new server instance on AWS. And every instance gets a new IP Address.

It's hard to supply an ip address that’s always changing.

So how do we solve the problem?

After doing a bit of research I discovered two working solutions. It took a bit of hopping from forum to forum but I eventually got my app up and running.

Solution 1: Whitelist everybody

You can go this route and just whitelist all connections coming into Atlas. Of course, in this case you want to be sure you are using best practices in terms of authorization. Ensure you have solid usernames and passwords for your mongodb users and ensure you don't have any defaults in place!

After logging into Atlas and navigating to your database. Find and click on the "Network Access" link.

Network Access LinkNetwork access link located in the the left nav bar.

You'll then see a list of whitelisted IP addresses. Yours might be empty. You'll want to click on the "+ add IP Address" button in the upper right.

Add IP AccessUse this button to add an IP address

You'll get a pop up form for adding the IP address. You can click on the "allow access from anywhere" button or just enter 0.0.0.0/0 into the "Whitelist Entry" input.

Allow access from anywhereAdd "0.0.0.0/0" to the Whitelist Entry field. Comment field is optional.

I just put a comment reminding myself what this entry is for in the comment input. Informative but optional.

Once you're done you should get an email that looks like this:

Warning email from MongoDB AtlasThis is the email I got from Atlas when I added access from anywhere. Good to know they have this in case you really need it!

You should now be good to continue developing your app. Again, you want to make sure using this solution that you have strong credentials and no defaults are in place. If you don't want to (or are not able to) allow access to everyone you can use solution 2!

Solution 2: Fixie Add-on

Fixie is an add-on in the Heroku add-on marketplace that gives your app a static IP address to reference for outgoing requests. It's fairly easy to setup and once you're done you'll be able connect your app to your Atlas MongoDB without allowing access to everyone.

For this solution we'll be starting in Heroku.

Login to your Heroku account and navigate to your apps' "Overview" page. Once you're here you'll click "Configure add-ons" to view your add-ons.

Heroku App Overview PageOnce you've navigated here, click on the "Configure Add ons" link. You could also click the "Resources" tab.

Once you're here you'll see an "Add-ons" search bar below. Type in "Fixie" and click on it when it shows up in the results. Wait for the pop up to load.

Fixie SearchSearch for Fixie using the add-on search below

Once you’ve clicked on it, wait for the pop up to load. You’ll see a pop up that will allow you to add Fixie. I’m using the “Tricycle - Free” plan because I don’t want to pay right now but you can choose your preferred plan and click “Provision”.

Fixie Pop Up and ProvisionChoose your preferred plan and click "Provision"

Afterwards, You'll see the following confirmation message. Click the Fixie link in the add-ons list to be taken to Fixie's dashboard.

Fixie Confirmation MessageYou'll see the Fixie installed message. Click on the "Fixie" link below in the list of add-ons. The Fixie dashboard will open in another tab.

On this page you'll want to look for "Account Details" below. Here you will find the IP address(es) you'll need to place in the whitelist over in Atlas.

Fixie IP AddressesIn "Account Details", you'll find your IP addresses

Now we can head over to Atlas, navigating to our MongoDB. We follow the same steps we followed for allowing access to every one from this point.

Network Access LinkNetwork access link located in the the left nav bar.

Once in Network Access, confirm you are on the "IP Whitelist" tab and click "add ip address" in the upper right.

Add IP AccessUse this button to add an IP address

Now you can add your Fixie URLs to the whitelist. If you get 2 IPs like I did, you'll be doing this twice:

Add Fixie IPsAdd both of your Fixie IP addresses to the Whitelist

Click confirm and that's it! Your MongoDB should restart. Once it's restarted, you should be able to connect your Heroku app with no issues!

Keywords:
#javascript#node.js#express.js#heroku
#mongodb#fixie#atlas