BlogAnnounced at MongoDB.local NYC 2024: A recap of all announcements and updatesLearn more >>
MongoDB Developer
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right

How to Deploy MongoDB Atlas with AWS CDK in TypeScript

Zuhair Ahmed, Andrea Angiolillo5 min read • Published Jan 23, 2024 • Updated Jan 23, 2024
Facebook Icontwitter iconlinkedin icon
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:

Step 1: Install AWS CDK

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:
This command installs AWS CDK. The optional -g flag allows you to use it globally anywhere on your machine.

Step 2: Bootstrap CDK

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.
Replace ACCOUNT_NUMBER with your AWS account number, and REGION with the AWS region you want to use.

Step 3: Initialize a New CDK app

Now we can initialize a new CDK app using TypeScript. This is done using the cdk init command:
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.

Step 4: Install MongoDB Atlas CDK

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:
This command installs the MongoDB Atlas CDK module, which will allow us to define and manage MongoDB Atlas resources in our CDK app.

Step 5: Replace the generated file with AtlasBasic CDK L3 repo example

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.

Step 6: Compare the deployed stack with the current state

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:
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.

Step 7: Deploy the app

Finally, if everything is set up correctly, you can deploy the app:
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.

(Optional) Step 8: Clean up the deployed resources

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:
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!

Facebook Icontwitter iconlinkedin icon
Rate this tutorial

How to Query from Multiple MongoDB Databases Using MongoDB Atlas Data Federation

Jan 23, 2024 | 7 min read

How to Seamlessly Use MongoDB Atlas and IBM LLMs in Your GenAI Applications

Nov 15, 2023 | 9 min read

How to Build a RAG System With LlamaIndex, OpenAI, and MongoDB Vector Database

Feb 16, 2024 | 10 min read

Listen Along at Scale Up with Atlas Application Services

Apr 02, 2024 | 3 min read
Table of Contents