MongoDB Atlas Clusters



A MongoDB Atlas cluster is a schema-less, NoSQL database that is fully managed by MongoDB. Although it is not an AWS-native service, Stacktape seamlessly integrates it into your stacks.

MongoDB Atlas clusters are secure, scalable, and highly available. They have built-in replication and support backups and point-in-time recovery. Each stack that includes a MongoDB Atlas cluster will also create a new MongoDB Atlas project, which ensures isolation between stacks.

Advantages

  • Secure: Your data is protected by multiple layers of security.
  • Scalable: You can scale your cluster up or down as your needs change.
  • Performant: MongoDB Atlas is designed for high performance.
  • Backups and point-in-time recovery: You can restore your data to a previous state in case of an accident.
  • High availability: Your data is replicated across multiple servers to ensure that it is always available.
  • ACID transactions: MongoDB supports multi-document ACID transactions.

Disadvantages

  • Separate billing: Although Stacktape integrates with MongoDB Atlas, you will still need to manage your billing separately.

Provider configuration

To use MongoDB Atlas with Stacktape, you will need to:

  1. Create a MongoDB Atlas account.
  2. Follow our step-by-step guide to get your organizationId, publicKey, and privateKey.
  3. Store your credentials in a secret.
providerConfig:
  mongoDbAtlas:
    privateKey: 'xxxxfa523543fxxxx42543xx'
    publicKey: 'xxxxxxx'
    organizationId: 'xxxxxxxxxxx07a593cbe63dd'

resources:
  myMongoCluster:
    type: 'mongo-db-atlas-cluster'
    properties:
      clusterTier: M2
MongoDbAtlasProviderAPI reference
publicKey

Your MongoDB Atlas public API key.

Type: string

Create API keys in the MongoDB Atlas console under Organization Settings > API Keys.

privateKey

Your MongoDB Atlas private API key. Store as $Secret() for security.

Type: string

Create API keys in the MongoDB Atlas console under Organization Settings > API Keys.

organizationId

Your MongoDB Atlas Organization ID.

Type: string

Found in the MongoDB Atlas console under Organization Settings.

accessibility

Network connectivity settings for all MongoDB Atlas clusters in this stack.

Type: MongoDbAtlasAccessibility

Stacktape auto-creates a MongoDB Atlas Project for your clusters. These accessibility settings apply at the project level — all clusters in the stack share the same network config.

Basic usage

resources:
  myMongoDbCluster:
    type: mongo-db-atlas-cluster
    properties:
      clusterTier: M2
  myLambda:
    type: function
    properties:
      packaging:
        type: stacktape-lambda-buildpack
        properties:
          entryfilePath: path/to/my/lambda.ts
      environment:
        - name: MONGODB_CONNECTION_STRING
          value: $ResourceParam('myMongoDbCluster', 'connectionString')
      connectTo:
        - myMongoDbCluster
import { MongoClient } from 'mongodb';

const client = new MongoClient(process.env.MONGODB_CONNECTION_STRING);

const handler = async (event, context) => {
  await client.connect();

  const db = client.db('mydb');

  await db.collection('posts').insertOne({
    title: 'My first post',
    content: 'Hello!'
  });

  const post = await db.collection('posts').findOne({ title: 'My first post' });

  await client.close();
};

Cluster tier

The cluster tier determines the resources (memory, storage, IOPS) for each data-bearing node in your cluster. To learn more, see the MongoDB Atlas documentation.

resources:
  myMongoCluster:
    type: 'mongo-db-atlas-cluster'
    properties:
      clusterTier: M2

Disk size

Each cluster tier comes with a default amount of storage. All M10+ clusters automatically scale their storage, but you can disable this behavior. You can also customize the storage capacity for all M10+ clusters.

resources:
  myMongoCluster:
    type: mongo-db-atlas-cluster
    properties:
      clusterTier: M2
      diskSizeGB: 60

Auto-scaling

Scales up when CPU or memory exceeds 75% for 1 hour. Scales down when both are below 50% for 24 hours.

