Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

555
Views
How do I get the full uri including username and password with the mongodbatlas provider in terraform

When I try to output the mongodb uri with Terraform and the mongodb atlas provider, I can't get the full uri with username and password. For example, when I do something like:

terraform {
  required_version = "~> 0.14.7"

  required_providers {
    mongodbatlas = {
      source  = "mongodb/mongodbatlas"
      version = "0.8.2"
    }
  }
}

provider "mongodbatlas" {
  public_key  = var.mongodbatlas_public_key
  private_key = var.mongodbatlas_private_key
}

data "mongodbatlas_cluster" "db" {
  project_id = var.mongodbatlas_project_id
  name       = format("some-db-name-%s", var.env)
}

output "db_url" {
  value = data.mongodbatlas_cluster.db.connection_strings[0].address_srv
}

I always get a uri of the form: mongodb+srv://some-db-name-staging.xjcol.mongodb.net Adding that as an environment variable to my web app in order to connect to db does not work as it needs to authenticate with a username and password. Manually adding the username and password to that string as in mongodb+srv://[username]:[password]@some-db-name-staging.xjcol.mongodb.net works and the app can connect to the db fine.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

While I get what you're trying to achieve, I suspect you're mixing things here. Let me explain:

  • MongoDB allows you to create database users that are able to authenticate using password. Those can be created using mongodbatlas_database_user resource.
  • You can create your cluster (or source cluster information) using both the resource or data source the way you're trying to achieve it.

However, cluster creation is independent of database and database user creation, meaning that what you're getting from Terraform is just a generic connection string from Mongo where not even Mongo knows which user/database you want to connect to.

I suggest you to compose your own connection string and pass it along to your application using a post-provisioning script, either using your Terraform outputs of cluster and database user, or simply composing it by yourself if you already know the info upfront.

In case you're using AWS, MongoDB Atlas supports connection strings using IAM Users and IAM Roles. This is a much better, safer approach than dealing with passwords and all the extra burden managing passwords implies. If this sounds like something you'd like to explore, do let me know.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!