Manage Data on Your Atlas Cluster Using AWS AppSync Lambda Resolvers
You can manage data on your Atlas cluster using AWS AppSync Direct Lambda Resolvers. The Lambda function, serving as a resolver for the AWS AppSync GraphQL API, enables seamless CRUD operations on your data in your Atlas cluster. Integrating Atlas with AWS AppSync offers a powerful, flexible solution for building scalable, serverless GraphQL APIs.
This integration not only reduces the complexity of managing multiple data sources, but also enhances the development experience by providing a unified and flexible platform for modern application architectures. Whether you're handling CRUD operations or optimizing payloads, this integration enables you to build scalable, efficient, and cost-effective APIs.
This page walks you through the following steps:
Deploy the CloudFormation stack that contains the Serverless Lambda function and an AWS-managed AppSync GraphQL API.
Test the AppSync API by running queries from the AWS AppSync API console or Postman.
Prerequisites
Before you begin, you must do the following:
Deploy an Atlas cluster
Configure the database for network security and access
Set up an AWS account with access to the AWS Management Console
Procedure
Create the Serverless Lambda function in AWS.
Log in to the AWS Management Console and navigate to Applications, which is located on the left sidebar within the AWS Lambda console.
Click Create application and then select the Serverless application tab.
Type
MongoDB-AppSync-App
in the search bar and then select the checkbox to show apps that create custom IAM roles or resource policies.Click the
MongoDB-AppSync-App
App in the search results.Paste the Atlas connection string in the AtlasConnectionString field under Application settings.
Select the checkbox to create IAM roles and click Deploy to deploy the CloudFormation stack.
The CloudFormation stack initiates the creation of:
A Lambda function containing the essential logic required to resolve queries from the AppSync API.
An AppSync GraphQL API containing a schema that defines Queries and Mutations. These Queries and Mutations are linked to the Lambda function established by the CloudFormation stack.
When the deployment completes, copy the following in the Outputs section of the CloudFormation stack:
GraphQLApiKey
, which is the AppSync API KeyGraphQLAPIURL
, which is the AppSync API URLLambdaFunction
, which is the ARN of the Lambda Function
Test the AppSync API with a Lambda resolver as its data source.
You can use the AWS AppSync console or Postman to test the AppSync API.
The API request and response payloads must be in the MongoDB Atlas Data API (v1) format. Each query must include the necessary request query and body parameters. To learn more about the Data API syntax, see MongoDB Atlas Data API (v1):
Log in to the AWS Management Console and navigate to the AWS AppSync page.
Click the link to the
MongoDB-AppSync-SAM_API
.Click Queries in the left navigation under MongoDB-AppSync-SAM_API.
Select the Explorer (third icon) under Queries.
Select the type of query and enter the query.
You can run any of the following types of query:
aggregate
find
andfindOne
insertOne
andinsertMany
updateOne
andupdateMany
deleteOne
anddeleteMany
If you type the query in the input field, the escape characters are automatically added to your query.
Click the play button to run the query.
The output contains escape sequence characters. We recommend implementing a logic in your application to convert the escape sequence into JSON.
Download the Postman collection named
postman.json
from GitHub.Open Postman and click Import.
Complete the steps to import the downloaded
postman.json
file.To query, do the following:
Send
POST
request to theGraphQLAPIURL
.Authenticate with
GraphQLApiKey
.
Click Try to run the query.