# How to Easily Pause and Resume MongoDB Atlas Clusters

One of the most important things to think about in the cloud is what is burning dollars while you sleep. In the case of MongoDB Atlas, that is your live clusters. The minute you start a cluster (with the exception of our free tier), we start accumulating cost.

If you're using a dedicated cluster—not one of the cheaper, shared cluster types, such as M0, M2 or M5—then it's easy enough to pause a cluster using the Atlas UI, but logging in over 2FA can be a drag. Wouldn't it be great if we could just jump on a local command line to look at our live clusters?

This you can do with a command line tool like `curl`, some programming savvy, and knowledge of the MongoDB Atlas Admin API. But who has time for that? Not me, for sure.

That is why I wrote a simple script to automate those steps. It's now a Python package up on PyPi called mongodbatlas.

You will need Python 3.6 or better installed to run the script. (This is your chance to escape the clutches of 2.x.)

Just run:

``` bash
$ pip install mongodbatlas
  Collecting mongodbatlas
   Using cached mongodbatlas-0.2.6.tar.gz (17 kB)
  ...
  ...
  Building wheels for collected packages: mongodbatlas
   Building wheel for mongodbatlas (setup.py) ... done
   Created wheel for mongodbatlas: filename=mongodbatlas-0.2.6-py3-none-any.whl size=23583       sha256=d178ab386a8104f4f5100a6ccbe61670f9a1dd3501edb5dcfb585fb759cb749c
   Stored in directory: /Users/jdrumgoole/Library/Caches/pip/wheels/d1/84/74/3da8d3462b713bfa67edd02234c968cb4b1367d8bc0af16325
  Successfully built mongodbatlas
  Installing collected packages: certifi, chardet, idna, urllib3, requests, six, python-dateutil, mongodbatlas
  Successfully installed certifi-2020.11.8 chardet-3.0.4 idna-2.10 mongodbatlas-0.2.6 python-dateutil-2.8.1 requests-2.25.0 six-1.15.0 urllib3-1.26.1
```

Now you will have a script installed called `atlascli`. To test the install worked, run `atlascli -h`.

``` bash
$ atlascli -h
  usage: atlascli -h] [--publickey PUBLICKEY] [--privatekey PRIVATEKEY]
                 [-p PAUSE_CLUSTER] [-r RESUME_CLUSTER] [-l] [-lp] [-lc]
                 [-pid PROJECT_ID_LIST] [-d]

  A command line program to list organizations,projects and clusters on a
  MongoDB Atlas organization.You need to enable programmatic keys for this
  program to work. See https://docs.atlas.mongodb.com/reference/api/apiKeys/

  optional arguments:
   -h, --help            show this help message and exit
   --publickey PUBLICKEY
                         MongoDB Atlas public API key.Can be read from the
                         environment variable ATLAS_PUBLIC_KEY
   --privatekey PRIVATEKEY
                         MongoDB Atlas private API key.Can be read from the
                         environment variable ATLAS_PRIVATE_KEY
   -p PAUSE_CLUSTER, --pause PAUSE_CLUSTER
                         pause named cluster in project specified by project_id
                         Note that clusters that have been resumed cannot be
                         paused for the next 60 minutes
   -r RESUME_CLUSTER, --resume RESUME_CLUSTER
                         resume named cluster in project specified by
                         project_id
   -l, --list            List everything in the organization
   -lp, --listproj       List all projects
   -lc, --listcluster    List all clusters
   -pid PROJECT_ID_LIST, --project_id PROJECT_ID_LIST
                         specify the project ID for cluster that is to be
                         paused
   -d, --debug           Turn on logging at debug level

  Version: 0.2.6
```

