Visual Studio Code Mongodb



MongoDB for VS Code is currently available as a Preview in the Visual StudioMarketplace. The product, its features, and the correspondingdocumentation may change during the Preview stage.

This page outlines how to use the Atlas template for Terraform filesincluded with the MongoDB Extension for Visual Studio Code to createShared Tier Atlas clusters.

After loading the template, you configure the cluster and providedetails about your Atlas account. You then use Terraformcommands to plan, apply, and destroy your Atlas cluster.

Prerequisites¶

  • Create a Cosmos DB database resource for MongoDB Create a Cosmos resource first because this will take several minutes. In Visual Studio Code, select the Azure icon in the left-most menu, then select the Databases section. If the Databases section isn't visible, make sure you have checked the section in the top Azure.
  • The CosmosDB Extension was created by Microsoft to make it easier to manage and work with CosmosDB databases inside of Visual Studio Code. But since CosmosDB supports the MongoDB API, the extension works perfectly for any MongoDB database. This article will treat CosmosDB as just another MongoDB database.

Browse other questions tagged javascript node.js mongodb express visual-studio-code or ask your own question. The Overflow Blog Podcast 324: Talking apps, APIs, and open source with developers from Slack. Mint: A new language designed for building single page applications.

Before you use the Atlas template for Terraform filesincluded with the MongoDB Extension for Visual Studio Code, you must:

  • Have an Atlas account.
  • Have an Atlas organization.
  • Have an API key in an organization with theOrganization Owner or Organization Project Creatorrole.

Procedures¶

Create an Atlas Terraform File using the Template¶

Use the Atlas template for Terraform files included with the MongoDB for VS Codeto configure an Atlas cluster:

In Visual Studio Code, create a new file.¶

Operating SystemMethodActions
AnyVisual Studio Code MenuFile > New File
MacOSKeyboard ShortcutPress Command + N
Windows and LinuxKeyboard ShortcutPress Control + N

Save the file. Name it main.tf

main.tf is the recommended filename for the entry point to aTerraform module.

In the main.tf file, type atlas, then press the Enter or Return key.¶

The MongoDB Extension for VSCode populates the file with an exampleconfiguration using the MongoDB AtlasTerraform provider to create a Shared Tier Atlas cluster.

Update the Atlas Terraform configuration to configure your cluster.¶

The cursor moves to select the value of the name argument of themongodbatlas_project resource.

Provide values for the following arguments to configure your cluster:

Press the Tab key to select to the next argument inthe template that you should update.

AttributeValue
.name
Name of the Atlas project that this configuration creates.
.name
Name of the Atlas cluster that this configuration creates.
.backing_provider_name

Provider on which the Atlas cluster that this configurationcreates is hosted. Choose one of the following accepted values:

  • AWS
  • AZURE
  • GCP
.provider_region_name

Region to which the Atlas cluster that this configurationcreates is deployed. Ensure that the region you chose supportsAtlas clusters of the instance size you want to deploy.

By default the template provisions Shared Tier clusters:M2 and M5.

For details about the instance sizes that each provider andregion supports, see the following sections in the Atlasdocumentation:

.provider_instance_size_name

Instance size of the Atlas cluster that this configurationcreates.

Either:

  • Choose one of the Shared Tier instance sizes included in thetemplate: M2 or M5.
  • Enter another instance size that Atlas supports for yourchosen provider and region.

For details about the instance sizes that each provider andregion supports, see the following sections in the Atlasdocumentation:

.disk_size_gbs

Disk size of the Atlas cluster that this configurationcreates. Ensure that you provide a value that is equal to orless than the maximum disk size for the instance size youchose.

  • For M2 clusters, enter 2.
  • For M5 clusters, enter 5.

For details about the disk sizes that each provider supportsfor each instance size, see the following sections in theAtlas documentation:

Update the local variables.¶

Studio

The local variables contain sensitive information. Do notcheck these values in to a repository that is available publicly.

Provide values for the following local variables:

VariableValue
mongodb_atlas_api_pub_keyAtlas public API key.
mongodb_atlas_api_pri_keyAtlas private API key.
mongodb_atlas_org_idAtlas organization ID in which you want to create aproject.
mongodb_atlas_database_usernameUsername of the MongoDB database user that Atlas createsfor your cluster.
mongodb_atlas_database_user_passwordPassword for the MongoDB database user named inmongodb_atlas_database_username.
mongodb_atlas_whitelistipIP address or CIDR block from which your Atlas cluster isaccessible.

To maximize security, consider taking the following steps:

  1. Define the local variables in an input variablesfile.

  2. Exclude the input variables file from your repository. Forexample, add the filename to the .gitignore file for yourrepository.
  3. Reference variables from the input variables file in themain.tf file by prefacing them with vars..

Add optional configuration options to the main.tf file.¶

For a complete list of supported configuration options, see theMongoDB Atlas Terraform Provider documentation.

Shared Tier Atlas clusters don't support all configurationsavailable with the MongoDB Atlas Terraform provider. For a list ofunsupported configurations, see the Atlas documentation.

Save the main.tf file.¶

Create the Atlas Cluster using Terraform¶

After you create a Terraform file using the template, create theAtlas cluster:

Navigate to the directory in which you saved your main.tf file.¶

Run the terraform init command to install the required providers.¶

The following output indicates that the MongoDB AtlasTerraform Provider is installed and ready for use:

Run the terraform plan command to view what happens when you apply the configuration.¶

The following output shows what happens when you apply the Terraform configuration:

Proceed to the next step if the information is correct.

Run the terraform apply command to create an Atlas cluster from the Terraform configuration.¶

Type yes when prompted to confirm that you want to apply theconfiguration.

The terraform apply command might take several minutes tocomplete.

The following output indicates that your Atlas cluster is created.You can create a connection to thisdeployment using the MongoDB for VS Code with the displayed connection strings:

Delete the Atlas Cluster using Terraform¶

Deleting a cluster destroys databases, collections, and documentsstored on it and all other resources defined in the Terraformconfiguration in which you configured the cluster.

Proceed with caution.

Visual Studio Code Mongodb

To delete the Atlas cluster:

Navigate to the directory in which you saved your main.tf file.¶

Run the terraform destroy command to install the required providers.¶

Vs Code Mongodb

Type yes when prompted to confirm that you want to destroy theresources defined in the configuration.

The terraform destroy command might take several minutes tocomplete.

Visual Studio Code Mongodb

Mongodb With Visual Studio Code

The following output indicates that the Atlas cluster and allassociated resources are deleted:

© MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.

Azure Data Studio Mongodb

Use require() to Include External ModulesReference