Easy Deployment of MEAN Stack with MongoDB Atlas, Cloud Run, and HashiCorp Terraform
Rate this article
This article was originally written by Aja Hammerly and Abirami Sukumaran, developer advocates from Google.
Serverless computing promises the ability to spend less time on infrastructure and more time on developing your application. But historically, if you used serverless offerings from different vendors you didn't see this benefit. Instead, you often spent a significant amount of time configuring the different products and ensuring they can communicate with each other. We want to make this easier for everyone. We've started by using HashiCorp Terraform to make it easier to provision resources to run the MEAN stack on Cloud Run with MongoDB Atlas as your database. If you want to try it out, our GitHub repository is here:
- MongoDB is responsible for data storage
- Express.js is a Node.js web application framework for building APIs
Our project runs the MEAN stack on Cloud Run (Express, Node) and MongoDB Atlas (MongoDB).
The repository uses a sample application to make it easy to understand all the pieces. In the sample used in this experiment, we have a client and server application packaged in individual containers each that use the MongoDB-Node.js driver to connect to the MongoDB Atlas database.
Below we'll talk about how we used Terraform to make deploying and configuring this stack easier for developers and how you can try it yourself.
To use these scripts, you'll need to have both MongoDB Atlas and Google Cloud accounts.
- Login with your MongoDB Atlas Account.
- Once you're logged in, click on "Access Manager" at the top and select "Organization Access"
- Select the "API Keys" tab and click the "Create API Key" button
- Give your new key a short description and select the "Organization Owner" permission
- Click "Next" and then make a note of your public and private keys
- Next, you'll need your Organization ID. In the left navigation menu, click “Settings”.
- Locate your Organization ID and copy it.
That's everything for Atlas. Now you're ready to move on to setting up Google Cloud!
You'll also need to pick a for your infrastructure. Note that Google Cloud and Atlas use different names for the same region. You can find a mapping between Atlas regions and Google Cloud regions . You'll need a region that supports the M0 cluster tier. Choose a region close to you and make a note of both the Google Cloud and Atlas region names.
atlas_pub_key = "<your Atlas public key>"
atlas_priv_key = "<your Atlas private key>"
atlas_org_id = "<your Atlas organization ID>"
google_billing_account = "<your billing account ID>"
If you selected the us-central1/US_CENTRAL region then you're ready to go. If you selected a different region, add the following to your
atlas_cluster_region = "<Atlas region ID>"
google_cloud_region = "<Google Cloud region ID>"
Run terraform init again to make sure there are no new errors. If you get an error, check your terraform.tfvars file.
You're ready to deploy! You have two options: you can run
terraform plan to see a full listing of everything that Terraform wants to do without any risk of accidentally creating those resources. If everything looks good, you can then run
terraform apply to execute the plan.
If everything looks good to you, type yes and press enter. This will take a few minutes. When it's done, Terraform will display the URL of your application:
Open that URL in your browser and you'll see the sample app running.
When you're done, run terraform destroy to clean everything up:
If you're sure you want to tear everything down, type yes and press enter. This will take a few minutes. When Terraform is done everything it created will have been destroyed and you will not be billed for any further usage.
You can use the code in this repository to deploy your own applications. Out of the box, it will work with any application that runs in a single container and reads the MongoDB connection string from an environment variable called ATLAS_URI, but the Terraform code can easily be modified if you have different needs or to support more complex applications.