leogomesdev / mongodb-terraform-deployment

This repo provides examples and instructions for Terraform general usage & Creating MongoDB Atlas cluster using Terraform

Home Page:https://www.mentimeter.com/app/presentation/alufw5aescceonjvqweq35a9uaboujkc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

🌐 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:

About

This repo provides examples and instructions for Terraform general usage & Creating MongoDB Atlas cluster using Terraform

https://www.mentimeter.com/app/presentation/alufw5aescceonjvqweq35a9uaboujkc

License:MIT License


Languages

Language:HCL 100.0%