1. Answers
  2. Setting up AWS DocumentDB with MongoDB compatibility

How do I set up AWS DocumentDB with MongoDB compatibility?

Setting up AWS DocumentDB with MongoDB Compatibility

In this guide, we’ll set up an AWS DocumentDB cluster that is compatible with MongoDB. AWS DocumentDB is a managed database service designed to be compatible with MongoDB, allowing you to leverage the performance, scalability, and availability of AWS while using MongoDB tools and applications.

What Will Be Done

  • Create a VPC and associated subnets to host the AWS DocumentDB cluster.
  • Set up security groups to control access to the cluster.
  • Create an AWS DocumentDB subnet group.
  • Deploy an AWS DocumentDB cluster within the defined subnet group.
  • Output the endpoint of the DocumentDB cluster.

AWS Resources Used

  • VPC: Virtual Private Cloud for network isolation.
  • Subnets: To distribute the DocumentDB instances across Availability Zones.
  • Security Group: Firewall rules to control access to the cluster.
  • Subnet Group: A collection of subnets for DocumentDB to deploy within.
  • DocumentDB Cluster: Managed NoSQL database with MongoDB compatibility.

Code Implementation

Let’s dive into the code:

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const main = new aws.ec2.Vpc("main", {
    cidrBlock: "10.0.0.0/16",
    tags: {
        Name: "main_vpc",
    },
});
const subnet1 = new aws.ec2.Subnet("subnet1", {
    vpcId: main.id,
    cidrBlock: "10.0.1.0/24",
    tags: {
        Name: "db_subnet1",
    },
});
const subnet2 = new aws.ec2.Subnet("subnet2", {
    vpcId: main.id,
    cidrBlock: "10.0.2.0/24",
    tags: {
        Name: "db_subnet2",
    },
});
const dbSg = new aws.ec2.SecurityGroup("db_sg", {
    name: "documentdb_sg",
    description: "Allow MongoDB access",
    vpcId: main.id,
    ingress: [{
        fromPort: 27017,
        toPort: 27017,
        protocol: "tcp",
        cidrBlocks: ["0.0.0.0/0"],
    }],
    egress: [{
        fromPort: 0,
        toPort: 0,
        protocol: "-1",
        cidrBlocks: ["0.0.0.0/0"],
    }],
    tags: {
        Name: "documentdb_sg",
    },
});
const example = new aws.docdb.SubnetGroup("example", {
    name: "docdb_subnet_group",
    subnetIds: [
        subnet1.id,
        subnet2.id,
    ],
    tags: {
        Name: "docdb_subnet_group",
    },
});
const exampleCluster = new aws.docdb.Cluster("example", {
    clusterIdentifier: "docdb-cluster",
    engine: "docdb",
    masterUsername: "masterUser",
    masterPassword: "securePassword123",
    dbSubnetGroupName: example.name,
    vpcSecurityGroupIds: [dbSg.id],
    tags: {
        Name: "docdb-cluster",
    },
});
const exampleClusterInstance = new aws.docdb.ClusterInstance("example", {
    identifier: "example-instance",
    clusterIdentifier: exampleCluster.id,
    instanceClass: "db.r5.large",
    tags: {
        Name: "docdb-instance",
    },
});
export const documentdbClusterEndpoint = exampleCluster.endpoint;

Key Points

  • VPC and Subnets: Providing a network environment for DocumentDB.
  • Security Group: Allowing access to the DocumentDB from any IP for port 27017.
  • Subnet Group: Cluster deployment across multiple subnets for high availability.
  • DocumentDB Cluster: The managed database cluster supporting MongoDB.
  • Output: Capturing the endpoint which will be used to connect to the DocumentDB cluster.

Conclusion

By following this guide, we’ve set up a highly available AWS DocumentDB cluster that is compatible with MongoDB. You now have a network environment ready and a secure cluster endpoint you can use with your MongoDB applications. This setup ensures that your document data is managed efficiently within AWS’s robust infrastructure.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up