Register for the free MongoDB.live developer conference July 13 and 14. Click to register now.
HomeLearnHow-toHow to Easily Pause and Resume MongoDB Atlas Clusters

How to Easily Pause and Resume MongoDB Atlas Clusters

Published: Mar 29, 2021

  • MongoDB
  • Atlas
  • Python
  • ...

By Joe Drumgoole

Rate this article

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 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:

1$ pip install mongodbatlas
2 Collecting mongodbatlas
3 Using cached mongodbatlas-0.2.6.tar.gz (17 kB)
4 ...
5 ...
6 Building wheels for collected packages: mongodbatlas
7 Building wheel for mongodbatlas (setup.py) ... done
8 Created wheel for mongodbatlas: filename=mongodbatlas-0.2.6-py3-none-any.whl size=23583 sha256=d178ab386a8104f4f5100a6ccbe61670f9a1dd3501edb5dcfb585fb759cb749c
9 Stored in directory: /Users/jdrumgoole/Library/Caches/pip/wheels/d1/84/74/3da8d3462b713bfa67edd02234c968cb4b1367d8bc0af16325
10 Successfully built mongodbatlas
11 Installing collected packages: certifi, chardet, idna, urllib3, requests, six, python-dateutil, mongodbatlas
12 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.

1$ atlascli -h
2 usage: atlascli [-h] [--publickey PUBLICKEY] [--privatekey PRIVATEKEY]
3 [-p PAUSE_CLUSTER] [-r RESUME_CLUSTER] [-l] [-lp] [-lc]
4 [-pid PROJECT_ID_LIST] [-d]
5
6 A command line program to list organizations,projects and clusters on a
7 MongoDB Atlas organization.You need to enable programmatic keys for this
8 program to work. See https://docs.atlas.mongodb.com/reference/api/apiKeys/
9
10 optional arguments:
11 -h, --help show this help message and exit
12 --publickey PUBLICKEY
13 MongoDB Atlas public API key.Can be read from the
14 environment variable ATLAS_PUBLIC_KEY
15 --privatekey PRIVATEKEY
16 MongoDB Atlas private API key.Can be read from the
17 environment variable ATLAS_PRIVATE_KEY
18 -p PAUSE_CLUSTER, --pause PAUSE_CLUSTER
19 pause named cluster in project specified by project_id
20 Note that clusters that have been resumed cannot be
21 paused for the next 60 minutes
22 -r RESUME_CLUSTER, --resume RESUME_CLUSTER
23 resume named cluster in project specified by
24 project_id
25 -l, --list List everything in the organization
26 -lp, --listproj List all projects
27 -lc, --listcluster List all clusters
28 -pid PROJECT_ID_LIST, --project_id PROJECT_ID_LIST
29 specify the project ID for cluster that is to be
30 paused
31 -d, --debug Turn on logging at debug level
32
33 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:

#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

A screenshot of the project's Access Manager page.

#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.

A screenshot showing the buttons described above this image

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:

1$ atlascli -l
2 {'id': 'xxxxxxxxxxxxxxxx464d175c',
3 'isDeleted': False,
4 'links': [{'href': 'https://cloud.mongodb.com/api/atlas/v1.0/orgs/599eeced9f78f769464d175c',
5 'rel': 'self'}],
6 'name': 'Open Data at MongoDB'}
7 Organization ID:xxxxxxxxxxxxf769464d175c Name:'Open Data at MongoDB'
8 project ID:xxxxxxxxxxxxd6522bc457f1 Name:'DevHub'
9 Cluster ID:'xxxxxxxxxxxx769c2577a54' name:'DRA-Data' state=running
10 project ID:xxxxxxxxx2a0421d9bab Name:'MUGAlyser Project'
11 Cluster ID:'xxxxxxxxxxxb21250823bfba' name:'MUGAlyser' state=paused
12 project ID:xxxxxxxxxxxxxxxx736dfdcddf Name:'MongoDBLive'
13 project ID:xxxxxxxxxxxxxxxa9a5a04e7 Name:'Open Data Covid-19'
14 Cluster ID:'xxxxxxxxxxxxxx17cec56acf' name:'pre-prod' state=running
15 Cluster ID:'xxxxxxxxxxxxxx5fbfe04313' name:'dev' state=running
16 Cluster ID:'xxxxxxxxxxxxxx779f979879' name:'covid-19' state=running
17 project ID xxxxxxxxxxxxxxxxa132a8010 Name:'Open Data Project'
18 Cluster ID:'xxxxxxxxxxxxxx5ce1ef94dd' name:'MOT' state=paused
19 Cluster ID:'xxxxxxxxxxxxxx22bf6c226f' name:'GDELT' state=paused
20 Cluster ID:'xxxxxxxxxxxxxx5647797ac5' name:'UKPropertyPrices' state=paused
21 Cluster ID:'xxxxxxxxxxxxxx0f270da18a' name:'New-York-Taxi' state=paused
22 Cluster ID:'xxxxxxxxxxxxxx11eab32cf8' name:'demodata' state=running
23 Cluster ID:'xxxxxxxxxxxxxxxdcaef39c8' name:'stackoverflow' state=paused
24 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:

1$ atlascli --project_id xxxxxxxxxxxxxxxxa132a8010 --pause demodata
2 Pausing 'demodata'
3 Paused cluster 'demodata'

To resume the same cluster, do the converse:

1$ atlascli --project_id xxxxxxxxxxxxxxxxa132a8010 --resume demodata
2 Resuming cluster 'demodata'
3 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 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.

Rate this article
MongoDB Icon
  • Developer Hub
  • Documentation
  • University
  • Community Forums

© MongoDB, Inc.