Infrastructure as Code has become the standard for provisioning cloud resources with repeatability and auditability. Terraform provides a declarative language for describing infrastructure, and two distinct providers named Atlas address different aspects of database management: the MongoDB Atlas Terraform Provider for provisioning MongoDB Atlas clusters and infrastructure, and the Ariga Atlas Terraform Provider for schema migrations and database lifecycle management using modern DevOps principles.
Both providers integrate with Terraform workflows but serve complementary purposes. One automates provisioning of MongoDB Atlas organizations, projects, clusters, users and network access as code. The other brings schema-first, versioned migration capabilities to relational databases and treats schema as code alongside infrastructure.
MongoDB Atlas Terraform Provider Overview
The MongoDB Atlas Terraform Provider enables you to manage your database schema as part of your Infrastructure-as-Code workflows using Terraform. With the provider, you can define your desired schema state and apply it to your database using Terraform resources.
Quick Start guidance emphasizes managing your database schema with Terraform: declare it, version it, and apply changes alongside the rest of your infrastructure.
Supported Workflows
Atlas supports versioned and declarative workflows. They are supported by the atlasmigration and atlasschema resources respectively.
Guides available with the provider cover:
- Ad-hoc Approval
Learn how to support ad-hoc approvals for declarative schema changes. - Project Configuration
Learn how to use Atlas Project Configuration with the Atlas Terraform Provider. - Provisioning Named Databases
Provision schemas in managed databases like Amazon RDS or Cloud SQL with Terraform. - OpenTaco (Digger) Integration
Automate database schema migrations from pull requests using OpenTaco (Digger).
The repository for the Terraform MongoDB Atlas Provider allows one to use Terraform with MongoDB's Database as a Service offering, Atlas. Learn more about Atlas at https://www.mongodb.com/cloud/atlas.
Support for the Terraform MongoDB Atlas Provider is provided under MongoDB Atlas support plans. Please submit support questions within the Atlas UI. Support questions submitted under the Issues section of this repo will be handled on a "best effort" basis.
Bugs should be filed under the Issues section of this repo.
Feature requests can be submitted at the MongoDB feedback portal - select Atlas -> infra-as-code as the category or vote for an already suggested feature.
To use a released provider in your Terraform environment, run terraform init and Terraform will automatically install the provider. To specify a particular provider version when installing released providers, see the Terraform documentation on provider versioning.
Documentation about the provider specific configuration options can be found on the provider's website.
To help with issues, you can turn on Logs with export TF_LOG=TRACE.
Provisioning Atlas Clusters with Terraform
You can use Terraform to manage your MongoDB Atlas infrastructure as code with the Atlas Terraform provider. The Atlas Terraform provider automates infrastructure deployments by simplifying the process to provision, manage, and control Atlas infrastructure as code.
This guide teaches you how to create, modify, and destroy Atlas clusters in an existing MongoDB Atlas organization and project.
Once you have configured a test environment, continue to experiment with a Terraform-managed infrastructure with additional resources from MongoDB and HashiCorp.
Before deploying MongoDB Atlas with Terraform, you must update the following fields in your main.tf file:
| Field | New Value |
|---|---|
| Your provider. Possible values are: | |
| See Cloud Providers and Regions for all the regions you can use. |
You can output information from your Terraform configuration to your terminal window. This is useful for values you won't know until Atlas creates the resources, such as your connection string.
If you want to display your parameters after you deploy your project, add some output lines of code to your main.tf file.
To deploy your infrastructure, run the following command:
terraform apply
When prompted Do you want to perform these actions?, enter yes.
Note
New Atlas resources can take a few minutes to provision.
Creating an Atlas Terraform File using the Template
Use the Atlas template for Terraform files included with the MongoDB for VS Code to configure an Atlas cluster.
To update the Atlas Terraform configuration to configure your cluster, you need to provide values for each of the attributes mentioned.
Create the Atlas Cluster using Terraform
After we create a Terraform file using the template, create the Atlas cluster:
- Go to the directory containing your main.tf file.
- Execute terraform init to install necessary providers.
terraform init
The following output indicates that the MongoDB Atlas Terraform Provider is installed and ready for use:
- Initializing the backend: This step involves setting up the backend configuration for Terraform, which could include storing the state file remotely for collaboration and version control.
- Initializing provider plugins: Terraform is checking for available provider plugins, which are responsible for managing resources in different infrastructure providers.
- Downloading plugin for provider "mongodbatlas": Terraform is downloading the MongoDB Atlas Terraform Provider plugin. The version specified is 0.5.1.
- Version constraints: It's recommended to add version constraints to prevent automatic upgrades to new major versions that may contain breaking changes. For the mongodbatlas provider, the suggested constraint is ~> 0.5, which means it will use version 0.5.x but not automatically upgrade to version 1.0 or higher.
- Initialization complete: Terraform has been successfully initialized, and the MongoDB Atlas Terraform Provider is ready for use.
Ariga Atlas Terraform Provider for Schema Management
Atlas is a language-agnostic tool for managing and migrating database schemas using modern DevOps principles. It offers two workflows:
- Declarative: Similar to Terraform, Atlas compares the current state of the database to the desired state, as defined in an HCL, SQL, or ORM schema. Based on this comparison, it generates and executes a migration plan to transition the database to its desired state.
- Versioned: Unlike other tools, Atlas automatically plans schema migrations for you. Users can describe their desired database schema in HCL, SQL, or their chosen ORM, and by utilizing Atlas, they can plan, lint, and apply the necessary migrations to the database.
Provider configuration example:
```
terraform {
required_providers {
atlas = {
source = "ariga/atlas"
version = "~> 0.9.8"
}
}
}
provider "atlas" {
Use MySQL 8 docker image as the dev database.
dev_url = "docker://mysql/8"
}
```
To create a schema for your database, first install atlas.
Then, inspect the schema of the database:
atlas schema inspect -u "mysql://root:pass@localhost:3306/example" > schema.hcl
Terraform Modules and Landing Zone Guidance
Back up any data you want to keep before running this command.
Atlas Examples repository: Complete, runnable Terraform examples for AWS, Azure, and Google Cloud.
Terraform-MongoDB Atlas Modules: The official Terraform module registry namespace with all available modules.
Atlas Terraform Provider: Full provider documentation.
Get Started with Terraform: A quickstart guide for provisioning a basic Atlas cluster with Terraform.
Guidance for Atlas Organizations, Projects, and Clusters: Atlas Architecture Center guidance to design your Atlas infrastructure.
Provider Comparison and Use Cases
The two Atlas providers address different layers of the stack.
| Provider | Primary Scope | Workflow Support |
|---|---|---|
| MongoDB Atlas Terraform Provider | MongoDB Atlas infrastructure provisioning | Declarative infrastructure |
| Ariga Atlas Terraform Provider | Database schema migrations and management | Declarative schema, Versioned schema |
MongoDB Atlas Terraform Provider is used when the goal is to provision and manage Atlas organizations, projects, clusters, users, network peering, and backups as code. It integrates with Terraform's plan and apply cycle for infrastructure.
Ariga Atlas Terraform Provider is used when the goal is to manage relational database schemas with DevOps practices, generate migration plans, lint schemas, and apply changes safely. It is language-agnostic and supports HCL, SQL, and ORM definitions.
Both can coexist in a Terraform workspace where infrastructure provisioning and schema evolution are separated but versioned together.
Practical Workflow Steps
- Initialize the Terraform working directory with terraform init to download the correct provider plugin.
- Configure provider blocks with source and version constraints to avoid breaking changes.
- Define resources for Atlas projects, clusters, and database users using the MongoDB Atlas provider.
- For schema management, define dev_url and target URLs in the Ariga provider block.
- Use terraform apply to provision infrastructure and apply schema changes.
- Output sensitive values such as connection strings for downstream consumption.
Version constraints are recommended for both providers. For the mongodbatlas provider, the suggested constraint is ~> 0.5, which means it will use version 0.5.x but not automatically upgrade to version 1.0 or higher.
For the Ariga provider, the example uses version ~> 0.9.8.
Conclusion
Terraform Atlas encompasses two distinct but complementary capabilities. The MongoDB Atlas Terraform Provider provides full infrastructure as code coverage for MongoDB Atlas Database as a Service, allowing teams to declare projects, clusters, and access controls and apply them consistently with Terraform. It supports versioned and declarative schema workflows via atlasmigration and atlasschema resources, and integrates with project configuration, named database provisioning, and OpenTaco for PR-driven migrations.
The Ariga Atlas Terraform Provider brings schema-first database lifecycle management to Terraform, offering declarative and versioned workflows for relational databases. It enables schema inspection, plan generation, linting, and safe application of migrations using HCL or SQL definitions.
Together they allow organizations to treat both cloud infrastructure and database schemas as code, version them, review changes, and apply them through automated pipelines. Using terraform init to bootstrap providers, applying version constraints, and outputting runtime values remain core practices for both providers. Backing up data before running migration commands and referencing official examples, modules, and landing zone guidance ensures safe adoption at scale.