This project enables you to build your own automated MongoDB Atlas Billing Dashboard. It provides the code referenced in my MongoDB.live 2020 talk ("Tracking and Managing Your Spend on MongoDB Atlas"), including the extended functions referenced towards the end of the presentation.
The code in this repo contains several functions, values & secrets and triggers as follow.
-
getdata
: a simple function to retrieve the pending invoice for a single Org. -
getdata2
: a more complex set of functions to retrieve the pending invoice for the given Org, unwinding the output data into a new collection. -
getdata3
: an evolution of the above code to map org & project names as well as reshape the output documents -
getdata4
: a more complex function which retrieves all invoice data (pending and historic), applies any new data and has additional categorization of the data to enhance the output in MongoDB Charts. -
modifyClusters
: a generic function to modify a set of clusters (referenced by theauto-clusters
Value). -
pause
: a function with leverages themodifyClusters
function to pause the referenced clusters. -
unpause
: a function with leverages themodifyClusters
function to unpause (resume) the referenced clusters.
-
billing-org
: maps to the Org Id we want to gather Billing data from. Maps tobilling-orgSecret
. -
billing-username
: maps to the Public API key for the Org we want to gather Billing data from. Maps tobilling-usernameSecret
. -
billing-password
: maps to the Private API key for the Org we want to gather Billing data from. Maps tobilling-passwordSecret
. -
auto-project
: maps to the Project Id of the Project where we want to control clusters. Maps toauto-projectSecret
. -
auto-username
: maps to the Public API key for the Project where we want to control clusters. Maps toauto-usernameSecret
. -
auto-password
: maps to the Private API key for the Project where we want to control clusters. Maps toauto-passwordSecret
. -
auto-clusters
: an array of cluster names we want to modify via the 'pause' and 'unpause' functions define above.
getdataTrigger
: runs at 4am GMT each morning to retrieve the billing data using thegetdata4
function above.pauseTrigger
: runs at 8pm GMT MON-FRI to pause the clusters listed in theauto-clusters
Value above.unpauseTrigger
: runs at 8am GMT MON-FRI to unpause (resume) the clusters listed in theauto-clusters
Value above.
You will need the following before you can use this code:
- A MongoDB Atlas cluster (an M0 cluster will do). This will be used to store the billing data we gather. (To minimize network data transfer create this M0 cluster on AWS, in the 'us-east-1', 'us-west-2', 'eu-east-1' or 'ap-southeast-2' regions).
- A local clone of this repo so you can import it into your MongoDB Stitch application.
To deploy the code in this repo you'll need several API Keys.
The main API key will be used to retrieve the Billing data from the target Organization.
The second key can be used to enable Automation for specific clusters in your Orgs/Projects (allowing you to pause/unpause the clusters on a schedule). Note you can use a single Org-level key for both functions if you want to automate clusters in the same Org you wish to retrieve the billing data for. If the clusters are in a different Org you will need two separate keys.
An additional key will be required to import the code in this repo into your own Stitch application.
You will need to create an API in the target organization (the one you want to gather Billing data for).
- Navigate to the target org.
- Click on the 'Access Manager' on the left navigation bar.
- Select the 'API Keys' tab.
- Create an API key by clicking the 'Create API Key' button on the top right of the page.
- Note the Public Key details.
- Give the API key a suitable description.
- Add the following permissions to your key:
Organization Billing Admin
andOrganization Read Only
. - Click 'Next'.
- Record the Private Key details and store them securely.
- Add a Whitelist Entry for the API key if required.
- Click 'Done' when you're ready to save the new key to your Organization.
Before moving on we will record the Organization ID:
- Navigate to the 'Organzation Settings' by clicking the cog to the right of the organization name (on the top left of the window).
- Select 'Settings' on the left navigation bar.
- Select 'General Settings'.
- Record the 'Organization ID'.
The code in this repo also includes functions to pause/unpause clusters. This functionality requires a key with additional roles.
You can create an Organization level key in order to control all of the clusters in each project in that Org. To do so you can repeat the steps above and add the Organization Owner
role for the Org in question. For our purposes we'll just create a Project level key which restricts the key to just that project rather than the whole Org.
- Navigate to the Project you wish to enable for automation.
- Click the 3 vertical dots to the right of the project name (on the top left of the window).
- Select 'Access Manager' on the left navigation bar.
- Select the 'API Keys' tab.
- Create a new API Key with the
Project Cluster Manager
role, following the same steps as above (for the Billing API Key, step 4 onwards).
Before moving on we will record the Project ID:
- Navigate back to the 'Project Settings' if required by clicking the the 3 vertical dots to the right of the project name (on the top left of the window).
- Select 'Settings' on the left navigation bar.
- Record the 'Project ID' from the 'Project Settings' tab.
To import the code in this repo into your own Stitch app you will need an additional Project-level API key associated with the Project where your Billing cluster resides.
- Navigate to the Project where you created the MongoDB cluster to store the Billing data.
- Create a new API key following the steps above but with the
Project Owner
role.
Follow the instructions on this page to download the Stitch CLI for your platform.
- Log in via the Stitch CLI using the details of the key created above:
stitch-cli login --api-key=my-api-key --private-api-key=my-private-api-key
- From the root of the local github clone run the following to create a new Stitch App:
stitch-cli import
Answer the questions e.g.:
- this app does not exist yet: would you like to create a new app? [y/n]:
y
- App name:
billing
- Available projects: <select your Atlas cluster from above>
- Location [US-VA]: <select the location where your Atlas cluster resides>
- Deployment Model [GLOBAL]:
LOCAL
Note: this is expected to fail with an error message similar to the follow, as some Secrets have not yet been created:
failed to import app: error: error validating Value: auto-password: could not find secret "auto-passwordSecret"
While the previous command failed, it did create a new Stitch App. Navigate back to the Stitch App page (refresh if required) and select the new billing
App.
With the Stitch App selected we can create the missing Secrets:
- Switch to
Values & Secrets
on the left navigation bar - Click on the
Secrets
tab. - Create the following Secrets mapped to the values from above:
billing-orgSecret
: the ID of the Org we want to retrieve the Billing data for.billing-usernameSecret
: the Public API key details for that Org.billing-passwordSecret
: the Private API key details for that Org.auto-projectSecret
: the ID of the Project we want to target when pausing/unpausing clusters.auto-usernameSecret
: the Public API key details for that Project.auto-passwordSecret
: the Private API key details for that Project.
Now that we have our Secrets in place we can redeploy our App:
stitch-cli import --strategy=replace
Select 'y
' to confirm you want to repace the existing application.
Now that the App has been redeployed, verify that the App is linked to your Atlas cluster:
- Switch to 'Clusters' on the left navigation bar.
- Ensure the 'Atlas Clusters' entry maps to your Atlas cluster from above.
If not, select the elipsis on the right for the 'mongodb-atlas' Service and select 'Edit Cluster Configuration'
- Select your Atlas cluster in the 'Atlas Cluster' drop-down.
- All other entries can be left as is.
- Click 'Save' to save your choice.
- Click the 'Review & Deploy Changes' option from the new blue bar at the top of the screen.
- Verify the changes in the resulting dialog and click 'Deploy' to deploy and make live your changes.
Now that we've deployed our code you can test it interactively.
- Navigate to 'Functions' from the left navigation bar.
- Select the 'getdata' function.
- Click the 'Run' button at the bottom of the screen.
All going well, the function should complete successfully and populate the billingdata
collection in the billing
database of your Atlas cluster.
If anthing goes wrong, check the error message and make sure you have entered the values of the Secrets correctly (you can update them at any stage by navigating to Values & Secrets
on the left navigation bar, choosing the Secrets
tab and updating each entry as required).
Now that the code is installed and validated you should review the following:
- Update the list of clusters in the
auto-clusters
Value to map to the clusters you want to pause/unpase on a schedule. - Update the time for each Trigger such that it runs when you want.
Once the Triggers are set up correctly and data is populating on a daily basis you can switch to MonogDB Charts to visualize the data as per the MongoDB.live presentation.
Additional enhancements are possible such as:
- Adding a Webhook to provide an endpoint allowing you to pause/unpase specific clusters on demand.
- Extend the Billing code to retrieve data from multiple MongoDB Atlas Orgs.
If there are any errors or corrections required for this repo please feel free to send me a pull-request.