Skip to main content

MongoDB (NoSQL)

Prerequisites​

To enable Mongo database, first you must have runtyme-kit pushed to the repostiory you've created in Get started step.

If you don't have it setted up yet, please go back to Get started step. Otherwise:

  • Make sure your repository has TF_ENABLED environment variable set to true, if not follow Installation step.
  • Make sure your Infrastructure Backend is configured (TF_BACKEND_PG_CONN_STR exists), if not follow State Management step.
  • Signup to MongoDB Atlas here if you don't have an account yet.

Get Organization ID​

  • Go to your MongoDB Atlas dashboard
  • Find your organization
  • Click Settings icon in top navigation bar
  • You should see a page with panel called Organization ID, copy the value from this panel

How to get MongoDB's Organization ID

Get MongoDB public and private key​

To get public and private key of your MongoDB Atlas organization you have to log into your dashboard, select your organization and click Settings button next to your organization's name on top nav bar. Then you'll be navigated to Organization Access Manager page, where you'll see Applications tab, click it, select API Keys and then tap Add new button on the right side of your screen.

How to find API Key access manager in MongoDB Atlas

After clicking Add new button, you'll see API Key creation wizard form, name your key however you want and select Organization Member and Organization Project Creator permissions. Then click Next

How to create MongoDB Atlas organization API Keys

Finally, you'll see your Public Key and Private Key, make sure you copy and save them in a safe space. You'll use them in next step.

How to find API Key access manager in MongoDB Atlas

GitHub Actions​

Your CI/CD runner will use MonogDB's Private and Public Keys as secured secrets environment variable to orchestrate MongoDB Atlas changes. It'll also use organization id to identify where to put your database configuration.

  • Go to your repository's Settings tab, under Security click Secrets and variables->Actions
  • Click New repository secret, name it TF_VAR_MONGODB_PUBLIC_KEY and paste the Public Key you've created as a value.
  • Click New repository secret, name it TF_VAR_MONGODB_PRIVATE_KEY and paste the Private Key you've created as a value.
  • Click New repository secret, name it TF_VAR_MONGODB_ORGANIZATION_ID and paste the organization identifier.

Configure __env.tf​

You'll be configuring MongoDB Atlas just from one file, which is infra/__env.tf. In this file you'll find plenty of disabled yet already configured resources, but in this guide we'll be looking at mongodb under Persistence segment.

  • To enable mongodb resource, you need to switch enabled flag to true, specify db_name and db_user.
  • It is recommended to also specify region but it is not required, default value would be EU_CENTRAL_1. Available values can be found here (runtyme-kit uses AWS as provider).
  • (non free tier) If you're not running on free tier, your can also specify size of the instance. Free tier is always M0. Available values can be found here. Keep in mind that non free tiers might require changes in source code of runtyme-kit, specifically infra/modules/mongodb/instance module.
__env.tf
locals {
...
mongodb = {
// might take couple of minutes when creating it
enabled = true
// (required) Name of the project and the database instance
db_name = "[YOUR_DB_NAME]"
// (required) Name of your API's connecting user, that will be authenticated to your MongoDB instance
db_user = "[YOUR_DB_USERNAME]"
// (optional) Instance size of MongoDB Atlas, either `M0`, `M2`, `M5` - available values can be found here: https://www.mongodb.com/docs/atlas/reference/amazon-aws/#cluster-configuration-options
// default: 'M0'
// NOTE: `M0` - Is a Free Tier cluster with 0.5 GB storage, learn about differences here: https://www.mongodb.com/docs/atlas/reference/free-shared-limitations/
// NOTE: for anything more than `M5` tier, custom solution (modifying source code) is necessary
size = "M0"
// (optional) AWS region on which the MongoDB Atlas database cluster will be deployed on. Available values can be found here: https://www.mongodb.com/docs/atlas/reference/amazon-aws/#amazon-web-services--aws-
// default: 'EU_CENTRAL_1'
region = "EU_CENTRAL_1"
}
...
}

When you make desired changes, push the code into the repository (create Pull Request or push it directly to default branch, it's up to you) and you should see Action Job being executed. Make sure to validate scheduled changes to your infrastructure, or potential errors.

info

Connection string and database name of MongoDB will be automatically attached to the running application (if heroku.enabled = true)