Giter Club home page Giter Club logo

mongodb-terraform-deployment's Introduction

๐ŸŒ MongoDB deployment using Terraform

๐Ÿ“š Description

This project contains examples and instructions for:

  1. Terraform general usage
  2. Creating MongoDB Atlas Cluster using Terraform

Terraform has several use cases in IT Industries. Both examples on this repository are ready to be used locally on a computer as a demonstration of Terraform usage. Please refer to the Terraform docs for other use cases of automation of Terraform.

โš  Important: The example on this repo for the MongoDB Cluster creates a cluster that can be accessed from any IP address in the world (0.0.0.0/0). This is just an example use case. Do not use the same config in a production environment. When running in prod, you must protect your applications and restrict access only to IP addresses or Security Group IDs that are really required.

๐Ÿ› ๏ธ Prerequisites

  • Clone this repository
    git clone https://github.com/leogomesdev/mongodb-terraform-deployment
  • Install the Terraform CLI
  • Prerequisites for example 1:
  • Prerequisites for example 2:
    • A MongoDB Atlas Account (Free) with access to manage your desired organization
    • Your organization ID:
      • Go to the Settings page and use the copy button Instructions to copy organization id
    • API Keys:
      • Go to Access Manager > Organization Access on the menu; then go to the API Keys tab and use the "Create API Key" button; insert a description and select the role Organization Project Creator; click on next; copy both values to a safe place Instructions to create API Key

Disclaimer:

Free tier cluster creation (M0) is supported for the resource mongodbatlas_cluster.

However, if your organization needs a bigger tier (paid), a payment method may be required for your organization on your Atlas Account. If this is your case, you could either check MongoDB Billing information for setting up a payment method or integrate payment with your AWS account through AWS Self-Serve Marketplace


๐Ÿณ Example 1: Using Terraform for docker management

  • Access the project folder:
    cd 01-docker-container-example
  • Create a new Terraform Workspace, by running the command:
    terraform workspace new local
  • Prepare your working directory for other commands (install provider plugins):
    terraform init
  • Optionally, show changes required by the current configuration:
    terraform plan
  • Create or update infrastructure:
    terraform apply
  • List Docker containers:
    docker ps
  • Access on the browser: http://localhost:8000/
  • Optionally, update terraform.tfvars file, and apply the new changes:
    terraform apply
  • Show the current state:
    terraform show
  • Destroy previously-created infrastructure:
    terraform destroy
  • Show the current state (should be empty now):
    terraform show

๐ŸŒ Example 2: Using Terraform for MongoDB Atlas Deployment

  • Access the project folder:

    cd 02-mongodb-example
  • Define your MongoDB Atlas API Keys as environment variables:

    export MONGODB_ATLAS_PUBLIC_KEY="XX"
    export MONGODB_ATLAS_PRIVATE_KEY="XX"
  • Define your Terraform variables on terraform.tfvars file. After copying the example file with the command below, you must edit the file to add your mongodb_atlas_org_id and update any desired variable.

    cp -v terraform.tfvars.example terraform.tfvars
  • Create a new Terraform Workspace, by running the command:

    terraform workspace new local
  • Prepare your working directory for other commands (install provider plugins):

    terraform init
  • Create or update infrastructure (skipping interactive approval):

    terraform apply -auto-aprove
  • After the success message, you are ready to use your database. On the MongoDB Atlas website you can get more info about how to connect with your database: Instructions to connect with your cluster

  • If using mongo shell, just connect with the cluster and run your MongoDB commands:

    mongosh "mongodb+srv://cluster01.<REPLACE>.mongodb.net/myApp" --username "my_user" --password "pass986@41"
    db.products.insertOne( { item: "card", qty: 15 } );
    db.products.find();
  • Destroy previously-created infrastructure:

    terraform destroy

๐Ÿ“ Docs and external resources:

mongodb-terraform-deployment's People

Contributors

leogomesdev avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.