Skip to content

mongodb-partners/aws-mongodb-atlas-offline-first-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Building offline-first app using AWS Amplify, AppSync and MongoDB Atlas

A comprehensive AWS + MongoDB based theme park management system featuring a React Native mobile app, Next.js web portal, and serverless backend infrastructure.

This project demonstrates modern cloud architecture with MongoDB Atlas, AWS services, and real-time data processing.


Project Overview

  • Cross-Platform Mobile App: React Native app for iOS and Android
  • Web Portal: Next.js dashboard for administrators and users
  • Serverless Backend: AWS Lambda + AppSync GraphQL API
  • MongoDB Atlas: Managed cloud database with change streams
  • Secure Authentication: AWS Cognito integration
  • Real-time Updates: Live data synchronization across platforms

Architecture

architecture


Prerequisites

System Requirements

  • Node.js ≥ 20
  • npm or yarn
  • Git
  • AWS CLI ≥ 2.x
  • Terraform ≥ 1.0

Development Tools

  • React Native CLI
  • Xcode (for iOS, macOS only)
  • Android Studio (for Android)

AWS & MongoDB Setup

  1. AWS Account with permissions for:
    • IAM, S3, Lambda, AppSync, API Gateway, Cognito
    • VPC, EC2, CloudFormation, Secrets Manager
  2. MongoDB Atlas Account (cluster + API keys)
  3. AWS CLI configured with credentials

Quick Start Guide


Clone the Repository

git clone <repo-url>
cd <repo-name>

Part 1. Infrastructure Setup with Terraform

cd terraform

# Copy and configure variables
cp terraform.tfvars.example terraform.tfvars
cp backend.hcl.example backend.hcl
# Edit both files with your values

Create an S3 bucket for storing the terraform state in the AWS

Bucket Name themepark-terraform-state-${SOME_UNIQUE_KEY}

Update this bucket name in the backend.hcl file

Important: Backend Configuration

Configure your backend using the backend.hcl file:

# backend.hcl
bucket = "REPLACE THE BUCKET NAME CREATED ABOVE"
key    = "themepark/terraform.tfstate"
region = "us-east-1"

Note: The region in backend.hcl should match your aws_region in terraform.tfvars.


Part 2. Configure AWS CLI

now, configure the AWS CLI with your credentials:

aws configure

This will prompt you for:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default AWS region
  • Default output format

Part 3. MongoDB & Terraform Deployment

Generate MongoDB Atlas Public/Private Keys

You need an Atlas Programmatic API Key.

  1. Log in to the MongoDB Atlas UI.
  2. Navigate to Organization Access ManagerAPI Keys.

api-key

  1. Create a new key with Project Owner role.
  2. Save the Public Key and Private Key.

Paste these into terraform.tfvars as atlas_public_key and atlas_private_key also the mongo project name as atlas_existing_project_name.


3.1. Set Up AWS–MongoDB Unified Role

MongoDB Atlas requires a unified role in AWS to manage resources securely.

Follow this guide to set it up: MongoDB Atlas: Set Up Unified AWS Access

  1. Go to integrations in MongoDB Atlas

    integrations

  2. Click AWS → Authorize on AWS IAM Role → Next → Follow steps and Validate and Finish

    iam-role

  3. Copy the role ARN.

  4. Add it to your terraform.tfvars as mongodb_atlas_stream_role_arn.

Important: Add Lambda Invoke Permission

After creating the unified role, you must add permission for MongoDB Atlas to invoke your Lambda functions. Follow these steps in the AWS Console:

  1. Go to AWS IAM Console

  2. Find the MongoDB Role

    • Search for the role you created (e.g., mdb-stream-integration)
    • Click on the role name to open it
  3. Attach AWS Managed Policy

    • Click on "Add permissions" → "Attach policies"
    • In the search box, type: AWSLambdaRole
    • Check the box next to AWSLambdaRole (AWS managed policy)
    • Click "Add permissions"

3.2. Atlas Private Endpoint

  1. In Atlas, go to Network Access

  2. Go to private endpoint tab → Add Private Endpoint → Choose AWS → Region → Wait for the service endpoint to be created

    service-name

  3. Once the service name is created, copy the service name and paste in the terraform.tfvars as mongodb_atlas_service_name


3.3. Deploy Infrastructure

Initialize Terraform

# Initialize with backend configuration file
terraform init -backend-config=backend.hcl

Preview Plan

terraform plan

Apply Deployment

terraform apply

3.4. Configure Private Endpoint Connection

  1. Copy the VPC endpoint ID from the Terraform deployment outputs

    copy-vpce

  2. In Atlas, go to Network Access → Private Endpoint tab

    empty

  3. VPC ID and Subnet IDs can be empty, go to next and paste the VPC Endpoint ID and then create it

    vpce


Create Cognito User via CLI

USER_POOL_ID=$(terraform output -raw cognito_user_pool_id); aws cognito-idp admin-create-user --user-pool-id $USER_POOL_ID --username [email protected] --user-attributes Name=email,[email protected] Name=email_verified,Value=true Name=given_name,Value=Test Name=family_name,Value=User --temporary-password TempPassword123! --message-action SUPPRESS; aws cognito-idp admin-set-user-password --user-pool-id $USER_POOL_ID --username [email protected] --password TestPassword123! --permanent

Part 4. Mongo Dump for Test Data

Restore rides test data

Restore Test Data


Part 5. Web Portal

Open the Web portal deployed in S3 using the url found in the terraform outputs.

web-portal

The credentials to login to Web Portal are added in terraform.tfvars.example Default Credentials


Part 6. Mobile App Setup

After Terraform deployment completes, the mobile app .env file will be automatically generated with all required AWS and MongoDB configuration values.

Run Mobile Application


Resource Cleanup

Destroy Infrastructure

When you're finished with the POV or need to clean up resources, use Terraform to destroy all AWS resources:

# Navigate to terraform directory
cd terraform

# Review what will be destroyed
terraform plan -destroy

# Destroy all resources
terraform destroy

Troubleshooting

IAM Permissions

  • Ensure AWS user has sufficient permissions
  • Check CloudTrail logs for permission errors

Mobile App Issues

iOS Build Issues:

cd mobile_app/ios
pod deintegrate
pod install

Android Build Issues:

cd mobile_app/android
./gradlew clean

Web Portal Issues

Build Errors:

cd web_portal
rm -rf .next node_modules
npm install
npm run build

About

Building offline-first apps on AWS Amplify, AppSync and MongoDB Atlas

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published