Browse the Repo


Browse the Repo



Deploy a MongoDB cluster. Supports replica sets, sharding, automated bootstrapping, backup, recovery, and OS optimizations.

Code Preview

Preview the Code

mobile file icon


MongoDB Infrastrucutre Package

This repo contains several modules that together ared used to run a production-ready MongoDB cluster.


As of November, 2019, we are putting this repo on hold and will not be investing in further improvements to it. This is because:

  1. Maintaining the code for a complicated, distributed, sharded, stateful system like MongoDB is very time consuming.
  2. We've had very little customer usage / interest in a self-managed MongoDB offering, so it does not make sense to continue to make a large investment in it.

Therefore, for the time being, if you need to use MongoDB, we strongly recommend using one of the following alternatives:

  1. MongoDB Atlas: Deploy fully managed MongoDB across AWS, Azure, or GCP. This is the official service from the creators of MongoDB.
  2. mLab: Database-as-a-Service for MongoDB. Note that mLab has been acquired by MongoDB and may be integrated into Atlas in the future.
  3. Amazon DocumentDB: A MongoDB-compatible managed database from AWS.

If you have questions or concerns, please contact us at

Terraform Modules

  • mongodb-cluster - Terraform code to deploy a cluster of mongod, mongos, or Mongo Config servers in an Auto Scaling Group.
  • mongodb-backup - Terraform code to create an S3 Bucket, IAM Policy, and CloudWatch alarm used to setup backup for a MongoDB cluster.

Bash Script Modules

  • setup-ec2-instance - Bash scripts that attach an EBS Volume and Elastic Network Interface so that a Mongo node can mount a data volume on a separate EBS Volume and have a static private IP address.
  • install-mongodb - Bash script that runs during a Packer build that installs MongoDB, Supervisord, and OS optimizations recommended for Mongo.
  • run-mongodb - Bash script that runs in EC2 User Data that generates the configuration files for MongoDB and supervisord.
  • init-mongodb - A set of bash scripts that run in EC2 User Data that initialize a MongoDB Replica Set, create a MongoDB user, and add a Hidden Node.
  • backup-mongodb - This module runs mongodump, uploads the file to S3, and notifies a CloudWatch alarm it's working.

What is a module?

At Gruntwork, we've taken the thousands of hours we spent building infrastructure on AWS and condensed all that experience and code into pre-built packages or modules. Each module is a battle-tested, best-practices definition of a piece of infrastructure, such as a VPC, ECS cluster, or an Auto Scaling Group. Modules are versioned using Semantic Versioning to allow Gruntwork clients to keep up to date with the latest infrastructure best practices in a systematic way.

How do you use a module?

Most of our modules contain either:

  1. Terraform code
  2. Scripts & binaries

Using a Terraform Module

To use a module in your Terraform templates, create a module resource and set its source field to the Git URL of this repo. You should also set the ref parameter so you're fixed to a specific version of this repo, as the master branch may have backwards incompatible changes (see module sources).

For example, to use v1.0.8 of the mongodb module, you would add the following:

module "mongodb" {
  source = ""

  // set the parameters for the MongoDB module

Note: the double slash (//) is intentional and required. It's part of Terraform's Git syntax (see module sources).

See the module's documentation and file for all the parameters you can set. Run terraform get -update to pull the latest version of this module from this repo before runnin gthe standard terraform plan and terraform apply commands.

Using scripts & binaries

You can install the scripts and binaries in the modules folder of any repo using the Gruntwork Installer. For example, if the scripts you want to install are in the modules/mongodb-scripts folder of the repo, you could install them as follows:

gruntwork-install --module-name "mongodb-scripts" --repo "" --tag "0.0.1"

See the docs for each script & binary for detailed instructions on how to use them.

Developing a module


We are following the principles of Semantic Versioning. During initial development, the major version is to 0 (e.g., 0.x.y), which indicates the code does not yet have a stable API. Once we hit 1.0.0, we will follow these rules:

  1. Increment the patch version for backwards-compatible bug fixes (e.g., v1.0.8 -> v1.0.9).
  2. Increment the minor version for new features that are backwards-compatible (e.g., v1.0.8 -> v1.1.0).
  3. Increment the major version for any backwards-incompatible changes (e.g. v1.0.8 -> v2.0.0).

The version is defined using Git tags. Use GitHub to create a release, which will have the effect of adding a git tag.


See the test folder for details.

Reference code

  1. Terraform -
  2. Terraform -
  3. CloudFormation (terribly complicated) -
  4. MongoDB on AWS - Quick start reference deployment


Please see LICENSE.txt for details on how the code in this repo is licensed.


  1. Address MongoDB monitoring best practices
  2. Consider requiring Mongo client certs for authentication via mutual TLS per
  3. Contemplate in further depth the security options in mongod.conf

Questions? Ask away.

We're here to talk about our services, answer any questions, give advice, or just to chat.

Ready to hand off the Gruntwork?