DEV Community

Nočnica Mellifera
Nočnica Mellifera

Posted on

Effortless MongoDB Atlas With Opta

Opta and Mongodb
*Originally posted on Run[x] Blog

Do you use MongoDB Atlas in your application stack? This post shows you how to use Opta to spin up a MongoDB Atlas database and link it to your application.

Introduction

Opta lets you do infrastructure-as-code where you work with High-level constructs instead of getting lost in low level cloud configuration.
MongoDB Atlas simplifies deploying and managing your MongoDB databases while offering the versatility you need to build resilient and performant global applications on the cloud providers of your choice.
Put together, Opta and MongoDB Atlas abstract away the complexity of deploying your applications and MongoDB databases in a production-ready cloud infrastructure. Together, they will smoothen development experience and improve your product’s feature velocity and reliability.
This blog shows you how to implement a movie search application with Atlas Search and deploy it using Opta. It is based on this blog post; but with some notable differences:

  1. Opta handles the creation of the MongoDB Atlas cluster and database, passing its credentials into the application’s code.

  2. Instead of MongoDB Realm, this example uses a simple Flask API backend and deploys it on to a Kubernetes cluster; don’t worry — you won’t have to spin up the Kubernetes cluster yourself, Opta will do it for you in the cloud of your choice or even your local machine!

Atlas is currently supported in Local and AWS Opta environments. Feel free to open a Github issue if you would like to see support in GCP or Azure.

All the code used in this example is available from here.

Let’s get started!

Setup

Install the open-source Opta cli tool by following the Opta installation instructions. If you are new to MongoDB Atlas you can sign up here. The search application described in this blog can be implemented with the MongoDB Atlas free-tier.

(Git) clone the Opta examples repository and use the Opta files in the opta-atlas-mongo-search sub-directory as a starting point. You will need your AWS and MongoDB Atlas API credentials.

You do not need to configure the IP address in the MongoDB Atlas API key, Opta will do that for you.

Prepping the Opta Environment

After you have downloaded these keys from the AWS and Atlas GUIs, set them as environment variables in the terminal shell where you plan to run Opta.

Atlas Mongo

export MONGODB_ATLAS_PUBLIC_KEY=”fakeefghij”
export MONGODB_ATLAS_PRIVATE_KEY=”FAKE015e-4503–4f95-f129–543d4e58bsdg”

AWS

export AWS_ACCESS_KEY_ID=FAKEFTRFFSDQAZA
export AWS_SECRET_ACCESS_KEY=FAKEksfja234sadfbjsdfgsdfg34SAD34fd
Next, we create the Opta environment with the AWS EKS Kubernetes cluster, as documented here. You can also refer to the environment example yaml file in the git repository.

Note: If you are only trying this out on your local machine, append the — local flag to all the opta commands so that a local Kubernetes cluster is spun up on your machine rather than the AWS EKS cluster. For local runs as well, the MongoDB Atlas Cluster will still be spun up in the cloud.

Deploying the Application with Opta

Once we have the Kubernetes cluster, we can create the MongoDB application by running opta apply on the opta-mongodb-atlas-search/opta/atlasmongoservice.yaml file. Remember to set your mongo_atlas_project_id in this file before running Opta.

First, lets create the docker image that contains our application code:

in the opta-examples directory cloned from Github (https://github.com/run-x/opta-examples.git)
docker build -t mongoapp:latest ./opta-atlas-mongo-search
Then,

For aws

opta deploy — image=mongoapp:latest -c opta-examples/opta-atlas-mongo-search/opta/atlasmongoservice.yaml

OR, For local

opta deploy — image=mongoapp:latest — local -c opta-examples/opta-atlas-mongo-search/opta/atlasmongoservice.yaml
After a few minutes, the application will be running against the new MongoDB Atlas cluster.

Sample Data and Atlas Search Index Setup

Before you can use the application, follow steps 1 and 2 in the original MongoDB tutorial in order to load the sample data and set up the search index in the Atlas database (Although you don’t have to create the cluster, Opta will perform that step). After you have created the MongoDB Atlas search index, wait for a few minutes before going to the next step as the index is built in MongoDB Atlas. Opta will also set correct firewall rules inside MongoDB Atlas so that your application’s IP address(s) can connect to the database.
Once you are done, run opta output to get the AWS ELB endpoint url and point your browser to the address (or to http://localhost:8080/ in case you are using Opta Local); You should now be able to search the movie database like in the screenshot below.
Movie Search Sample Screenshot

Movie Search Sample Screenshot

If you want, you can use Opta to add TLS to serve the application over HTTPS on your domain!

Conclusion

And there you have it, a web application (Flask API+SPA) using MongoDB Atlas deployed in minutes with Opta.
We were able to deploy the application to a SOC-2 compliant AWS EKS Kubernetes cluster and spin up an Atlas database that automatically connected into our Flask application. All with a couple of simple Opta infrastructure-as-code yaml files and without diving into the nitty-gritties of low-level terraform AWS constructs. Spend less time wrestling with your infrastructure and more time with your application!

Top comments (0)