Skip to content

elestio-examples/terraform-elestio-mongodb-cluster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Elestio MongoDB Cluster Terraform module

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.

Module requirements

Usage

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.

Quick configuration

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.

Ready-to-deploy example

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.

How to use the cluster

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"

Inputs

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 = {
username = "admin"
public_key = chomp(file("~/.ssh/id_rsa.pub"))
private_key = file("~/.ssh/id_rsa")
}
object({
username = string
public_key = string
private_key = string
})
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(
object({
server_name = string
provider_name = string
datacenter = string
server_type = string
admin_email = optional(string)
alerts_enabled = optional(bool)
app_auto_update_enabled = optional(bool)
backups_enabled = optional(bool)
firewall_enabled = optional(bool)
keep_backups_on_delete_enabled = optional(bool)
remote_backups_enabled = optional(bool)
support_level = optional(string)
system_auto_updates_security_patches_only_enabled = optional(bool)
ssh_public_keys = optional(list(
object({
username = string
key_data = string
})
), [])
})
)
[] no
project_id n/a string n/a yes

Modules

No modules.

Outputs

Name Description
connection_string n/a
nodes This is the created nodes full information

Providers

Name Version
elestio >= 0.17.0
null >= 3.2.0

Requirements

Name Version
terraform >= 1.0
elestio >= 0.17.0
null >= 3.2.0

Resources

Name Type
elestio_mongodb.nodes resource
null_resource.update_nodes_env resource