resources:
  myMongoDbCluster:
    type: mongo-db-atlas-cluster
    properties:
      clusterTier: M10
      autoScaling:
        minClusterTier: M10
        maxClusterTier: M30
        disableDiskScaling: true
        disableScaleDown: true
MongoDbAutoScalingAPI referenceParent:MongoDbAtlasCluster
minClusterTier

Lowest tier the cluster can scale down to. Prevents unexpected cost increases from always scaling up.

Type: string ENUM

Possible values: M10M100M140M20M200M200 Low-CPU (R200)M200_NVMEM30M300M300 Low-CPU (R300)M40M40 Low-CPU (R40)M400 Low-CPU (R400)M400_NVMEM40_NVMEM50M50 Low-CPU (R50)M50_NVMEM60M60 Low-CPU (R60)M60_NVMEM700 Low-CPU (R700)M80M80 Low-CPU (R80)M80_NVME

maxClusterTier

Highest tier the cluster can scale up to. Set a ceiling to control maximum costs.

Type: string ENUM

Possible values: M10M100M140M20M200M200 Low-CPU (R200)M200_NVMEM30M300M300 Low-CPU (R300)M40M40 Low-CPU (R40)M400 Low-CPU (R400)M400_NVMEM40_NVMEM50M50 Low-CPU (R50)M50_NVMEM60M60 Low-CPU (R60)M60_NVMEM700 Low-CPU (R700)M80M80 Low-CPU (R80)M80_NVME

disableDiskScaling

Prevent automatic disk expansion. By default, storage grows when usage hits 90%. Storage never scales down.

Type: boolean

disableScaleDown

Prevent automatic scale-down. The cluster can only scale up, never back down to a smaller tier.

Type: boolean

Sharding

Number of shards. More than 1 enables sharded mode for horizontal scaling. Requires M30+.

resources:
  myMongoCluster:
    type: mongo-db-atlas-cluster
    properties:
      clusterTier: M30
      numShards: 3

Backups

Enable daily snapshots (18:00 UTC). M10+ only — M2/M5 get automatic snapshots with different rules.

There are different types of snapshots with different retention periods and frequencies:

  • Hourly: Every 6 hours, retained for 2 days.
  • Daily: Every day, retained for 7 days.
  • Weekly: Every Saturday, retained for 4 weeks.
  • Monthly: Last day of the month, retained for 12 months.
resources:
  myMongoDbCluster:
    type: mongo-db-atlas-cluster
    properties:
      clusterTier: M10
      enableBackups: true

Point-in-time recovery

Restore to any second within the last 7 days. Requires enableBackups: true and M10+.

resources:
  myMongoDbCluster:
    type: mongo-db-atlas-cluster
    properties:
      clusterTier: M10
      enablePointInTimeRecovery: true

Accessing clusters from workloads

You can grant a compute resource permission to access a cluster by listing it in the resource's connectTo property. This will inject the necessary credentials into the resource.


resources:
  myMongoCluster:
    type: 'mongo-db-atlas-cluster'
    properties:
      clusterTier: M2

  myMongoFunction:
    type: function
    properties:
      packaging:
        type: stacktape-lambda-buildpack
        properties:
          entryfilePath: 'lambdas/mongo-lambda.ts'
      memory: 512
      # by allowing access to cluster, lambda receives permissions for reading and writing into cluster databases
      connectTo:
        - myMongoCluster
      environment:
        # injecting the connection string as environment variable
        - name: MONGODB_CONNECTION_STRING
          value: $ResourceParam('myMongoCluster', 'connectionString')

Accessibility

You can restrict access to your cluster to specific resources or IP addresses. Unlike other database resources, accessibility is set globally in the provider configuration, not on the resource itself. This means that all clusters in a given stack will have the same accessibility mode.

Shared cluster tiers (M2 and M5) only support internet accessibility mode.

MongoDbAtlasAccessibilityAPI referenceParent:MongoDbAtlasProvider
accessibilityModeDefault: internetRequired

