Get Started with Terraform and the MongoDB Atlas Provider
On this page
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 an Atlas organization API key.
Install Terraform.
Deploy MongoDB Atlas with Terraform
Populate supporting files.
See the 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.
For an example, see the MongoDB Terraform Blog Post.
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.
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
To explore more advanced configurations, extend the fundamentals covered in this tutorial with additional Atlas resources, Atlas Terraform Provider configurations, and Terraform capabilities, see the following resources: