How To Pause and Resume Atlas Clusters

Joe Drumgoole


One of the most important things to think about in the Cloud is what is burning dollars while I 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.

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 the local command line to look at our live clusters?

This you can do with a command line too like curl, some programming savvy, and knowledge of the MongoDB Atlas API. But who has time for that? Not me for sure. That is why I wrote a simple script to automate those simple 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:

$ 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 ( ... 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 script installed called atlascli. To test the install worked run atlascli -h.

$ 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

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
                        pause named cluster in project specified by project_id
                        Note that clusters that have been resumed cannot be
                        paused for the next 60 minutes
                        resume named cluster in project specified by
  -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 the cluster that is to be
  -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.

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 not recommended because they will be stored in the command line history. 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 ID's with xxxx characters for security purposes:

$ atlascli -l
{'id': 'xxxxxxxxxxxxxxxx464d175c',
 'isDeleted': False,
 'links': [{'href': '',
            '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 resume a cluster you will need to specify the project ID and the cluster name. Here is an example:

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

To resume the same cluster do the converse:

JD10Gen:GIT jdrumgoole$ 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? its in this repo on github.

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