Skip to content

iamtripathi25/hospital-backend

Repository files navigation

Hospital Mini-Backend

A secure, serverless hospital backend system built with AWS SAM, allowing patients to access their medical records and administrators to view aggregate health metrics.

Architecture Diagram

alt text

🚀 Features

  • Patient Self-Service: Patients can securely retrivew their own medical records
  • Admin Dashboard: Web-based interface for viewing aggregate health metrics
  • Secure Authentication: AWS Cognito-based JWT authentication
  • Role-Based Access: Separate permissions for patients and administrators
  • Serverless Architecture: Scalable, pay-per-use infrastructure
  • Infrastructure as Code: Complete SAM template for reproducible deployments

📋 Prerequisites

  • AWS IAM account with administrative access
  • AWS CLI v2.x configured locally with credentials
  • AWS SAM CLI v1.100+
  • Python 3.11+
  • Git

🛠️ Setup & Installation

1. Clone the Repository

git clone https://github.com/iamtripathi25/hospital-backend.git
cd hospital-backend

2. Set Up Python Environment

# Create virtual environment
python -m venv .venv

# Activate virtual environment
.venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

3. Build the Application

sam build

4. Run Test

pytest

5. Deploy the Application

# First deployment (guided)
sam deploy --guided

# Follow the prompts:
# - Stack Name: hospital-backend
# - AWS Region: us-east-1 (or your preferred region)
# - Parameter Environment: dev
# - Confirm changes before deploy: Y
# - Allow SAM CLI IAM role creation: Y
# - Save arguments to configuration file: Y

# Subsequent deployments
sam deploy

# Recommended
sam deploy --no-confirm-changeset --no-fail-on-empty-changeset --stack-name $STACK_NAME

6. Get Exports from Cloudformation

aws cloudformation describe-stacks --stack-name $STACK_NAME --output json > exports.json

7. Generate config.js

python3 .\scripts\generate_config.py <stack-name>

8. Deploy Dashboard to S3-bucket

aws s3 sync ./dashboard/ s3://$DASHBOARD_BUCKET --delete

# Get s3_bucket_name from exports.json

🌱 Seeding Test Data

1. Create Patient Users

# Dry run first (recommended)
python3 -m scripts.seed_cognito_users \
  --user-pool-id $USER_POOL_ID \
  --count 100 \
  --output cognito_users.csv 
  --dry-run

python3 -m scripts.seed_cognito_users \
  --user-pool-id $USER_POOL_ID \
  --count 100 \
  --output cognito_users.csv

# Get User_POOL_ID from exports.json

2. Populate Patient Data in DynamoDB

python3 -m scripts.seed_dynamodb_records \
  --input cognito_users.csv \
  --table-name $PATIENT_RECORD_TABLE \

# Dry run first (recommended)
python3 -m scripts.seed_dynamodb_records \
  --input cognito_users.csv \
  --table-name $PATIENT_RECORD_TABLE \
  --dry-run 

# Get PATIENT_RECORD_TABLE from export.json

3. Create Admin User

python3 -m scripts.make_admin_user \
  --user-pool-id $USER_POOL_ID \
  --name <admin_name> \
  --email <admin_email_id> \
  --password <admin_password>

Testing Endpoints using browser + Postman

# Call this url in browser or postman to check health of backend
$API_ENDPOINT/health 

# Call this in browser to get id_token after successful login. In case of Admin application redirects to admin dashboard
$COGNITO_HOSTED_URL

# Call this url in postman with Authorization as header with value id_token from above 
$API_ENDPOINT/patient/me # 

# To get json data call following url in postman with above id_token 
$API_ENDPOINT/admin/metrics/overview
$API_ENDPOINT/admin/metrics/medications
$API_ENDPOINT/admin/metrics/diseases

# Get API_ENDPOINT, COGNITO_HOSTED_URL from exports.json

🧹 Cleanup / Teardown

# Delete all S3 objects first
aws s3 rm s3://$DASHBOARD_BUCKET --recursive

# Delete the CloudFormation stack
sam delete

# Or use AWS CLI
aws cloudformation delete-stack --stack-name $STACK_NAME

# Verify deletion
aws cloudformation describe-stacks --stack-name $STACK_NAME

About

Serverless backend using AWS

Topics

Resources

License

Stars

Watchers

Forks

Contributors