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 tier, storage capacity, or both, based on usage. To control costs, you can select a range of cluster tiers to which your cluster can scale.
A cluster is scaled up (to the next tier) if:
- Average CPU utilization has exceeded 75% for the past hour.
- Memory utilization has exceeded 75% for the past hour.
A cluster is scaled down (to a lower tier) if:
- The average CPU 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 one shard, the cluster will run in sharded mode, which distributes data across multiple machines to enable horizontal scaling. Sharded mode is only available for cluster tiers M30 and higher.
For more details on sharding, see the MongoDB documentation.
resources:myMongoCluster:type: mongo-db-atlas-clusterproperties:clusterTier: M30numShards: 3
Backups
Backups are copies of your data that provide a safety measure in the event of data loss. The default snapshot time is every day at 18:00 UTC. This feature is only available for M10+ clusters.
Snapshots are also automatically taken for M2/M5 clusters but have different properties. For more details, see the [M2 and M5 backup documentation](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
This feature enables Continuous Cloud Backups, which replay the oplog (a history of ordered logical writes) to allow you to restore a cluster to a specific point in time within the last 7 days.
It is only available for M10+ clusters and requires enableBackups to be true.
Continuous Cloud Backup incurs additional charges. For more details, see the MongoDB Continuous Cloud Backup pricing documentation.
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
This allows you to create an admin user with administrative access privileges.
While not required for accessing the cluster from your compute resources, creating an admin user is useful for performing administrative tasks or 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.