Network access mode.

Type: string ENUM

Possible values: internetscoping-workloads-in-vpcvpcwhitelisted-ips-only

  • internet: Accessible from anywhere (credentials still required).
  • vpc: Only from resources in your VPC + any whitelistedIps.
  • scoping-workloads-in-vpc: Like vpc, but also requires security-group access via connectTo.
  • whitelisted-ips-only: Only from IP addresses listed in whitelistedIps.
whitelistedIps

IP addresses or CIDR ranges allowed to access the cluster (e.g., your office IP).

Type: Array of string

No effect in internet mode. In vpc/scoping-workloads-in-vpc, adds access for IPs outside the VPC. In whitelisted-ips-only, these are the only IPs that can connect.

Internet mode

This is the default and least restrictive mode. The cluster can be accessed from anywhere on the internet. However, your cluster is still protected by IAM permissions.

VPC mode

In addition to IAM permissions, the cluster is also protected at the network level. This means that only resources within your stack's default VPC can access the cluster. Any IP addresses in the whitelistedIps list can also access the cluster. Traffic between your stack's resources and the cluster never leaves the AWS network, which is more secure and can be cheaper.

providerConfig:
  mongoDbAtlas:
    privateKey: 'xxxxfa523543fxxxx42543xx'
    publicKey: 'xxxxxxx'
    # "accessibility" option is shared between "mongo-db-atlas-cluster" resources of your stack
    accessibility:
      accessibilityMode: vpc
    organizationId: 'xxxxxxxxxxx07a593cbe63dd'

resources:
  myMongoCluster:
    type: mongo-db-atlas-cluster
    properties:
      clusterTier: M10

Scoped VPC mode

This mode is similar to VPC mode, but even more restrictive. In this mode, the connectTo property not only grants IAM permissions, but also grants access at the network level.

providerConfig:
  mongoDbAtlas:
    privateKey: 'xxxxfa523543fxxxx42543xx'
    publicKey: 'xxxxxxx'
    # "accessibility" option is shared between "mongo-db-atlas-cluster" resources of your stack
    accessibility:
      accessibilityMode: scoping-workloads-in-vpc
    organizationId: 'xxxxxxxxxxx07a593cbe63dd'

resources:
  # functionOne does NOT have access to database eventhough it is joined in vpc
  functionOne:
    type: function
    properties:
      packaging:
        type: stacktape-lambda-buildpack
        properties:
          entryfilePath: 'path/to/my-lambda.ts'
      joinDefaultVpc: true

  # functionTwo does have access to database, because it is scoping the database in connectTo list
  functionTwo:
    type: function
    properties:
      packaging:
        type: stacktape-lambda-buildpack
        properties:
          entryfilePath: 'path/to/my-lambda-2.ts'
      joinDefaultVpc: true
      connectTo:
        - myMongoCluster

  myMongoCluster:
    type: mongo-db-atlas-cluster
    properties:
      clusterTier: M10

Whitelisted IPs only mode

The cluster can only be accessed from the IP addresses and CIDR blocks in the whitelistedIps list.

providerConfig:
  mongoDbAtlas:
    privateKey: 'xxxxfa523543fxxxx42543xx'
    publicKey: 'xxxxxxx'
    # "accessibility" option is shared between "mongo-db-atlas-cluster" resources of your stack
    accessibility:
      accessibilityMode: whitelisted-ips-only
      whitelistedIps:
        - 193.12.16.4
    organizationId: 'xxxxxxxxxxx07a593cbe63dd'
resources:
  myMongoCluster:
    type: mongo-db-atlas-cluster
    properties:
      clusterTier: M10

Admin user

Not required for app-to-database access via connectTo — that's handled automatically.

MongoDbAdminUserCredentialsAPI referenceParent:MongoDbAtlasCluster
userNameRequired

Name of the admin user

Type: string

passwordRequired

Password for the admin user

Type: string

