To create a Lambda function that can perform CRUD operations on MongoDB, first make sure that you deploy a MongoDB instance to an EC2 instance as discussed here:
Install and configure MongoDB community edition
Once you verify that the MongoDB instance is succesfully deploy and running, now you can write a Lambda function that perform CRUD operations. I would write the Lambda function using the Lambda runtime Java API and then use the Mongo Java API to interact with a MongoDB collection.
For example, create a MongoClient instance in your Lambda function.
private String mongoUri = "mongodb://<ENTER EC2 IP Address>.amazonaws.com:27017" ;
private MongoClient getConnection() {
try {
MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoUri));
return mongoClient;
} catch (Exception e) {
e.getStackTrace();
}
return null;
}
Now you can perform CRUD operations from within a Lambda Function.
If you want to use API Gateway, you can invoke this Lambda function using API Gateway.
contextandevent.proxy integrationas well astemplate mappingto get theresource namein the lambda.