How to Deploy MongoDB Atlas with AWS CDK in TypeScript
Zuhair Ahmed, Andrea Angiolillo5 min read • Published Jan 23, 2024 • Updated Jan 23, 2024
Rate this tutorial
MongoDB Atlas, the industry’s leading developer data platform, simplifies application development and working with data for a wide variety of use cases, scales globally, and optimizes for price/performance as your data needs evolve over time. With Atlas, you can address the needs of modern applications faster to accelerate your go-to-market timelines, all while reducing data infrastructure complexity. Atlas offers a variety of features such as cloud backups, search, and easy integration with other cloud services.
AWS Cloud Development Kit (CDK) is a tool provided by Amazon Web Services (AWS) that allows you to define infrastructure as code using familiar programming languages such as TypeScript, JavaScript, Python, Java, Go, and C#.
MongoDB recently announced the GA for Atlas Integrations for CDK. This is an ideal use case for teams that want to leverage the TypeScript ecosystem and no longer want to manually provision AWS CloudFormation templates in YAML or JSON. Not a fan of TypeScript? No worries! MongoDB Atlas CDK Integrations also now support Python, Java, C#, and Go.
In this step-by-step guide, we will walk you through the entire process. Let's get started!
Before we start, you will need to do the following:
- Open a MongoDB Atlas account ✅ Already have an AWS account? Atlas supports paying for usage via the AWS Marketplace (AWS MP) without any upfront commitment — simply sign up for MongoDB Atlas via AWS Marketplace.
- Install and configure an AWS Account + AWS CLI
- MongoDB::Atlas::Project
- MongoDB::Atlas::Cluster
- MongoDB::Atlas::DatabaseUser
- MongoDB::Atlas::ProjectIpAccessList
The AWS CDK is an open-source software (OSS) development framework for defining cloud infrastructure as code and provisioning it through AWS CloudFormation. It provides high-level components that preconfigure cloud resources with proven defaults, so you can build cloud applications without needing to be an expert. You can install it globally using npm:
1 npm install -g aws-cdk
This command installs AWS CDK. The optional -g flag allows you to use it globally anywhere on your machine.
Next, we need to bootstrap our AWS environment to create the necessary resources to manage the CDK apps. The
cdk bootstrap
command creates an Amazon S3 bucket for storing files and a CloudFormation stack to manage the resources.1 cdk bootstrap aws://ACCOUNT_NUMBER/REGION
Replace ACCOUNT_NUMBER with your AWS account number, and REGION with the AWS region you want to use.
Now we can initialize a new CDK app using TypeScript. This is done using the
cdk init
command:1 cdk init app --language typescript
This command initializes a new CDK app in TypeScript language. It creates a new directory with the necessary files and directories for a CDK app.
To manage MongoDB Atlas resources, we will need a specific CDK module called awscdk-resources-mongodbatlas (see more details on this package on our Construct Hub page). Let's install it:
1 npm install awscdk-resources-mongodbatlas
This command installs the MongoDB Atlas CDK module, which will allow us to define and manage MongoDB Atlas resources in our CDK app.
Feel free to start coding if you are familiar with CDK already or if it’s easier, you can leverage the AtlasBasic CDK resource example in our repo (also included below). This is a simple CDK Level 3 resource that deploys a MongoDB Atlas project, cluster, database user, and project IP access List resources on your behalf. All you need to do is paste this in your “lib/YOUR_FILE.ts” directory, making sure to replace the generated file that is already there (which was created in Step 3).
Please make sure to replace the
export class CdkTestingStack extends cdk.Stack
line with the specific folder name used in your specific environment. No other changes are required.1 // This CDK L3 example creates a MongoDB Atlas project, cluster, databaseUser, and projectIpAccessList 2 3 import * as cdk from 'aws-cdk-lib'; 4 import { Construct } from 'constructs'; 5 import { AtlasBasic } from 'awscdk-resources-mongodbatlas'; 6 7 interface AtlasStackProps { 8 readonly orgId: string; 9 readonly profile: string; 10 readonly clusterName: string; 11 readonly region: string; 12 readonly ip: string; 13 } 14 15 //Make sure to replace "CdkTestingStack" with your specific folder name used 16 export class CdkTestingStack extends cdk.Stack { 17 18 constructor(scope: Construct, id: string, props?: cdk.StackProps) { 19 super(scope, id, props); 20 21 const atlasProps = this.getContextProps(); 22 const atlasBasic = new AtlasBasic(this, 'AtlasBasic', { 23 clusterProps: { 24 name: atlasProps.clusterName, 25 replicationSpecs: [ 26 { 27 numShards: 1, 28 advancedRegionConfigs: [ 29 { 30 analyticsSpecs: { 31 ebsVolumeType: "STANDARD", 32 instanceSize: "M10", 33 nodeCount: 1 34 }, 35 electableSpecs: { 36 ebsVolumeType: "STANDARD", 37 instanceSize: "M10", 38 nodeCount: 3 39 }, 40 priority: 7, 41 regionName: atlasProps.region, 42 }] 43 }] 44 }, 45 projectProps: { 46 orgId: atlasProps.orgId, 47 }, 48 ipAccessListProps: { 49 accessList:[ 50 { ipAddress: atlasProps.ip, comment: 'My first IP address' } 51 ] 52 }, 53 profile: atlasProps.profile, 54 }); 55 } 56 57 getContextProps(): AtlasStackProps { 58 const orgId = this.node.tryGetContext('orgId'); 59 60 if (!orgId){ 61 throw "No context value specified for orgId. Please specify via the cdk context." 62 } 63 64 const profile = this.node.tryGetContext('profile') ?? 'default'; 65 const clusterName = this.node.tryGetContext('clusterName') ?? 'test-cluster'; 66 const region = this.node.tryGetContext('region') ?? "US_EAST_1"; 67 const ip = this.node.tryGetContext('ip'); 68 69 if (!ip){ 70 throw "No context value specified for ip. Please specify via the cdk context." 71 } 72 73 return { 74 orgId, 75 profile, 76 clusterName, 77 region, 78 ip 79 } 80 } 81 }
It's always a good idea to check what changes the CDK will make before actually deploying the stack. Use
cdk diff
command to do so:1 cdk diff --context orgId="YOUR_ORG" --context ip="YOUR_IP"
Replace YOUR_ORG with your MongoDB Atlas organization ID and YOUR_IP with your IP address. This command shows the proposed changes to be made in your infrastructure between the deployed stack and the current state of your app, notice highlights for any resources to be created, deleted, or modified. This is for review purposes only. No changes will be made to your infrastructure.
Finally, if everything is set up correctly, you can deploy the app:
1 cdk deploy --context orgId="YOUR_ORG" --context ip="YOUR_IP"
Again, replace YOUR_ORG with your MongoDB Atlas organization ID and YOUR_IP with your IP address. This command deploys your app using AWS CloudFormation.
Once you're finished with your MongoDB Atlas setup, you might want to clean up the resources you've provisioned to avoid incurring unnecessary costs. You can destroy the resources you've created using the cdk destroy command:
1 cdk destroy --context orgId="YOUR_ORG" --context ip="YOUR_IP"
This command will destroy the CloudFormation stack associated with your CDK app, effectively deleting all the resources that were created during the deployment process.
Congratulations! You have just deployed MongoDB Atlas with AWS CDK in TypeScript. Next, head to YouTube for a full video step-by-step walkthrough and demo.
The MongoDB Atlas CDK resources are open-sourced under the Apache-2.0 license and we welcome community contributions. To learn more, see our contributing guidelines.
The fastest way to get started is to create a MongoDB Atlas account from the AWS Marketplace. Go build with MongoDB Atlas and the AWS CDK today!