Using this module, you can easily and quickly deploy with Terraform a MongoDB Cluster on Elestio that is configured correctly and ready to use, without spending a lot of time on manual configuration.
- The minimum number of nodes in a mongodb cluster is 3
- 1 Elestio account https://dash.elest.io/signup
- 1 API key https://dash.elest.io/account/security
- 1 SSH public/private key (see how to create one here)
This is a minimal example of how to use the module:
module "cluster" {
source = "elestio-examples/mongodb-cluster/elestio"
project_id = "xxxxxx"
mongodb_pass = "xxxxxx"
mongodb_keyfile = file("./keyfile") # openssl rand -base64 741 > ./keyfile
configuration_ssh_key = {
username = "something"
public_key = chomp(file("~/.ssh/id_rsa.pub"))
private_key = file("~/.ssh/id_rsa")
}
nodes = [
{
server_name = "mongodb-1"
provider_name = "scaleway"
datacenter = "fr-par-1"
server_type = "SMALL-2C-2G"
},
{
server_name = "mongodb-2"
provider_name = "scaleway"
datacenter = "fr-par-2"
server_type = "SMALL-2C-2G"
},
{
server_name = "mongodb-3"
provider_name = "scaleway"
datacenter = "fr-par-2"
server_type = "SMALL-2C-2G"
},
]
}
output "connection_string" {
value = module.cluster.connection_string
sensitive = true
}
Keep your mongodb password safe, you will need it to access the admin panel.
If you want to know more about node configuration, check the mongodb service documentation here.
If you want to choose your own provider, datacenter or server type, check the guide here.
If you want to generated a valid SSH Key, check the guide here.
If you add more nodes, you may attains the resources limit of your account, please visit your account quota page.
The following example will create a MongoDB cluster with 3 nodes.
You may need to adjust the configuration to fit your needs.
Create a main.tf
file at the root of your project, and fill it with your Elestio credentials:
terraform {
required_providers {
elestio = {
source = "elestio/elestio"
}
}
}
provider "elestio" {
email = "xxxx@xxxx.xxx"
api_token = "xxxxxxxxxxxxx"
}
resource "elestio_project" "project" {
name = "mongodb-cluster"
}
Generate your MongoDB keyfile using the following command:
openssl rand -base64 756 > keyfile
Now you can use the module to create mongodb nodes:
module "cluster" {
source = "elestio-examples/mongodb-cluster/elestio"
project_id = elestio_project.project.id
mongodb_version = null # null means latest version
mongodb_pass = "xxxxxxxxxxxxx"
mongodb_keyfile = file("./keyfile") # openssl rand -base64 741 > ./keyfile
configuration_ssh_key = {
username = "terraform"
public_key = chomp(file("~/.ssh/id_rsa.pub"))
private_key = file("~/.ssh/id_rsa")
}
nodes = [
{
server_name = "mongodb-1"
provider_name = "hetzner"
datacenter = "fsn1"
server_type = "SMALL-1C-2G"
},
{
server_name = "mongodb-2"
provider_name = "hetzner"
datacenter = "fsn1"
server_type = "SMALL-1C-2G"
},
{
server_name = "mongodb-3"
provider_name = "hetzner"
datacenter = "fsn1"
server_type = "SMALL-1C-2G"
},
]
}
To get a valid random mongodb password, you can use the url https://api.elest.io/api/auth/passwordgenerator
$ curl -s https://api.elest.io/api/auth/passwordgenerator
{"status":"OK","password":"7Tz1lCfD-Y8di-AyU2o467"}
Finally, let's add some outputs to retrieve useful information:
output "nodes_admins" {
value = { for node in module.cluster.nodes : node.server_name => node.admin }
sensitive = true
}
output "connection_string" {
value = module.cluster.connection_string
sensitive = true
}
You can now run terraform init
and terraform apply
to create your MongoDB cluster.
After a few minutes, the cluster will be ready to use.
You can access your outputs with terraform output
:
$ terraform output nodes_admins
$ terraform output connection_string
If you want to update some parameters, you can edit the main.tf
file and run terraform apply
again.
Terraform will automatically update the cluster to match the new configuration.
Please note that changing the node count requires to change the .env of existing nodes. This is done automatically by the module.
We created a MongoDB ready-to-deploy cluster example which creates the same infrastructure as the previous example. You can find it here. Follow the instructions to deploy the example.
According to the MongoDB documentation, you can put several hosts in a connection string.
Example: mongodb://user:password@host1:17271,host2:17271,host3:17271/?replicaSet=Cluster0
Use terraform output connection_string
command to output database secrets:
$ terraform output connection_string
"mongodb://*****:******@mongodb-1-u525.vm.elestio.app:17271,mongodb-2-u525.vm.elestio.app:17271,mongodb-3-u525.vm.elestio.app:17271/?replicaSet=Cluster0"
Name | Description | Type | Default | Required |
---|---|---|---|---|
configuration_ssh_key | After the nodes are created, Terraform must connect to apply some custom configuration. This configuration is done using SSH from your local machine. The Public Key will be added to the nodes and the Private Key will be used by your local machine to connect to the nodes. Read the guide "How generate a valid SSH Key for Elestio". Example: configuration_ssh_key = { |
object({ |
n/a | yes |
mongodb_keyfile | The nodes will use this key to authenticate each other. Read How to generate a keyfile. For example, the following operation openssl rand -base64 741 > ./keyfile :mongodb_keyfile = file("./keyfile") |
string |
n/a | yes |
mongodb_pass | Require at least 10 characters, one uppercase letter, one lowercase letter and one number. Generate a random valid password: https://api.elest.io/api/auth/passwordgenerator |
string |
n/a | yes |
mongodb_version | The cluster nodes must share the same mongodb version. Leave empty or set to null to use the Elestio recommended version. |
string |
null |
no |
nodes | Each element of this list will create an Elestio MongoDB Resource in your cluster. Read the following documentation to understand what each attribute does, plus the default values: Elestio KeyDB Resource. |
list( |
[] |
no |
project_id | n/a | string |
n/a | yes |
No modules.
Name | Description |
---|---|
connection_string | n/a |
nodes | This is the created nodes full information |
Name | Version |
---|---|
elestio | >= 0.17.0 |
null | >= 3.2.0 |
Name | Version |
---|---|
terraform | >= 1.0 |
elestio | >= 0.17.0 |
null | >= 3.2.0 |
Name | Type |
---|---|
elestio_mongodb.nodes | resource |
null_resource.update_nodes_env | resource |