Getting started with Public Cloud Databases via Terraform (EN)

Find out how to order and manage your Public Cloud managed database service using Terraform

Last updated October 19th, 2022


Public Cloud managed databases allow you to focus on building and deploying cloud applications while OVHcloud takes care of the database infrastructure and maintenance.

This guide explains how to order a MongoDB instance of a Public Cloud managed database service using Terraform.



Step 1: Gather the OVHcloud required parameters

Getting your cluster/API tokens information

The "OVH provider" needs to be configured with a set of credentials:

  • an application_key
  • an application_secret
  • a consumer_key


Because, behind the scenes, the "OVH Terraform provider" is doing requests to OVHcloud APIs.

In order to retrieve this necessary information, please follow our First steps with the OVHcloud APIs tutorial.

Specifically, you have to generate these credentials via the OVHcloud token generation page with the following rights:

OVHcloud API rights

When you have successfully generated your OVHcloud tokens, please save them as you will have to use them very soon.

The last needed information is the service_name: it is the ID of your Public Cloud project.

How to get it?

In the Public Cloud section, you can retrieve your service name ID thanks to the Copy to clipboard button.

Copy paste service name

You will also use this information in Terraform resources definition files.

Step 2: Gather the set of required parameters

In order to create a new MongoDB cluster, you will need to specify at least:

  • the version (e.g. "5.0")
  • the region (e.g. "DE")
  • the plan (e.g. "business")
  • the flavor of the cluster (e.g. "db1-7")

Step 3: Create Terraform files

First, create a file defining the resources that will be created

terraform {
  required_providers {
    ovh = {
      source  = "ovh/ovh"
      version = "0.22"

  required_version = ">= 0.17.1"

provider "ovh" {
  endpoint           =
  application_key    =
  application_secret =
  consumer_key       =

resource "ovh_cloud_project_database" "service" {
  service_name = var.product.project_id
  description  =
  engine       = "mongodb"
  version      = var.product.version
  plan         = var.product.plan
  nodes {
    region = var.product.region
  flavor = var.product.flavor

resource "ovh_cloud_project_database_mongodb_user" "dbuser" {
  service_name = ovh_cloud_project_database.service.service_name
  cluster_id   =
  name         =

resource "ovh_cloud_project_database_ip_restriction" "iprestriction" {
  service_name = ovh_cloud_project_database.service.service_name
  engine       = ovh_cloud_project_database.service.engine
  cluster_id   =
  ip           = var.access.ip

Then, create a file defining the variables used in

variable "ovh" {
  type = map(string)
  default = {
    endpoint           = "ovh-eu"
    application_key    = ""
    application_secret = ""
    consumer_key       = ""

variable "product" {
  type = map(string)
  default = {
    name       = "mongodb-terraform"
    project_id = ""
    region     = "DE"
    plan       = "business"
    flavor     = "db1-7"
    version    = "5.0"

variable "access" {
  type = map(string)
  default = {
    name = "johndoe"
    ip = "xx.xx.xx.xx/32"

Here, we defined the ovh-eu endpoint because we want to call the OVHcloud Europe API. Other endpoints exist, depending on your needs:

  • ovh-eu for OVHcloud Europe API
  • ovh-ca for OVHcloud North-America API

Then, create a secrets.tfvars file containing the required variables values:

ovh = {
  endpoint           = "ovh-eu"
  application_key    = "<application_key>"
  application_secret = "<application_secret>"
  consumer_key       = "<consumer_key>"

product = {
  project_id = "<service_name>"
  name       = "mongodb-terraform"
  region     = "DE"
  plan       = "business"
  flavor     = "db1-7"
  version    = "5.0"

access = {
  name = "johndoe"
  ip = "<ip_range>"

Don't forget to replace <service_name>, <application_key>, <application_secret>, <consumer_key>, <ip_range> by the real data.

Finally, create an file defining the resources that will be exported:

output "cluster_uri" {
  value = ovh_cloud_project_database.service.endpoints.0.uri

output "user_name" {
  value =

output "user_password" {
  value     = ovh_cloud_project_database_mongodb_user.dbuser.password
  sensitive = true

Step 4: Run

Now we need to initialise Terraform, generate a plan, and apply it.

$ terraform init

The init command will initialize your working directory which contains .tf configuration files.

It’s the first command to execute for a new configuration, or after doing a checkout of an existing configuration in a given git repository for example.

The init command will:

  • Download and install Terraform providers/plugins
  • Initialise backend (if defined)
  • Download and install modules (if defined)

Now, we can generate our plan:

$ terraform plan -var-file=secrets.tfvars

Thanks to the plan command, we can check what Terraform wants to create, modify or remove.

The plan is OK for us, so let's apply it:

$ terraform apply -var-file=secrets.tfvars -auto-approve

Finally export the user credentials and the URI

export PASSWORD=$(terraform output -raw user_password)
export USER=$(terraform output -raw user_name)
export URI=$(terraform output -raw cluster_uri)

And, voilà, the MongoDB cluster is created.

How to deploy with other engines

In this guide, we explained how to deploy a MongoDB service but you can find example for other database engine here and tweak them according to your needs :

Go further

MongoDB capabilities

Managing a MongoDB service from the OVHcloud Control Panel

Configuring vRack for Public Cloud

Visit our dedicated Discord channel: Ask questions, provide feedback and interact directly with the team that builds our databases services.

Join our community of users on

¿Le ha resultado útil esta guía?

Si lo desea, también puede enviarnos sus sugerencias para ayudarnos a mejorar nuestra documentación.

Imágenes, contenido, estructura...: ayúdenos a mejorar nuestra documentación con sus sugerencias.

No podemos tratar sus solicitudes de asistencia a través de este formulario. Para ello, haga clic en "Crear un tíquet" .

¡Gracias! Tendremos en cuenta su opinión.

Otras guías que podrían interesarle...

OVHcloud Community

¡Acceda al espacio de la OVHcloud Community! Resuelva sus dudas, busque información, publique contenido e interactúe con otros miembros de la comunidad.

Discuss with the OVHcloud community

A partir del 1 de enero de 2015, con arreglo a la Directiva 2006/112/CE modificada, los precios IVA incluido pueden variar según el país de residencia del cliente (por defecto, los precios con IVA incluyen el IVA español vigente).