DEV Community

Cover image for Building a Scalable Pet Rehoming Platform with AWS Serverless Architecture
Randika Madhushan Perera
Randika Madhushan Perera

Posted on

Building a Scalable Pet Rehoming Platform with AWS Serverless Architecture

Introduction

PawReHome is a modern, full-featured pet rehoming platform that connects loving families with pets in need. Build with a serverless-first approach, the platform leverages AWS managed services to provide a scalable, cost-effective and secure solution for animal rescue organizations and induvial users.

This article explores the AWS architecture behind the PawReHome, how several AWS services work together to create a robust, role based access control, real-time data management.

Project Overview

PawReHome is a application fully integrated with AWS backend services. The platform supports:

  1. User authentication and authorization with role-based access control
  2. Multi-tenant organization management for rescue groups
  3. Pet listing management with approval workflows
  4. Image upload for pet photos
  5. Adoption applications and inquiry systems
  6. Platform administration for content moderation
  7. Featured pets curation for homepage visibility

AWS Architecture Overview

This utilizes a serverless architecture that eliminates server management overhead and provides automatic scaling.

Complete AWS services stack:

  • AWS Amplify: Frontend hosting, CI/CD and deployment
  • Amazon Cognito: User authentication and authorization
  • AWS Lambda: Serverless backend functions
  • Amazon API Gateway: RESTFUL API management and routing
  • DynamoDB: NoSQL database for all application data
  • S3: Object storage for pet images
  • IAM: Identity and access management

Architecture Flow

High-Level Architecture Diagram

High-Level Architecture Diagram

Deep Dive: AWS Services Implementation

1. AWS Amplify - Frontend Hosting & CI/CD

Purpose: As you can see Amplify hosts the Next.js frontend application and provides continues deployment capabilities.

Implementation Details:

  • The amplify.yml configuration defines the build process
  • Automatic builds triggered on Git commits
  • Environment variables managed through Amplify Console
  • Global CDN ensures low latency worldwide
  • Built-in SSL certificate provisioning via AWS Certificate Manager

Benefits of the Amplify

  • Zero server management, you do not need to worry about.
  • Automatic scaling when traffic spikes.
  • Preview deployment.
  • Rollback capabilities when you have an issue.

2. Cognito - Authentication & Authorization

Cognito provides secure user access and authentication across your application. This has role-based access control (RBAC) thorough Cognito groups.

Let me explain the Authorization Flow:

  1. User logs in via Cognito
  2. Cognito issues JWT token with user claims
  3. Token includes cognito:groups claim with user's groups
  4. API Gateway validates token
  5. Lambda functions check group membership for authorization
  6. DynamoDB queries are filtered based on user permissions

3. Lambda - Serverless Backend Functions

AWS Lambda functions handle all the business logic, data processing and API responses without managing servers. How relief :)

Example Lambda function (pet listing):

const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const { DynamoDBDocumentClient, PutCommand, ScanCommand } = require("@aws-sdk/lib-dynamodb");

const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}));
const TABLE_NAME = "PawReHome-Pets";

exports.handler = async (event) => {
  // Handle pet rehoming submission
  if (event.httpMethod === "POST") {
    const pet = {
      id: uuidv4(),
      ...JSON.parse(event.body),
      status: "pending",
      createdAt: new Date().toISOString()
    };

    await docClient.send(new PutCommand({
      TableName: TABLE_NAME,
      Item: pet
    }));

    return { statusCode: 201, body: JSON.stringify(pet) };
  }

  // Get approved pets
  if (event.httpMethod === "GET") {
    const result = await docClient.send(new ScanCommand({
      TableName: TABLE_NAME,
      FilterExpression: "#status = :status",
      ExpressionAttributeNames: { "#status": "status" },
      ExpressionAttributeValues: { ":status": "approved" }
    }));

    return { statusCode: 200, body: JSON.stringify(result.Items) };
  }
};
Enter fullscreen mode Exit fullscreen mode

4. Amazon API Gateway - RESTful API Management

API gateway servers as the front door for all backend API requests, providing routing, authentication and rate limiting. (Do not forget about the rate limit, your pocket will empty 😉 and will have some concerns. 🛡️)

API Gateway Configuration:

REST API endpoint
CORS enabled for frontend cross-origin requests
Cognito Authorizer for automatic JWT validation
Lambda Proxy Integration for seamless function invocation
Stage Variables for environment management

5. DynamoDB - NoSQL Database

DynamoDB stores all application data with single-digit millisecond latency and automatic scaling.

Table Design as below:

PawReHome-Pets

Partition Key: id (String)
Attributes: name, species, breed, age, size, gender, description, 
           images[], userId, organizationId, status, featured, 
           createdAt, updatedAt
GSIs:
  - UserIdIndex (userId) - Query user's pets
  - OrgIdIndex (organizationId) - Query organization's pets
Enter fullscreen mode Exit fullscreen mode

PawReHome-Organizations

Partition Key: id (String)
Attributes: name, slug, email, phone, description, logo, 
           status, verified, createdAt, reviewedBy, reviewedAt
GSIs:
  - SlugIndex (slug) - Query by URL slug
Enter fullscreen mode Exit fullscreen mode

PawReHome-OrgMembers

Partition Key: organizationId (String)
Sort Key: userId (String)
Attributes: role (admin/member), joinedAt, invitedBy
GSIs:
  - UserIdIndex (userId) - Query user's organizations
Enter fullscreen mode Exit fullscreen mode

PawReHome-Applications

Partition Key: id (String)
Attributes: petId, userId, status, applicantInfo, 
           submittedAt, reviewedAt
GSIs:
  - UserIdIndex (userId) - Query user's applications
  - PetIdIndex (petId) - Query pet's applications
Enter fullscreen mode Exit fullscreen mode

PawReHome-Questions

Partition Key: id (String)
Attributes: petId, userId, status, applicantInfo, 
           submittedAt, reviewedAt
GSIs:
  - UserIdIndex (userId) - Query user's applications
  - PetIdIndex (petId) - Query pet's applications
Enter fullscreen mode Exit fullscreen mode

PawReHome-Favorites

Partition Key: userId (String)
Sort Key: petId (String)
Attributes: addedAt
Enter fullscreen mode Exit fullscreen mode

6. S3 - Image Storage

S3 uses to stores pet photos with public read access for displaying images on the website.

to be continued.....

Made with ❤️ for pets everywhere

Top comments (0)