Question

How to retrieve database credentials and certificate in Terraform?

I’m using the DigitalOcean Terraform Provider to set up my infrastructure, and I would like to set up a MongoDB database inside a cluster for each environment I’m running.

When applying a config like this:

variable "db-cluster-name" {
  default = "cluster"
  type = string
}

data "digitalocean_database_cluster" "db-cluster" {
  name = var.db-cluster-name
}

resource "digitalocean_database_db" "db" {
  cluster_id = data.digitalocean_database_cluster.db-cluster.id
  name       = var.environment
}

resource "digitalocean_database_user" "db-user" {
  cluster_id = data.digitalocean_database_cluster.db-cluster.id
  name       = var.environment
}

output "database-url" {
  value = "mongodb+srv://${digitalocean_database_user.db-user.name}:${digitalocean_database_user.db-user.password}@${data.digitalocean_database_cluster.db-cluster.host}/${resource.digitalocean_database_db.db.name}?tls=true&authSource=admin"
  sensitive = true
}

I would expect a database and user to be created and managed by Terraform, and a MongoDB connection URL to be outputted by Terraform with the user credentials baked in. Unfortunately the password field is always either empty (“”) or null, depending on if I create a new user or try to retrieve the default user doadmin. I understand it’s a secret, but on creation of a user the password should be retrievable somehow, else the automation of infrastructure will be impossible. Is there any way to retrieve this using the Terraform Provider? Also, how do I retrieve the certificate authority file (.crt) associated with the database, that is needed for connection?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Rajkishore J
DigitalOcean Employee
DigitalOcean Employee badge
October 6, 2021
Accepted Answer

Hi @yuriEel,

Terraform identified a bug where users getting MongoDB user password attribute empty and creating connection issues. This might be the reason you’re not getting credentials for MongoDB via Terraform.

For your reference, You can follow the below documents for more information :

https://github.com/digitalocean/terraform-provider-digitalocean/issues/706

Also note, DigitalOcean engineers currently working on removing the necessity for installing a CA cert. After this you won’t need to have a CA cert to connect the cluster; there is no guaranteed ETA on this right now.

Regarding limitations on managed MongoDB cluster, You can follow the below document on MongoDB limits :

https://docs.digitalocean.com/products/databases/mongodb/#mongodb-limits

I hope this helps!

Regards, Rajkishore

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel