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.
Prerequisites
Before deploying MongoDB Atlas with Terraform, you must:
Create an Atlas account.
Obtain your Organization ID of which you are the
Organization Owner.Tip
You can find your Organization ID in the Atlas UI, under your organization's General Settings.
Configure a Service Account (SA) Authentication. The Organization Owner role is required to view the Organization ID, manage service accounts, and create or delete clusters and other project resources.
Install Terraform.
Deploy MongoDB Atlas with Terraform
Populate supporting files.
See the atlas-basic module and copy the content over to the files you created in the previous step (click the link at the Source Code to see the content).
Note
Your Atlas public key and private key are secrets. Consider storing them as environment variables.
Configure main.tf contents.
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. |
(Optional) Display parameters.
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.
Deploy your infrastructure.
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. The Atlas Terraform provider updates you every ten seconds until it's complete.
Verify the Deployment
After terraform apply completes, confirm the cluster is deployed.
A new cluster appears in the Atlas UI under your project.
If you added output values to
main.tf, the connection string appears in the Terraform output.
Terminate MongoDB Atlas Instance
To delete all of the resources created in your Terraform directory, run the following command:
terraform destroy
Warning
If you delete all your resources, you can't recover them.
When prompted Do you really want to destroy all resources?, enter yes.
Next Steps
After you deploy your cluster, you can:
Create an organization and project: If you are new to Atlas, see Manage Organizations and Projects.
Explore advanced configurations: Review the atlas-basic module and the Atlas Terraform Provider documentation for more complete configurations.
Connect to the cluster: Use the connection string from the cluster object or from your Terraform output values.
Additional resources: