BlogAnnounced at MongoDB.local NYC 2024: A recap of all announcements and updatesLearn more >>
MongoDB Developer
MongoDB
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
MongoDBchevron-right

Leveraging Atlas Vector Search With HashiCorp Terraform: Empowering Semantic Search in Modern Applications

Rutuja Rajwade, Zuhair Ahmed4 min read • Published May 02, 2024 • Updated May 02, 2024
TerraformMongoDB
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Last year, MongoDB announced the general availability of Atlas Vector Search, a new capability in Atlas that allows developers to search across data stored in MongoDB based on its semantic meaning using high dimensional vectors (i.e., “embeddings”) created by machine learning models.
This allows developers to build intelligent applications that can understand and process human language in a way traditional, text-based search methods cannot since they will only produce an exact match for the query.
For example, searching for “warm winter jackets” on an e-commerce website that only supports text-based search might return products with the exact match keywords "warm," "winter," and "jackets." Vector search, on the other hand, understands the semantic meaning of "warm winter jackets'' as apparel designed for cold temperatures. It retrieves items that are not only labeled as "winter jackets” but are specifically designed for warmth, including products that might be described with related terms like "insulated," giving users more helpful search results.
Integrating Atlas Vector Search with infrastructure-as-code (IaC) tools like HashiCorp Terraform can then streamline and optimize your development workflows, ensuring that sophisticated search capabilities are built directly into the infrastructure deployment process.
This guide will walk you through how to get started with Atlas Vector Search through our HashiCorp Terraform Atlas provider. Let’s get started!

Pre-requisites

Step 1: Deploy Atlas dedicated cluster with Atlas Search Nodes

First, we need to deploy basic Atlas resources to get started. This includes an Atlas project, an M10 dedicated Atlas cluster (which is pay-as-you-go, great for development and low-traffic applications), a database user, and an IP Access List Entry.
Note: When configuring your MongoDB Atlas cluster with Terraform, it's important to restrict IP access to only the IP address from which the Terraform script will be deployed. This minimizes the risk of unauthorized access.
In addition, as part of this tutorial, we will be using Atlas Search Nodes (optional). These provide dedicated infrastructure for Atlas Search and Vector Search workloads, allowing you to fully scale search independent of database needs. Incorporating Search Nodes into your Atlas deployment allows for better performance at scale and delivers workload isolation, higher availability, and the ability to optimize resource usage.
Lastly, when using Terraform to manage infrastructure, it is recommended to maintain organized file management practices. Typically, your Terraform configurations/scripts will be written in files with the .tf extension, such as main.tf. This file, which we are using in this tutorial, contains the primary configuration details for deploying resources and should be located ideally in a dedicated project directory on your local machine or on Terraform Cloud.
See the below Terraform script as part of our main.tf file:
Note: Before deploying, be sure to store your MongoDB Atlas programmatic API keys created as part of the prerequisites as environment variables. To deploy, you can use the below commands from the terminal:

Step 2: Create your collections with vector data

For this tutorial, you can create your own collection of vectorized data if you have data to use.
Alternatively, you can use our sample data. This is great for testing purposes. The collection you can use is the "sample_mflix.embedded_movies" which already has embeddings generated by Open AI.
To use sample data, from the Atlas UI, go into the Atlas cluster Overview page and select “Atlas Search” at the top of the menu presented. (Cluster Overview in the Atlas UI)
Then, click “Load a Sample Dataset.” (Atlas Search menu in the Atlas UI)

Step 3: Add vector search index in Terraform configuration

Now, head back over to Terraform and create an Atlas Search index with type “vectorSearch.” If you are using the sample data, also include a reference to the database “sample_mflix” and the collection “embedded_movies.”
Lastly, you will need to set the “fields” parameter as per our example below. See our documentation to learn more about how to index fields for vector search and the associated required parameters.
To deploy again, you can use the below commands from the terminal:
If your deployment was successful, you should be greeted with “Apply complete!” (Terraform in terminal showcasing deployment)
To confirm, you should be able to see your newly created Atlas Search index resource in the Atlas UI with Index Type “vectorSearch” and Status as “ACTIVE.” (Atlas UI showcasing Vector Search deployment)

Step 4: Get connection string and connect to the MongoDB Shell to begin Atlas Vector Search queries

While still in the Atlas UI, go back to the homepage, click “Connect” on your Atlas cluster, and select “Shell.”
(Atlas UI page where you can choose a cluster connection method)
This will generate your connection string which you can use in the MongoDB Shell to connect to your Atlas cluster.

All done

Congratulations! You have everything that you need now to run your first Vector Search queries.
With the above steps, teams can leverage Atlas Vector Search indexes and dedicated Search Nodes for the Terraform MongoDB Atlas provider to build a retrieval-augmented generation, semantic search, or recommendation system with ease.
The HashiCorp Terraform Atlas provider is open-sourced under the Mozilla Public License v2.0 and we welcome community contributions. To learn more, see our contributing guidelines.
The fastest way to get started is to create a MongoDB Atlas account from the AWS Marketplace or Google Cloud Marketplace. To learn more about the Terraform provider, check out the documentation, solution brief, and tutorials, or get started today.
Go build with MongoDB Atlas and the HashiCorp Terraform Atlas provider today!

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
News & Announcements

Halting Development on MongoDB Swift Driver


Apr 04, 2024 | 1 min read
Quickstart

Quick Start: BSON Data Types - Date


Sep 23, 2022 | 2 min read
Tutorial

Real-time Data Architectures with MongoDB Cloud Manager and Verizon 5G Edge


Jan 23, 2024 | 8 min read
Tutorial

Currency Analysis with Time Series Collections #1 — Generating Candlestick Charts Data


May 31, 2023 | 6 min read
Table of Contents