This tutorial describes how to create and delete an Atlas project and cluster to use as a temporary testing environment during automation script development. Use this workflow to validate scripts against a test cluster without leaving behind resources after you finish testing.
Prerequisites
Before you begin, you must have the following:
An Atlas user account.
An Atlas organization and its organization ID. You can retrieve the organization ID using the atlas organizations list Atlas CLI command.
The Atlas CLI installed and configured. To learn more, see Install or Update the Atlas CLI and Connect from the Atlas CLI.
Procedure
Create a temporary project, cluster, and database user.
Run the following Atlas CLI commands to create a testing environment that you can delete when you are finished testing:
Run the atlas users describe command to return your Atlas user ID. Replace
<userEmail>with the email address that is associated with your Atlas user account.atlas users describe --username <userEmail> Run the atlas projects create command to create a new project named
myTempProjectin your Atlas organization. Replace<orgId>with your organization ID and replace<userId>with the Atlas user ID that you retrieved in the previous step.atlas projects create myTempProject --orgId <orgId> --ownerId <userId> Retrieve the project ID for the project you created from the response. In the following example response, the project ID is
64933bde48add154124e343f.Project '64933bde48add154124e343f' created. Alternatively, you can use the atlas projects list command to find the project ID.
Run the atlas setup command to create an Atlas cluster and database user in your new project. The following command creates an
M10cluster in the AWSUS_EAST_1region, with the temporary usermyTempUser.Replace
<password>with a password for the database user and replace<projectId>with the project ID for the project you created.Important
An
M10cluster is not free. The cluster you create will incur hourly charges until you delete it. For more information about Atlas cluster billing, see the Cluster Configuration Costs documentation.atlas setup --clusterName myTempCluster --provider AWS --region US_EAST_1 --tier M10 --username myTempUser --password <password> --currentIp --skipSampleData --projectId <projectId> --force After Atlas creates the cluster, the Atlas CLI provides the cluster connection string and connects to your cluster through
mongosh.
Set up programmatic authentication to the temporary project.
To run Atlas CLI commands from your automation scripts, you need a way for your automation to authenticate to the Atlas control plane programmatically. One way to do this is to create a service account for your project and add its credentials to your environment:
Create a service account for the temporary project.
Follow the instructions in Get Started with the Atlas Administration API to create a service account for your temporary project. Save the client ID and client secret values for the service account.
Add service account credentials to your test environment.
To enable your script to run Atlas CLI commands without requiring interactive authentication, you must add the service account credentials to your testing environment. You can do this by setting the
MONGODB_ATLAS_CLIENT_IDandMONGODB_ATLAS_CLIENT_SECRETenvironment variables, runningatlas auth loginin the testing environment, or adding the credentials to a named profile and appending--profile <profileName>to each Atlas CLI command in your script.
Delete the temporary project, cluster, and database user.
Run the following Atlas CLI commands to delete the testing environment. This ensures that all resources you created for testing are properly removed and stop incurring charges on your Atlas account:
Run the atlas clusters delete command to delete the temporary cluster and its database users. Replace
<projectId>with the project ID for the temporary project.atlas clusters delete myTempCluster --projectId <projectId> --force Run the atlas projects delete command to delete the temporary project. Replace
<projectId>with the project ID for your temporary project.atlas projects delete <projectId> --force Note
You can't delete the temporary project until the temporary cluster finishes shutting down. If you get an error stating
CANNOT_CLOSE_GROUP_ACTIVE_ATLAS_CLUSTERS, wait five minutes, then run theatlas projects deletecommand again.
You can run the atlas projects list command to confirm successful deletion of the temporary project and cluster. If the temporary project is missing from the list, you successfully deleted both the project and the cluster.