EventoObtenha 50% de desconto no seu ingresso para MongoDB.local Londres em outubro 2. Use o código WEB50Saiba mais >>
Desenvolvedor MongoDB
Central de desenvolvedor do MongoDBchevron-right
Produtoschevron-right
Atlaschevron-right

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

BS
como
Babu Srinivasan, Igor Alekseev, Ashwin S.4 min read • Published Sep 19, 2024 • Updated Sep 19, 2024
Atlas
Ícone do FacebookÍcone do Twitterícone do linkedin
Avalie esse 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.
Para acompanhar este tutorial, você deve ter proficiência intermediária com os serviços da AWS e do MongoDB.

Diagrama de arquitetura.

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.

Pré-requisitos

Before proceeding, ensure you have the following prerequisites installed:

Configuração do ambiente

Clone the application repository.
Configure AWS CLI.
Enter your AWS Access Key ID, Secret Access Key, and preferred region.
Set up the Python virtual environment.
To build the project locally, execute the following command:
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.

Implantando o aplicativo

Execute the following command to deploy the application:
bash
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
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.

Packaging the application

Package the serverless application model (SAM) deployed application to the S3 bucket created in the previous steps of SAM build.

Publishing the application

To publish your application as a serverless application model package, execute the following command:
bash
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.

Limpeza

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

Solução de problemas

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

Comandos úteis

  • 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.
Principais comentários nos fóruns
Ainda não há comentários sobre este artigo.
Iniciar a conversa

Ícone do FacebookÍcone do Twitterícone do linkedin
Avalie esse Tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Relacionado
Início rápido

Introdução ao desenvolvimento do Atlas Stream Processing


Aug 28, 2024 | 7 min read
Tutorial

Como implementar fluxos de trabalho do Databricks e o Atlas Vector Search para melhorar a precisão da pesquisa de comércio eletrônico


Sep 18, 2024 | 6 min read
Artigo

Multi-agent Systems With AutoGen and MongoDB


Sep 18, 2024 | 10 min read
Tutorial

Simplifique a pesquisa semântica do Atlas com LangChain e MongoDB


Sep 18, 2024 | 4 min read
Sumário
  • Diagrama de arquitetura.