An Introduction to mongocli, the easiest way to deploy an Atlas cluster!
Tags: mongodb, cloudskills, programming
Published: 2021-09-16 22:26:38

Deploying infrastructure as code has seemed something worthy of the lucky few in the high castle. Yet, once you get pretty familiar with a product you want to stop going through a UI. With tools like [Fig.io](https://dev.to/nicholasoxford/how-fig-improved-my-zsh-experience-fig-autocomplete-tutorial-pt-1-2aj3), using the command line is more accessible than ever. It's now possible that new developers aren't feeling like they are talking into a black-box.

![salon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecn4qi8bbbjlhfk7cmlf.jpeg)
 

Recently, I've been creating a lot of weekend projects. Standing up a cluster in Mongo, creating something kinda cool, hit some snag and then abandon the project. This type of coding is super productive. Anyways, because I've used Atlas so much I am constantly looking for ways to automate away the infrastructure. This is the idea behind [terraform](https://www.terraform.io/) and [chef](https://docs.chef.io/chef_overview/). But this is the code for those mysterious SREs. I am sure I could figure it out, but I am a bit afraid I would end up deploying 7 sharded M400s clusters. 

The Mongo Atlas CLI is a sweet spot between full infrastructure automation and using the UI. After running a simple authentication flow, the world is yours. You can `mongocli atlas quickstart` which will prompt an interactive cluster creation flow. It's probably helpful to do this at least once. Now, it's going to default you to an `M10`. This is probably as much as you need, but you are probably wondering how do I specify what tier I want!

## Getting started

After running
```console
brew install mongocli
```

We need to give our mongocli an authentication configuration. Lets create a pair of API keys to be able to fully configure Mongo Atlas services. To do this, navigate to Mongodb.com and [log in](https://account.mongodb.com/account/login). Select the dropdown: Access Manager --> Organization Access at the top of the page. We are giving `mongocli` more than just the rights to connect to an individual cluster. If you went ahead and tried the `quickstart` command, you would see at the end of the flow, it ran `mongosh` with your new connection string!

![Screen Shot 2021-09-16 at 5.15.44 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xq48hsz2xy28mfmxn0r.png)
 
On the top right, we need to click Create API Key. You will see an input field for the name of our apiKey and what role we will give it. I named my `mongoCli`, and then chose Project Owner for Organization Permissions. While it's possible to give it lower permissions and still be beneficial. I want to transition to start using the UI as little as possible. The next screen will be your public and private api keys. Copy this to a note or VsCode. 

By default, Mongo limits what IP address can access your clusters. This is done at a granular level, or different API keys can have different IP whitelist. Click `Add Access List Entry` and then "Us Current IP Address". Forgetting to whitelist my IP is something we will all get hit with at-least-once. Done.

```console
mongocli config
```

Paste your public, then private key. And we are configured. 

What are you, silly? Clearly you want to deploy to AWS, US-East-1. Real ones know if that one goes down - the world ain't working. You are also reasonably worried about the performance issues you will never have. Let us chose M30, 10 gigs. Even if I'm brave enough to adventure past the sample data and upload my own data. I'm sure my collection would never surpass 10k documents. Still, something just doesn't feel right about choosing M10. We're also going to use 5.0, so we need to pass that into our cluster `create` argument. You can learn more about Mongo Atlas CLI commands [here](https://docs.mongodb.com/mongocli/stable/command/mongocli-atlas/).

```console
mongocli atlas cluster create cliTest2 --provider AWS --region US_EAST_1 --members 3 --tier M30 --mdbVersion 5.0 --diskSizeGB 10
``` 

Replace `<clusterName>` with whatever your heart desires. But keep it close by, I chose cliTest2. If we head back to our Mongo UI, we should see that Mongo is deploying our cluster. This process takes about 7-10 minutes. Once it is done we can retrieve the connection string for our Atlas instance. 
```
mongocli atlas clusters connectionStrings describe cliTest2
```
We can now use that connection string with `mongosh` and start querying our databases. We are just scratching the surface with `mongocli` - almost criminally so. The last thing we need to do to save our butts financially is terminate this cluster. 

```
mongocli atlas clusters delete cliTest2
```

In a few minutes if we return back to our UI. We will see our cluster is now gone. Let me know if you are interested in learning more about the Mongo Atlas CLI.

Nicholas Oxford