Skip to content

Latest commit

 

History

History
171 lines (92 loc) · 8.66 KB

README.md

File metadata and controls

171 lines (92 loc) · 8.66 KB

AWS AppSync Integration with MongoDB Atlas

Introduction

MongoDB Atlas

MongoDB Atlas is a modern Developer Data Platform with a fully managed cloud database at its core. Atlas is the best way to run MongoDB, the leading non-relational database. It provides rich features like flexible schema model, native timeseries collections, geo-spatial data, multi level indexing, RBAC, isolated workloads and many more–all built on top of the MongoDB document model.

MongoDB Atlas App Services help developers build apps, integrate services, and connect to their data without operational overhead utilizing features like hosted Data API and GraphQL API. Atlas Data API allows developers to easily integrate Atlas data into their cloud apps and services over HTTPS with a flexible API. Atlas GraphQL API lets developers access Atlas data from any standard GraphQL client with an API that generates based on your data’s schema.

AWS AppSync

AWS AppSync is an AWS product that allows developers to build GraphQL and Pub/Sub APIs. With AppSync developers can create APIs that access data from one or many sources enabling real-time interactions in their applications. The resulting APIs are serverless making it possible for the client to query only for the data it needs, in the format it needs, and pay only for the requests and the real-time messages delivered.

Salient Features

Authentication and Authorization through Amazon Cognito Simple, Secured, Easy GraphQL APIs Caching to improve performance End-to-end serverless architecture AutoScaling based on API request volumes Subscriptions to support real-time updates

Pre-requisite

Developer Tool: VSCode, AWS CLI, Docker, Postman

Architecture

Steps for Integration

1.Create a MongoDB Atlas cluster

Please follow the link to setup a free cluster in MongoDB Atlas

Configure the database for network security and access.

create documents for customer and risk . please refer the sample structure for CounterParty and CounterPartyRisk documents.

2.Create MongoDB Data API

Create the Data APIs using the link

3.Store the API Key in AWS Secrets

Copy the API Key and MongoDB Credentials to a local JSON file. Template for the JSON files shown in the link: mycreds.json & myapikey.json

  aws secretsmanager create-secret --name <secret_name> \
      --description "MongoDB secrets created for AWS AppSync" \
      --secret-string file://mycreds.json
      
  aws secretsmanager create-secret --name <secret_name> \
      --description "API Keys secret created for AWS AppSync" \
      --secret-string file://myapikey.json

4. crerate a AWS Elastic Container Repository

  aws ecr create-repository \
              --repository-name partner_atlas_appsync_int \
              --image-scanning-configuration scanOnPush=true \
              --region <aws_region>

5.create the docker image and deploy to lambda

The code base contain 3 repos: data_api_appsync, driver_appsync (datasource_create & datasource_read). Copy the appropriate code to the VSCode.

Note: AWS ECR repositories and Lambda functions are to be created for each of the 3 repos, viz: data_api_appsync / driver_appsync (read / create)

update the DATA API endpoints and the database credentials appropriate for your requirement.

create the docker image

  aws ecr get-login-password --region <aws region> | docker login --username AWS --password-stdin <accountid>.dkr.ecr.<aws region>.amazonaws.com
  
  docker build -t partner_atlas_appsync_int . --platform=linux/amd64
  
  docker tag partner_test:latest <accountid>.dkr.ecr.<aws region>.amazonaws.com/partner_atlas_appsync_int:latest
  
  docker images
  
  docker push <accountid>.dkr.ecr.<aws region>.amazonaws.com/partner_atlas_appsync_int:latest

6.Create the Lambda function

  aws lambda create-function --region <aws region>  --function-name partner_atlas_appsync_int \
      --package-type Image  \
      --code ImageUri= <accountid>.dkr.ecr.<aws region>.amazonaws.com/partner_atlas_appsync_int:latest   \
      --role <Lambda execution role ARN>

pls check the link for reference code

Note: Ensure the lambda function is having adequate permission to read from the secret manager.

7.Create the AWS AppSync API

a. Create API

  1. Choose Build from scratch and hit Start, for getting a blank API.

AppSync_Creation_Step1

  1. Add API Name

Screenshot 2022-10-28 at 5 36 25 PM

b. Add Schema

  1. Click on Schema form the left navigations

Screenshot 2022-10-28 at 5 36 56 PM

  1. GraphQL Schema can be built with Queries and Mutations and click Save Schema

Screenshot 2022-10-28 at 5 37 45 PM

c. Create Data Sources

  1. Click on Data Sources from the left navigations and hit Create data source

Screenshot 2022-10-28 at 5 38 46 PM

  1. Attach Lambda function which contains MongoDB driver code for quering the data (Multiple data sources can be added based on requirements)

Screenshot 2022-10-28 at 5 40 00 PM

d. Attach Resolvers

  1. Go to schema and attach resolvers for Mutations and Queries

Screenshot 2022-10-28 at 6 43 22 PM

  1. Select lambda data source added in previous step, and click save.

Screenshot 2022-10-28 at 6 44 32 PM

8. Test the API

a. Using AWS Management Console

Goto Queries and select the query to execute, hit Run button and you should see your query result on the right side.

Screenshot 2022-10-28 at 6 46 20 PM

b. Using Postman
  1. Click on settings and create a curl API as below.

         curl --location --request <CRUD OPERATION> <API URL>
         --header 'Content-Type: application/graphql' \
         --header 'x-api-key: <API KEY> \
         --data-raw '{"query": <QUERY>}'
    

Screenshot 2022-10-28 at 8 14 14 PM

  1. Goto Postman and import the curl command and hit send to get the response.

Screenshot 2022-10-28 at 7 41 33 PM

Summary

This solution can be extended to AWS Amplify for building mobile applications.

For any further information, please contact [email protected]