How to Easily Pause and Resume MongoDB Atlas Clusters
Joe DrumgoolePublished Nov 30, 2021 • Updated Sep 23, 2022
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 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.)
Now you will have a script installed called
atlascli. To test the install worked, run
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.
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.
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.
The programmatic key has two parts: a public key and a private key. Both of these are used by the
atlascliprogram 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
atlascliprogram 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 -lto list the organization and its associated projects and clusters. I've blocked out part of the actual IDs with
xxxxcharacters for security purposes:
To pause a cluster, you will need to specify the
project IDand the
cluster name. Here is an example:
To resume the same cluster, do the converse:
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
crontabentry 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.