MongoDB Atlas CLI

repository·master·Indexed 41 days ago

https://github.com/mongodb/mongodb-atlas-cli

A modern command-line interface for managing MongoDB Atlas deployments directly from a terminal. The CLI, invoked via the `atlas` binary, allows users to authenticate, manage IP access lists (create, delete, describe, and list), retrieve cluster access logs, and handle configuration through profiles and environment variables.

Tokens
116K
Snippets
320
Records
404
Agent score
52%

What's inside MongoDB Atlas CLI

  1. Install the MongoDB Atlas CLI

    master

    You can install the Atlas CLI using Homebrew, by downloading pre-built binaries, or by building from source.

    Homebrew

    On macOS or Linux systems with Homebrew installed, run:

    brew install mongodb-atlas-cli

    Pre-built Binaries

    Download the appropriate version for your platform from the Atlas CLI releases. You can run the binary from any location without a global installation, which is useful for shared hosts where you lack privileged access. For ease of use, place the binary in a directory included in your PATH, such as /usr/local/bin.

    Build From Source

    To build the CLI from the source code, follow these steps:

    1. Fetch Source:
      git clone https://github.com/mongodb/mongodb-atlas-cli.git
      cd mongodb-atlas-cli
    2. Build: Run make build to generate the atlas binary in the ./bin directory.
    3. Install: Run make install to install the atlas binary into $GOPATH/bin. Note that make build is not required before running make install.
    brew install mongodb-atlas-cli
  2. Authenticate using a Service Account in Atlas CLI

    master

    You can authenticate an Atlas CLI profile using a Service Account (also known as an OAuth Application) to enable programmatic access via a Client ID and Client Secret. This is ideal for automation and CI/CD workflows.

    Interactive Authentication

    To set up a profile interactively:

    1. Run atlas auth login --profile <profile_name>.
    2. Select ServiceAccount from the authentication type prompt.
    3. Provide your Client ID and Client Secret when prompted.
    4. Select a default project and a default output format (e.g., plaintext or json).

    Programmatic Authentication (Non-interactive)

    For CI/CD or scripting where interactive prompts are not possible, set the following environment variables before running Atlas CLI commands:

    • MONGODB_ATLAS_CLIENT_ID
    • MONGODB_ATLAS_CLIENT_SECRET

    Using the Profile

    Once authenticated, use the --profile flag with any Atlas CLI command to execute it within the scope of that profile's permissions.

    # Interactive setup
    atlas auth login --profile myProfile
    
    # Using the profile
    atlas cluster list --profile myProfile
    
    # Programmatic setup (Environment Variables)
    export MONGODB_ATLAS_CLIENT_ID=<your-client-id>
    export MONGODB_ATLAS_CLIENT_SECRET=<your-client-secret>
    atlas cluster list
  3. Configure shell completions for Atlas CLI

    master

    If you installed via Homebrew, shell completions are configured automatically. For other installation methods, you can generate completion scripts for your preferred shell (bash, zsh, fish, or powershell). To see the specific instructions and flags for your shell, run:

    atlas completion <bash|zsh|fish|powershell> --help
    atlas completion <bash|zsh|fish|powershell> --help
  4. Rename a MongoDB Atlas cluster

    master

    There is no direct command to rename an existing Atlas cluster. To achieve a rename, you must deploy a new cluster with the desired name using the configuration of the old cluster, and then delete the old one.

    Warning: This process does not migrate data from the old cluster to the new cluster. You must handle data migration separately.

    # 1. Export the configuration of the existing cluster to a JSON file
    atlas cluster describe oldName --output json > oldcluster.json
    
    # 2. Create a new cluster using that configuration file with a new name
    atlas cluster create newName --file oldcluster.json
    
    # 3. Delete the original cluster
    atlas cluster delete oldName --force
  5. Configure Atlas CLI via Profiles and Environment Variables

    master

    The Atlas CLI manages configuration through profiles and environment variables. The CLI automatically performs a migration of your configuration to the latest version upon startup using the DefaultMigrator.

    Configuration is loaded via config.LoadAtlasCLIConfig(), which merges settings from the configuration file and any applicable environment variables. The active configuration is then injected into the command execution context.

  6. Restore method flag requirements

    master

    The atlas backup restore start command enforces different required flags depending on the restore method selected:

    • automated: Requires --clusterName, --snapshotId, --targetClusterName, and --targetProjectId.
    • pointInTime: Requires --clusterName, --targetClusterName, and --targetProjectId.
    • download: Requires --snapshotId.

    Note: --clusterName is always required.

  7. Configure cluster creation options

    master

    When creating a new MongoDB Atlas cluster via the CLI, you can specify several configuration options to define the cluster's topology, hardware, and metadata.

    Key configuration parameters include:

    • Cluster Name: The unique identifier for your cluster.
    • Tier: The instance size (e.g., M10, M30) which determines the hardware specifications.
    • Region: The cloud provider region where the cluster will be deployed.
    • Provider: The cloud provider (e.g., AWS, GCP, Azure).
    • MongoDB Major Version: The version of MongoDB to run (e.g., 6.0, 7.0). If not specified, the CLI uses a default version.
    • Disk Size: The disk size in GB. This is automatically calculated based on the provider and tier if not explicitly overridden.
    • Tags: Key-value pairs used to organize and filter resources.
    • Termination Protection: A boolean flag to prevent accidental deletion of the cluster.

    Note: For non-tenant clusters, the default node count is 3 and the default priority is 7. For tenant clusters, the Provider option is used as the BackingProviderName.

  8. Run the MongoDB Atlas CLI

    master

    The MongoDB Atlas CLI is invoked via the atlas binary. It uses a command-based structure (built with Cobra) to interact with MongoDB Atlas services. When running commands, the CLI automatically handles configuration loading, profile management, and telemetry tracking.

    To learn more about connecting to Atlas via the CLI, visit the official documentation: https://www.mongodb.com/docs/atlas/cli/stable/connect-atlas-cli/

    # Example of running the CLI (actual commands depend on the installed version)
    atlas --help