How to Deploy MongoDB Atlas with AWS CDK in TypeScript
Rate this tutorial
, the industry’s leading developer data platform, simplifies application development and working with data for a wide variety of use cases, scales globally, and 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.
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:
The AWS CDK is an open-source software (OSS) development framework for defining cloud infrastructure as code and provisioning it through . 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.
Next, we need to bootstrap our AWS environment to create the necessary resources to manage the CDK apps. The
cdk bootstrapcommand 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.
Now we can initialize a new CDK app using TypeScript. This is done using the
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.
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 (also included below). This is a simple 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.Stackline with the specific folder name used in your specific environment. No other changes are required.
It's always a good idea to check what changes the CDK will make before actually deploying the stack. Use
cdk diffcommand 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.
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.
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.
Building Generative AI Applications Using MongoDB: Harnessing the Power of Atlas Vector Search and Open Source Models
Dec 04, 2023 | 10 min read