Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

Securely Hosting a Lambda Application With a Microservice Architecture and MongoDB Atlas

BS
AS
Babu Srinivasan, Igor Alekseev, Ashwin S.4 min read • Published Sep 19, 2024 • Updated Sep 19, 2024
Atlas
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
In this tutorial, you'll discover how to securely host a Lambda application designed with a microservice architecture, utilizing AWS Lambda, Amazon API Gateway, and MongoDB Atlas. AWS Lambda is pivotal for enterprises, offering standardized serverless deployment, enhanced security, and streamlined management, which collectively lead to consistent performance and simplified operations.
This guide not only details the process of setting up and deploying a MongoDB Atlas-based application with a microservice and API pattern but also provides a robust framework applicable to any such deployment. By leveraging AWS CDK for automated deployment, you ensure a scalable, efficient, and secure solution that meets enterprise needs and accelerates your deployment process across various environments.
To follow along with this tutorial, you should have intermediate proficiency with AWS and MongoDB services.

Architecture diagram

AWS Lambda architecture diagram
The architecture described here includes the following components:
  1. AWS Serverless Application Repository: Public Repository for AWS Lambda application 
  2. AWS Lambda: Hosts the microservices logic
  3. Amazon API Gateway: Exposes the Lambda functions as RESTful APIs
  4. MongoDB Atlas: Stores the application data securely

Implementation steps to set up your Lambda application

This tutorial assumes us-east-1 as the default region. Please update the scripts to your specific region. if required.

Prerequisites

Before proceeding, ensure you have the following prerequisites installed:

Setting up the environment

Clone the application repository.
1git clone https://github.com/mongodb-partners/Microservice-api-lambda-MongoDBAtlas
2cd Microservice-api-lambda-MongoDBAtlas
Configure AWS CLI.
1aws configure
Enter your AWS Access Key ID, Secret Access Key, and preferred region.
Set up the Python virtual environment.
1python3 -m venv .venv
2source .venv/bin/activate
3pip3 install -r lambda/python-crud-lambda/requirements.txt
To build the project locally, execute the following command:
1sam build
This command will locally build the project, and output files will be saved in the .aws-sam folder.

Set up MongoDB Atlas 

Use an existing connection string: Ensure you have the complete MongoDB SRV connection string with your username and password included. Ensure that Lambda can access your cluster. For testing purposes, you can use 0.0.0.0/0 in your network IP access list. In production, consider setting up a VPC peering connection between your AWS VPC and the Atlas VPC. This will restrict public access and only accept connections from your VPC. Learn more about VPC peering in the MongoDB Atlas documentation.

Deploying the application

Execute the following command to deploy the application:
bash
1sam deploy --guided
Follow the guided deploy instructions:
  • Stack Name: Enter a name for your stack.
  • AWS Region: Enter your preferred region.
  • Parameter ConnectionString: Enter your MongoDB connection string.
  • Parameter DBName: Enter the name of your MongoDB database.
  • Parameter CollectionName: Enter the name of your MongoDB collection.
Sample values for deployment
1 Setting default arguments for 'sam deploy'
2 =========================================
3 Stack Name [sam-app]: sam-app-api-mdb
4 AWS Region [us-east-1]:
5 Parameter ConnectionString []: mongodb+srv://test1:test1@demo.mongodb.net/?retryWrites=true&w=majority
6 Parameter DBName [test]: sam-app-api-mdb
7 Parameter CollectionName [test]: microservice-lambda-app
8 #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
9 Confirm changes before deploy [y/N]: y
10 #SAM needs permission to be able to create roles to connect to the resources in your template
11 Allow SAM CLI IAM role creation [Y/n]: y
12 #Preserves the state of previously provisioned resources when an operation fails
13 Disable rollback [y/N]: y
14 CRUDLambda has no authentication. Is this okay? [y/N]: y
15 Save arguments to configuration file [Y/n]: y
16 SAM configuration file [samconfig.toml]:
17 SAM configuration environment [default]:
Sample deployment screenshot
Wait for the CloudFormation stack to finish creating the stack. After deployment, you will receive the API Gateway link in the outputs.
Cloud formation stack completion
AWS Cloudformation console

Create a S3 bucket

Create an S3 public bucket to store the Lambda code and restrict the access through bucket policy.
1aws s3api create-bucket --bucket aws-sam-cli-mongodb-app1 --region us-east-1
2
3aws s3api put-bucket-policy --bucket my-unique-bucket-name --policy '{
4 "Version": "2012-10-17",
5 "Statement": [
6 {
7 "Effect": "Allow",
8 "Principal": {
9 "Service": "serverlessrepo.amazonaws.com"
10 },
11 "Action": "s3:GetObject",
12 "Resource": "arn:aws:s3:::aws-sam-cli-mongodb-app1/*",
13 "Condition" : {
14 "StringEquals": {
15 "aws:SourceAccount": "<aws account id >"
16 }
17 }

Packaging the application

Package the serverless application model (SAM) deployed application to the S3 bucket created in the previous steps of SAM build.
1sam package --template-file template.yaml --output-template-file packaged.yaml --s3-bucket aws-sam-cli-mongodb-app1

Publishing the application

To publish your application as a serverless application model package, execute the following command:
bash
1sam publish --template packaged.yml --region us-east-1
Follow the instructions to publish your application. By default, your application will be published as a private app. After publication, you can convert it to a public app if needed. For detailed guidance, refer to the SAM documentation on publishing an application.

Verification and validation

Use the API Gateway link received in the deployment outputs to test the APIs. You can use Postman or any other request-making tool.
  • Postman collection: Import the Postman collection postman/Python-Microservice.postman_collection.json into Postman to test the API.

Clean up

Use the following command to clean up all the AWS resources created during deployment:
1sam delete

Troubleshooting

Refer to the GitHub repository to resolve common issues encountered when using AWS SAM with MongoDB Atlas.

Useful commands

  • sam build: Builds the project locally
  • sam deploy --guided: Deploys the application with guided prompts
  • sam publish --template packaged.yaml: Publishes the application as a SAM package
  • sam delete: Deletes the deployed application
By following these steps, you can set up a private hosting environment for your microservices-based Lambda application using AWS services and MongoDB Atlas. This setup ensures a secure, scalable, and cost-effective solution for hosting serverless microservices applications.
Try out the AWS API Gateway! For any MongoDB related queries, visit the MongoDB community support.
Top Comments in Forums
There are no comments on this article yet.
Start the Conversation

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Build a JavaScript AI Agent With LangGraph.js and MongoDB


Sep 18, 2024 | 15 min read
Tutorial

Discover Your Ideal Airbnb: Implementing a Spring Boot & Atlas Search With Kotlin Sync Driver


Oct 02, 2024 | 8 min read
Tutorial

How to Improve LLM Applications With Parent Document Retrieval Using MongoDB and LangChain


Dec 13, 2024 | 15 min read
Tutorial

How to Do Semantic Search in MongoDB Using Atlas Vector Search


Sep 18, 2024 | 8 min read
Table of Contents
  • Architecture diagram