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:
- Create a MongoDB Atlas account.
- Follow our step-by-step guide to get your
organizationId
,publicKey
, andprivateKey
. - Store your credentials in a secret.
providerConfig:mongoDbAtlas:privateKey: 'xxxxfa523543fxxxx42543xx'publicKey: 'xxxxxxx'organizationId: 'xxxxxxxxxxx07a593cbe63dd'resources:myMongoCluster:type: 'mongo-db-atlas-cluster'properties:clusterTier: M2
Basic usage
resources:myMongoDbCluster:type: mongo-db-atlas-clusterproperties:clusterTier: M2myLambda:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: path/to/my/lambda.tsenvironment:- name: MONGODB_CONNECTION_STRINGvalue: $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-clusterproperties:clusterTier: M2diskSizeGB: 60
Auto-scaling
- You can configure your cluster to automatically scale its cluster tier, storage capacity, or both based on the cluster usage.
- To help control the costs, you can select a range of cluster tiers to which your cluster can scale to.
- Cluster is
scaled up
(to the next tier), if one the following criteria is met:- Average CPU Utilization has exceeded 75% for the past hour
- Memory Utilization has exceeded 75% for the past hour
- Cluster is
scaled down
(to the lower tier), if both of the following criteria are met:- The average CPU Utilization and Memory Utilization over the past 24 hours is below 50%
- The cluster has not been scaled down (manually or automatically) in the past 24 hours
resources:myMongoDbCluster:type: mongo-db-atlas-clusterproperties:clusterTier: M10autoScaling:minClusterTier: M10maxClusterTier: M30disableDiskScaling: truedisableScaleDown: true
- If you configure more than 1 shard, the cluster will run in a sharded mode.
- Sharding distributes data across multiple physical machines enabling horizontal scaling.
- Sharded mode is available only for cluster tiers M30 or higher.
- To learn more about sharding, refer to MongoDb Docs.
resources:myMongoCluster:type: mongo-db-atlas-clusterproperties:clusterTier: M30numShards: 3
Backups
- Backups are copies of your data that encapsulate the state of your cluster at a given time. Backups provide a safety measure in the event of a data loss.
- The default snapshot time is every day at 18:00 UTC.
- Available only in M10+ Clusters.
- Snapshots are automatically taken even for M2/M5 clusters, but have different properties. To learn more, refer to [M2 and M5 backups docs](https://docs.atlas.mongodb.com/backup-restore-cluster/#m2
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-clusterproperties:clusterTier: M10enableBackups: true
Point-in-time recovery
- Enables Continuous Cloud Backups, which replay the oplog (history of ordered logical writes) and enables you to restore a cluster from a particular point in time.
- You can make a point in time recovery to any point within the last 7 days.
- Available only in M10+ Clusters.
- If you enable point-in-time recovery, you must also enable backups.
- MongoDb Continuous cloud backup includes additional charges. To learn more, refer to MongoDb Continuous cloud backup pricing Docs
resources:myMongoDbCluster:type: mongo-db-atlas-clusterproperties:clusterTier: M10enablePointInTimeRecovery: 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: M2myMongoFunction:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: 'lambdas/mongo-lambda.ts'memory: 512# by allowing access to cluster, lambda receives permissions for reading and writing into cluster databasesconnectTo:- myMongoClusterenvironment:# injecting the connection string as environment variable- name: MONGODB_CONNECTION_STRINGvalue: $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.
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 stackaccessibility:accessibilityMode: vpcorganizationId: 'xxxxxxxxxxx07a593cbe63dd'resources:myMongoCluster:type: mongo-db-atlas-clusterproperties: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 stackaccessibility:accessibilityMode: scoping-workloads-in-vpcorganizationId: 'xxxxxxxxxxx07a593cbe63dd'resources:# functionOne does NOT have access to database eventhough it is joined in vpcfunctionOne:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: 'path/to/my-lambda.ts'joinDefaultVpc: true# functionTwo does have access to database, because it is scoping the database in connectTo listfunctionTwo:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: 'path/to/my-lambda-2.ts'joinDefaultVpc: trueconnectTo:- myMongoClustermyMongoCluster:type: mongo-db-atlas-clusterproperties: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 stackaccessibility:accessibilityMode: whitelisted-ips-onlywhitelistedIps:- 193.12.16.4organizationId: 'xxxxxxxxxxx07a593cbe63dd'resources:myMongoCluster:type: mongo-db-atlas-clusterproperties:clusterTier: M10
Admin user
- Optionally, you can create an admin database user user with administrative access privileges.
- Accessing the cluster from your compute resources (batch-jobs, multi-container-workloads, functions or container based services), is possible even without creating this user.
- Creating an admin user can be useful for performing administrative tasks, or when connecting to the cluster from a local machine.
resources:myMongoCluster:type: 'mongo-db-atlas-cluster'properties:clusterTier: M2adminUserCredentials:userName: my-master-userpassword: $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.
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.
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.