How to Easily Pause and Resume MongoDB Atlas Clusters
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?
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
There are two ways to create an 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 .
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.
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.