EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

How to Easily Pause and Resume MongoDB Atlas Clusters

Joe Drumgoole5 min read • Published Nov 30, 2021 • Updated Sep 23, 2022
Atlas
Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
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:
Now you will have a script installed called atlascli. To test the install worked, run atlascli -h.
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:
To pause a cluster, you will need to specify the project ID and 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 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.

Facebook Icontwitter iconlinkedin icon
Rate this article
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

How to Build Serverless Applications with SST and MongoDB Atlas


Jan 23, 2024 | 3 min read
Tutorial

Using OpenAI Latest Embeddings In A RAG System With MongoDB


Feb 01, 2024 | 15 min read
Tutorial

MongoDB Atlas With Terraform - Cluster and Backup Policies


Mar 04, 2024 | 22 min read
Code Example

Blogue


Jul 07, 2022 | 1 min read
Table of Contents
  • Single Project API Key
  • Organization API Key