resources:
  myMongoCluster:
    type: 'mongo-db-atlas-cluster'
    properties:
      clusterTier: M2
      adminUserCredentials:
        userName: my-master-user
        password: $Secret('mongo-master-password')

Business Intelligence Connector for SQL

The MongoDB Connector for BI allows you to use SQL to query your MongoDB data with tools like Tableau, Power BI, and Excel.

MongoDbBiConnectorAPI referenceParent:MongoDbAtlasCluster
enabledRequired

Enable the BI Connector for SQL-based access.

Type: boolean

readPreference

Which node type the BI Connector reads from. Use analytics to avoid impacting production queries.

Type: string ENUM

Possible values: analyticsprimarysecondary


resources:
  myMongoCluster:
    type: 'mongo-db-atlas-cluster'
    properties:
      clusterTier: 'M10'
      biConnector:
        enabled: true

Referenceable parameters

The following parameters can be easily referenced using $ResourceParam directive directive.

To learn more about referencing parameters, refer to referencing parameters.

connectionString
  • Connection string (URL) that allows connecting to the cluster.

  • Usage: $ResourceParam('<<resource-name>>', 'connectionString')

Pricing

You are charged for:

  • Cluster tier: Starting at $8.60/month for an M2 shared cluster.
  • Data transfer: For most customers, data transfer fees are less than 10% of their bill. There are no data transfer fees for M0, M2, and M5 clusters.

For a more detailed overview, see the MongoDB documentation.

API reference

MongoDbReplicationAPI referenceParent:MongoDbAtlasCluster
numAnalyticsNodes

Read-only nodes for long-running analytics queries. Prevents impact on primary workload performance.

Type: number

numElectableNodesDefault: 3

Nodes that can become primary. More = better redundancy. Must be odd.

Type: number ENUM

Possible values: 357

numReadOnlyNodes

Read-only replica nodes for scaling read throughput.

Type: number

MongoDbAtlasClusterAPI reference
typeRequired
No description

Type: string "mongo-db-atlas-cluster"

properties.clusterTierRequired

Instance size. M2/M5 = shared (cheapest). M10+ = dedicated (more features, auto-scaling, backups).

Type: string ENUM

Possible values: M10M100M140M2M20M200M200 Low-CPU (R200)M200_NVMEM30M300M300 Low-CPU (R300)M40M40 Low-CPU (R40)M400M400 Low-CPU (R400)M400_NVMEM40_NVMEM5M50M50 Low-CPU (R50)M50_NVMEM60M60 Low-CPU (R60)M60_NVMEM700M700 Low-CPU (R700)M80M80 Low-CPU (R80)M80_NVME

properties.diskSizeGB

Disk size in GB. Not available for shared tiers (M2/M5). M10+ auto-scales storage by default.

Type: number

properties.versionDefault: 7.0

MongoDB engine version.

Type: string ENUM

Possible values: 5.06.07.0

properties.numShardsDefault: 1

Number of shards. More than 1 enables sharded mode for horizontal scaling. Requires M30+.

Type: number

properties.replication

Node count configuration: electable, read-only, and analytics nodes. Default: 3 electable nodes.

Type: MongoDbReplication

properties.enableBackups

Enable daily snapshots (18:00 UTC). M10+ only — M2/M5 get automatic snapshots with different rules.

Type: boolean

properties.enablePointInTimeRecovery

Restore to any second within the last 7 days. Requires enableBackups: true and M10+.

Type: boolean

properties.biConnector

BI Connector for SQL-based access to MongoDB data. CPU-intensive — may degrade M10/M20 performance.

Type: MongoDbBiConnector

properties.autoScaling

Auto-scale tier and/or storage based on CPU/memory usage. Set min/max tier to control costs.

Type: MongoDbAutoScaling

Scales up when CPU or memory exceeds 75% for 1 hour. Scales down when both are below 50% for 24 hours.

properties.adminUserCredentials

Admin user for direct database access (e.g., from your local machine or admin tools).

Type: MongoDbAdminUserCredentials

Not required for app-to-database access via connectTo — that's handled automatically.