This project contains examples and instructions for:
- Terraform general usage
- 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.
- Clone this repository
git clone https://github.com/leogomesdev/mongodb-terraform-deployment
- Install the Terraform CLI
- Prerequisites for example 1:
- Docker Desktop installed and running locally
- Prerequisites for example 2:
- A MongoDB Atlas Account (Free) with access to manage your desired organization
- Your organization ID:
- API Keys:
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
- 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
-
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:
-
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