Create databases and collections via API thorough Terraform
Create databases (DB) and collections via API thorough Terraform after the cluster has been created in Atlas. This would provide the ease of writing and running it in a single script before any data is loaded.

-
Joel commented
Why is this still not implemented...? As others have said creating collections and indexes is not the responsibility of application code.
-
Anuradha commented
+1 much needed solution for the enterprise customer
-
Nitish commented
Thanks Anuj, I'll reach out via email.
-
Anuj Tyagi commented
Hello Nitish,
Great to hear from you. Please feel free to connect with me in case of any comments or feedback. I've provided my contact details in last post.
-
Nitish commented
Thank you Anuj for sharing about your provider. We've been using terraform-provider-mongodb for managing our MongoDB users, roles, and indexes in our infrastructure from more than a month. The provider is straightforward to use and handles the core MongoDB resources we need.
-
Anuj Tyagi commented
If anyone need help to manage Indexes through Terraform, here is the provider feature I have added: registry.terraform.io/providers/megum1n/mongodb/latest
It also support user and their roles.In case of any help required, feel free to contact me on https://www.linkedin.com/in/anujtyagi1 or anujajtyagi@gmail.com
I will be happy to help, improve in case of any feedback -
Jonas commented
+1 ask from my enterprise customer!
-
Usman commented
We manage all our MongoDB Atlas infrastructure using Terraform for consistency, repeatability, and compliance with our IaC standards. The inability to create databases and collections through Terraform breaks this workflow and requires manual intervention or external scripting, which increases the risk of configuration drift and human error.
Having support for database and collection creation via Terraform would allow us to fully automate our MongoDB environments and maintain a consistent, version-controlled deployment pipeline across all environments. This would be a valuable enhancement for teams adopting infrastructure as code best practices.
-
Kevin commented
+1 ask from my enterprise customer.
-
Anuj Tyagi commented
Hello, I have created a terraform provider to manage MongoDB indexes. It also support users and roles. When you create index, it also will create collections.
Feel free to give it a try:
https://registry.terraform.io/providers/megum1n/mongodb/latest -
Josh commented
There's no resource in terraform-provider-mongodbatlas to create a collection, but there is a resource to create a search index, which requires a collection to be created beforehand.
This means that the lifecycle of a DB with a search index cannot be managed entirely in Terraform - I have to do something out-of-process to create the collection (e.g. use the MongoDB client). I can't even do this using the Atlas CLI, much to my chagrin.
-
Martin commented
That would be really useful.
-
Spencer commented
I've been using a python script with null_resource buried within a module to create databases and indexes as a work around.
-
Phil Owen commented
Really difficult to make use of the mongodbatlas_event_trigger resource if you want one of type DATABASE and the collection needs to exist, but you can't create the collection using Terraform...
-
Ben Gesoff commented
The documentation for the online archive resource states that a collection must already exist in order to enable the online archive feature for it: https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/online_archive
Therefore I think it's pretty important to support creating a collection from Terraform, otherwise it's impossible to use the online archive resource as part of a declarative infrastructure-as-code configuration. Instead we have to resort to some sort of flakey script in a Terraform "provisioner" which is an antipattern.
-
Spencer Langley commented
This would fill a painful gap in the dev ops our practices that otherwise Atlas serves quite well.
-
John commented
Set up of indices should not be the responsibility of application code. Set up of indices is an infrastructure task. Managing IAC is commonly considered a critical piece.
-
Kareem Zock commented
Creating a database is core functionality. Im surprised that creating a database isn't available.
-
Dave Fournier commented
Creating a database is core functionality. I'm stunned that creating a database isn't available.
-
Frank Ledo commented
This is one of those features that should be expected from an enterprise product.
Not having this creates many challenges with creating search and global shard indexes, since those cannot be created during runtime.