Connecting Python to MongoDB Atlas from AWS Lambda: A Serverless Approach

mdshamsfiroz
3 min readOct 31, 2024

--

In today’s cloud-driven world, serverless architectures are becoming increasingly popular. AWS Lambda, combined with MongoDB Atlas, offers a powerful solution for building scalable, efficient applications. In this blog post, we’ll walk through the process of connecting a Python-based AWS Lambda function to a MongoDB Atlas cluster.

Prerequisites

Before we begin, make sure you have:

  1. An AWS account with Lambda access
  2. A MongoDB Atlas account with a cluster set up
  3. Basic knowledge of Python and AWS Lambda

Step 1: Set Up Your MongoDB Atlas Cluster

If you haven’t already, create a MongoDB Atlas cluster:

  1. Log in to your MongoDB Atlas account
  2. Create a new cluster (or use an existing one)
  3. In the cluster’s security settings, add 0.0.0.0/0 to the IP whitelist (for testing purposes only)
  4. Create a database user with read/write permissions

Step 2: Prepare Your Python Environment

We’ll need to include the pymongo library in our Lambda function. Since Lambda doesn't include this by default, we'll create a deployment package:

  1. Create a new directory for your project
  2. Install pymongo in this directory:
pip install pymongo -t .

Step 3: Create Your Lambda Function

Now, let’s create a Python script for our Lambda function. Name it lambda_function.py:

import json
import pymongo
import os
def lambda_handler(event, context):
# Retrieve the connection string from environment variables
connection_string = os.environ['MONGODB_URI']

# Connect to MongoDB
client = pymongo.MongoClient(connection_string)

# Select the database and collection
db = client['your_database_name']
collection = db['your_collection_name']

# Perform a simple operation (e.g., insert a document)
document = {"name": "John Doe", "email": "john@example.com"}
result = collection.insert_one(document)

# Close the connection
client.close()

return {
'statusCode': 200,
'body': json.dumps(f'Document inserted with id: {result.inserted_id}')
}

Step 4: Create a Deployment Package

  1. Zip all the contents of your project directory:
zip -r deployment_package.zip .

Step 5: Create and Configure the Lambda Function

  1. Go to the AWS Lambda console
  2. Click “Create function”
  3. Choose “Author from scratch”
  4. Name your function and select Python 3.8 (or later) as the runtime
  5. Under “Function code”, upload your deployment_package.zip
  6. Set the handler to lambda_function.lambda_handler

Step 6: Set Environment Variables

In the Lambda function configuration:

  1. Scroll down to “Environment variables”
  2. Add a new variable:
  • Key: MONGODB_URI
  • Value: Your MongoDB Atlas connection string

Step 7: Configure VPC Settings (Optional but Recommended)

For enhanced security:

  1. In your Lambda function configuration, go to “VPC”
  2. Select the VPC, subnets, and security groups that can access your MongoDB Atlas cluster

Step 8: Test Your Function

  1. Create a test event in the Lambda console
  2. Run the test

If successful, you should see a response with the inserted document’s ID.

Best Practices and Considerations

  1. Connection Reuse: In a production environment, consider initializing the MongoDB client outside the handler function to reuse connections.
  2. Error Handling: Add robust error handling to manage potential connection issues or MongoDB operations failures.
  3. Security: In a production setting, avoid using 0.0.0.0/0 in your IP whitelist. Instead, use VPC peering or AWS PrivateLink to secure your connections.
  4. Performance: Monitor your function’s performance and adjust the memory allocation if necessary to improve execution speed.
  5. Timeouts: Be mindful of Lambda’s execution time limits, especially for operations that might take longer to complete.

Conclusion

Connecting Python to MongoDB Atlas from AWS Lambda opens up a world of possibilities for serverless applications. This setup allows you to build scalable, database-driven applications without the need to manage servers.

As you become more comfortable with this setup, you can explore more complex operations, implement connection pooling for better performance, and integrate with other AWS services to build comprehensive serverless solutions.

Remember to always follow best practices for security and performance optimization when working with cloud-based services and databases.

So, whether you’re a tech enthusiast, a professional, or just someone who wants to learn more, I invite you to follow me on this journey. Subscribe to my blog and follow me on social media to stay in the loop and never miss a post.

Together, let’s explore the exciting world of technology and all it offers. I can’t wait to connect with you!”

Connect me on Social Media: https://linktr.ee/mdshamsfiroz

Happy coding! Happy learning!

--

--

mdshamsfiroz
mdshamsfiroz

Written by mdshamsfiroz

Trying to learn tool by putting heart inside to make something

No responses yet