Databases And Persistence / Integrate With a Database Provider /
Integrate MongoDB Atlas with Northflank
This guide explains how to connect your MongoDB® Atlas cluster to your applications on Northflank. We will first take you through setting up an Atlas cluster, deploying an application on Northflank, and connecting a database in your cluster with your application.
We will then look at egress gateways and configuring your Atlas cluster and Northflank project to reduce latency and bandwidth costs.
To get started you will need a Northflank account and a MongoDB Atlas account. Create your Northflank account here and follow the onboarding steps, and create your MongoDB Atlas account here .
Create a MongoDB Atlas cluster
For this example we will deploy a free cluster on Google Cloud, in the central US region, the same as the Northflank project we will create in this guide.
- Log in to your Atlas account, and select the organisation and project to deploy your cluster in
- Navigate to the Database page under the Deployment section and click Build a database
- Configure your Atlas cluster:
- select the
M0 (free)
plan - Choose
Google Cloud
as the provider - Select
Iowa (us-central1)
for the region - Name your cluster
Northflank-Atlas
- select the
- Click Create
You should be taken automatically to the security quickstart page for your new Atlas cluster. You can read more about configuring your Atlas cluster for low latency, and reduced bandwidth costs later in this guide.
Configure MongoDB Atlas authentication
You can authenticate to your cluster using a username and password combination, or via certificate. To configure authentication using the quickstart page for security:
- Select username and password, enter a username (
northflank
, for example), and generate a password - Save the password in a secure place and click Create user
- Add
0.0.0.0/0
to your IP Access List - Click finish and close, and return to the cluster overview
This will allow Northflank workloads to connect to your Atlas cluster. You can read more about configuring a secure egress gateway on Northflank later in this guide.
Find your MongoDB Atlas connection string
We will now find and save your connection string, which you will use to connect your application to your Atlas database.
- From the overview click the Connect button for the database we just deployed
- Select Drivers from the 'connect to your application' section
- Skip the configuration steps and find and copy your connection string. It should look like this:
mongodb+srv://<username>:<password>@<host>/?retryWrites=true&w=majority
- Replace
<password>
with your password for the user that you created earlier, and save the connection string in a secure place
We are now ready to connect our Atlas database with an application running on Northflank.
Deploy an application on Northflank
Follow the steps below to create a project and deploy your application:
- Open the Create new menu and select Project
- Give your project a name (for example, ‘Atlas integration’) and select
US - Central
as the region - Click Create project, then select Create secret and give it a name
- Add the following variables to your secret group:
PAYLOAD_SECRET=${fn.randomSecret(32)}
MONGODB_URI=<your-Atlas-connection-string>
- Open the Create new menu and select Combined service
- Give it a name, enter
https://github.com/northflank-guides/deploy-payload-on-northflank-with-atlas
for the repository and select Dockerfile for the build type - Create your service, and it will start a new build and deploy it when ready. It may take a moment for the Payload application to become ready after the container has deployed and is running.
- Open the domain displayed in the header of your service to view the Payload application and add your first user
You can now return to your database on Atlas to Browse collections, where you will see your new user in the users
collection.
Use an egress gateway to secure your network traffic
In our example we allowed any IP address to connect to our Atlas cluster, by adding 0.0.0.0/0
to the IP Access List. This makes your databases less secure, and should only be used for demonstration and testing purposes.
To use MongoDB Atlas with Northflank in production you should restrict your Atlas cluster to specific IP addresses. To allow your Northflank projects to communicate with your Atlas cluster, you can request to route traffic via an egress gateway, and provide a static IP address for your Atlas cluster’s IP Access List.
Contact Northflank for more information.Provision clusters to reduce latency and bandwidth costs
You can provision your Atlas clusters and Northflank projects on the same cloud provider and region to reduce latency between your applications and databases, and reduce your bandwidth costs.
If you’re deploying on Northflank’s managed cloud, you can choose Google Cloud when configuring your Atlas cluster, and choose the corresponding region:
Northflank managed cloud region | Atlas Google Cloud region |
---|---|
Europe West | London (europe-west2) |
Europe West Netherlands | Netherlands (europe-west4) |
US East | South Carolina (us-east1) |
US Central | Iowa (us-central1) |
US West | Oregon (us-west1) |
Asia Southeast | Singapore (asia-southeast1) |
You can also deploy Northflank clusters in your own cloud provider account, and use any provider and region available to you on MongoDB Atlas.