To make this script work, you will need to do a little one-time setup on your cluster. You will need a [programmatic key for your cluster. You will also need to enable the IP address that the client is making requests from.

There are two ways to create an API key:

-   If you have a single project, it's probably easiest to create a single project API key
-   If you have multiple projects, you should probably create an organization API key and add it to each of your projects.

## Single Project API Key

Going to your "Project Settings" page by clicking on the "three dot" button next your project name at the top-left of the screen and selecting "Project Settings". Then click on "Access Manager" on the left side of the screen and click on "Create API Key". Take a note of the public *and* private parts of the key, and ensure that the key has the "Project Cluster Manager" permission. More detailed steps can be found in the documentation.

## Organization API Key

Click on the cog icon next to your organization name at the top-left of the screen. Click on "Access Manager" on the left-side of the screen and click on "Create API Key". Take a note of the public *and* private parts of the key. Don't worry about selecting any specific organization permissions.

Now you'll need to invite the API key to each of the projects containing clusters you wish to control. Click on "Projects' on the left-side of the screen. For each of the projects, click on the "three dots" icon on the same row in the project table and select "Visit Project Settings" Click on "Access Manager", and click on "Invite to Project" on the top-right. Paste your public key into the search box and select it in the menu that appears. Ensure that the key has the "Project Cluster Manager" permission that it will need to pause and resume clusters in that project.

More detailed steps can be found in the documentation.

## Configuring `atlascli`

The programmatic key has two parts: a public key and a private key. Both of these are used by the `atlascli` program to query the projects and clusters associated with the organization.

You can pass the keys in on the command line, but this is not recommended because they will be stored in the command line history. It's better to store them in environment variables, and the `atlascli` program will look for these two:

-   `ATLAS_PUBLIC_KEY`: stores the public key part of the programmatic key
-   `ATLAS_PRIVATE_KEY`: stores the private part of the programmatic key

Once you have created these environment variables, you can run `atlascli -l` to list the organization and its associated projects and clusters. I've blocked out part of the actual IDs with `xxxx` characters for security purposes:

``` bash
$ atlascli -l
  {'id': 'xxxxxxxxxxxxxxxx464d175c',
  'isDeleted': False,
  'links': {'href': 'https://cloud.mongodb.com/api/atlas/v1.0/orgs/599eeced9f78f769464d175c',
             'rel': 'self'}],
  'name': 'Open Data at MongoDB'}
  Organization ID:xxxxxxxxxxxxf769464d175c Name:'Open Data at MongoDB'
      project ID:xxxxxxxxxxxxd6522bc457f1 Name:'DevHub'
        Cluster ID:'xxxxxxxxxxxx769c2577a54' name:'DRA-Data'                state=running
      project ID:xxxxxxxxx2a0421d9bab Name:'MUGAlyser Project'
        Cluster ID:'xxxxxxxxxxxb21250823bfba' name:'MUGAlyser'              state=paused
      project ID:xxxxxxxxxxxxxxxx736dfdcddf Name:'MongoDBLive'
      project ID:xxxxxxxxxxxxxxxa9a5a04e7 Name:'Open Data Covid-19'
        Cluster ID:'xxxxxxxxxxxxxx17cec56acf' name:'pre-prod'               state=running
        Cluster ID:'xxxxxxxxxxxxxx5fbfe04313' name:'dev'                    state=running
        Cluster ID:'xxxxxxxxxxxxxx779f979879' name:'covid-19'               state=running
      project ID xxxxxxxxxxxxxxxxa132a8010 Name:'Open Data Project'
        Cluster ID:'xxxxxxxxxxxxxx5ce1ef94dd' name:'MOT'                    state=paused
        Cluster ID:'xxxxxxxxxxxxxx22bf6c226f' name:'GDELT'                  state=paused
        Cluster ID:'xxxxxxxxxxxxxx5647797ac5' name:'UKPropertyPrices'       state=paused
        Cluster ID:'xxxxxxxxxxxxxx0f270da18a' name:'New-York-Taxi'          state=paused
        Cluster ID:'xxxxxxxxxxxxxx11eab32cf8' name:'demodata'               state=running
        Cluster ID:'xxxxxxxxxxxxxxxdcaef39c8' name:'stackoverflow'          state=paused
      project ID:xxxxxxxxxxc9503a77fcce0c Name:'Realm'
```

To pause a cluster, you will need to specify the `project ID` and the `cluster name`. Here is an example:

``` bash
$ atlascli --project_id xxxxxxxxxxxxxxxxa132a8010 --pause demodata
  Pausing 'demodata'
  Paused cluster 'demodata'
```

To resume the same cluster, do the converse:

``` bash
$ atlascli --project_id xxxxxxxxxxxxxxxxa132a8010 --resume demodata
  Resuming cluster 'demodata'
  Resumed cluster 'demodata'
```

Note that once a cluster has been resumed, it cannot be paused again for a while.

This delay allows the Atlas service to apply any pending changes or patches to the cluster that may have accumulated while it was paused.

Now go save yourself some money. This script can easily be run from a `crontab` entry or the Windows Task Scheduler.

Want to see the code? It's in this [repo on GitHub.

For a much more full-featured Atlas Admin API in Python, please check out my colleague Matthew Monteleone's PyPI package AtlasAPI.

> If you have questions, please head to our developer community website where the MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB.