Docs Menu
Docs Home
/
MongoDB Atlas
/ /

Get Started with Terraform and the MongoDB Atlas Provider

On this page

  • Prerequisites
  • Deploy MongoDB Atlas with Terraform
  • Terminate MongoDB Atlas Instance
  • Next Steps

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:

  • 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.

1
mkdir terraform-proj
cd terraform-proj
2

Create the main.tf, provider.tf, variables.tf, and versions.tf files.

touch main.tf provider.tf variables.tf versions.tf
3

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.

4

Update the following fields in your main.tf file:

Field
New Value

instance_size

"M0"

backing_provider_name

Your provider. Possible values are: "AWS", "AZURE", or "GCP".

region_name

See Cloud Providers and Regions for all the regions you can use.

5

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.

6

To initialize your project, run the following command:

terraform init

This command also downloads and installs the MongoDB Atlas Provider, if you haven't already.

7

To view your execution plan, run the following command:

terraform plan

Terraform details the changes that it plans to make. If the output is not what you expect, then there might be an issue in your main.tf file.

8

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.

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.

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:

Back

Infrastructure as Code