Effortless MongoDB Atlas With Opta
Tags: opta, runx, mongodb, kubernetes
Published: 2021-11-30 04:12:12

---
title: Effortless MongoDB Atlas With Opta
published: true
description: 
tags: opta, runx, mongodb, kubernetes
cover image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vu64m6o3p2lo0t60tzf6.png
---
![Opta and Mongodb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vu64m6o3p2lo0t60tzf6.png)
*Originally posted on [Run[x]](https://blog.runx.dev/effortless-mongodb-atlas-with-opta-eeb1ef0c5902) 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](https://docs.opta.dev/) lets you do infrastructure-as-code where you work with High-level constructs instead of getting lost in low level cloud configuration.
[MongoDB Atlas](https://docs.atlas.mongodb.com/) 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](https://www.mongodb.com/developer/how-to/build-movie-search-application/); 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](https://github.com/run-x/opta-examples).

Let’s get started!
##Setup
Install the open-source Opta cli tool by following the [Opta installation instructions](https://docs.opta.dev/installation/). If you are new to MongoDB Atlas you can sign up [here](https://www.mongodb.com/try?utm_source=runx_opta&utm_campaign=pla&utm_method=referral). The search application described in this blog can be implemented with the MongoDB Atlas free-tier.

(Git) clone the Opta examples [repository](https://github.com/run-x/opta-examples) 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](https://docs.atlas.mongodb.com/tutorial/manage-programmatic-access/). 

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](https://docs.opta.dev/getting-started/aws/). 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](https://www.mongodb.com/developer/how-to/build-movie-search-application/#step-1.-spin-up-atlas-cluster-and-load-movie-data) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86tpgb6cbz5gehma9ueh.png)

## Movie Search Sample Screenshot
If you want, you can use Opta to [add TLS](https://docs.opta.dev/tutorials/ingress/) 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!