5

I have successfully created a project, user and cluster via the Mongodb terraform provider, however I am expecting to see a database already created under my new cluster, which is not to be found. I am not sure what it is missing or incorrect and I could not find any example/info in the documentation that diverges from what I implemented myself. Here are the relevant info from my main.tf file:

# Create a db user
resource "mongodbatlas_database_user" "mongodb_user" {
  username = "${var.database_username}"
  password = "${random_string.master_password.result}"
  project_id = "${mongodbatlas_project.mongodb.id}"
  database_name  = "admin"

  roles {
    role_name = "readWrite"
    database_name = "admin"
  }
}

group

resource "mongodbatlas_project" "mongodb"{
  org_id = "${var.mongodb_atlas_org_id}"
  name = "${var.project_name}-${var.stage}"
 }

cluster

# Create a cluster
resource "mongodbatlas_cluster" "mongodb-cluster" {
  project_id = "${mongodbatlas_project.mongodb.id}"
  name = "${var.cluster_name}-${var.stage}"
  num_shards = 1

  replication_factor = 3
  backup_enabled = true
  auto_scaling_disk_gb_enabled = true
  mongo_db_major_version = "4.0"

  //Provider Settings "block"
  provider_name = "AWS"
  disk_size_gb = 100
  provider_disk_iops = 300
  provider_encrypt_ebs_volume = false
  provider_instance_size_name = "M40"
  provider_region_name = "us-east-1"
}

Any help/advice is greatly appreciated.

Thank you

panza
  • 1,341
  • 7
  • 38
  • 68
  • Do you mean you're expecting there to be a database on the cluster? AFAIK, the Atlas API doesn't allow you to do that and instead that needs to be managed via the MongoDB driver in your application. Attempting to use a non existent database should just create it for you. – ydaetskcoR Sep 29 '19 at 11:26
  • Hi, thanks for your reply. Yes, since I can actually create a db manually via the UI, I though that the terraform provider (according to the above settings) will actually create the db. Are you saying that it is a totally wrong assumption? – panza Sep 29 '19 at 18:35
  • 1
    It's not something exposed by [the Atlas API](https://docs.atlas.mongodb.com/reference/api-resources/) so unfortunately Terraform can't do that bit for you. That said, if your application attempts to use a database in a cluster and it doesn't exist then the MongoDB driver should just create it as necessary so this shouldn't really matter. – ydaetskcoR Sep 29 '19 at 18:53
  • OK, it makes sense now. Thank you indeed, I could not really wrap my head around it! – panza Sep 29 '19 at 18:55

2 Answers2

3

The database creation is a CRUD operation, and the MongoDB Atlas API does not supports CRUD operation.

Also, Terraform is used to deploy your infrastructure and not the data inside it. You can create your own REST API which connects to the cluster created by Terraform, uses the user created by Terraform to connect, and then perform any CRUD operation you want.

Hope this answers your question.

Nikhil Singh
  • 114
  • 3
  • 3
    "Terraform is used to deploy your infrastructure and not the data inside it." This seems like a semantic distinction. You can create mongodbatlas_database_users via Terraform--it's pretty hard to say that a user is "infrastructure" but a database isn't. Moreover, with dynamodb you can even create tables with Terraform. So this seems like a pretty weak distinction IMHO. – weberc2 Jul 22 '21 at 19:49
1

Database and collection creation in MongoDb Atlas is a developer jobs.

I suggest you define the User's permissions on Terraform (mongodbatlas_custom_db_role).

So you can to restrict the acces, database name and collection name. it's a good approach.

https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/custom_db_role

Davidson Dd-harlie
  • 151
  • 1
  • 2
  • 9