This project is a serverless AI application with a React frontend, utilizing AWS Lambda and PostgreSQL for backend operations.
The application consists of two main components: a serverless backend API built with Node.js and Express, and a React-based frontend created using Vite. The backend is designed to be deployed on AWS Lambda and connects to a PostgreSQL database. The frontend is a modern, responsive web application that interacts with the backend API.
Key features of this project include:
- Serverless architecture using AWS Lambda
- Express.js backend with PostgreSQL database integration
- React frontend with TypeScript support
- Vite for fast and efficient frontend development
- Environment-based configuration for local development and production deployment
The repository is organized into two main directories:
-
dev/serverless-ai/
: Contains the backend API codeapp.js
: Main entry point for the Express.js applicationpackage.json
: Node.js project configuration and dependenciestf/
: Terraform configuration files for infrastructure
-
dev/serverless-al-frontend/
: Contains the frontend React applicationsrc/
: Source code for the React applicationvite.config.ts
: Vite configuration filepackage.json
: Frontend project configuration and dependencies
Key files:
dev/serverless-ai/app.js
: Entry point for the backend APIdev/serverless-ai/tf/main.tf
: Main Terraform configuration filedev/serverless-al-frontend/src/main.tsx
: Entry point for the React applicationdev/serverless-al-frontend/src/App.tsx
: Main React component
-
Navigate to the
dev/serverless-ai
directory:cd dev/serverless-ai
-
Install dependencies:
npm install
-
Set up environment variables: Create a
.env
file in thedev/serverless-ai
directory with the following content:DB_USER=your_db_user DB_PASSWORD=your_db_password DB_HOST=your_db_host DB_PORT=your_db_port DB_NAME=your_db_name
-
Start the development server:
npm start
-
Navigate to the
dev/serverless-al-frontend
directory:cd dev/serverless-al-frontend
-
Install dependencies:
npm install
-
Set up environment variables: Create a
.env
file in thedev/serverless-al-frontend
directory with the following content:VITE_API_URL=http://localhost:3000
-
Start the development server:
npm run dev
The backend API provides two endpoints:
-
GET
/api
: Returns a simple JSON messagecurl http://localhost:3000/api
-
GET
/api/hello
: Returns a JSON object with a message and the current timestamp from the databasecurl http://localhost:3000/api/hello
The frontend application is a simple React app with a counter. You can interact with it by clicking the button to increment the counter.
Currently, there are no specified tests for either the backend or frontend. It is recommended to add unit tests and integration tests to ensure the reliability of the application.
-
Database Connection Issues:
- Problem: Unable to connect to the PostgreSQL database
- Error message: "Database query failed"
- Diagnostic steps:
a. Check if the database is running and accessible
b. Verify the environment variables in the
.env
file c. Ensure the database user has the necessary permissions - Debug mode: Add
console.log
statements in the/api/hello
route to log connection details
-
Frontend API Connection Issues:
- Problem: Frontend unable to connect to the backend API
- Error message: Network error in the browser console
- Diagnostic steps:
a. Check if the backend server is running
b. Verify the
VITE_API_URL
in the frontend.env
file c. Ensure CORS is properly configured in the backend - Debug mode: Use browser developer tools to inspect network requests
-
Backend:
- Monitor database query performance using PostgreSQL's EXPLAIN ANALYZE
- Implement connection pooling for database connections
- Use caching mechanisms for frequently accessed data
-
Frontend:
- Implement code splitting and lazy loading for large components
- Optimize asset sizes using compression and minification
- Use React.memo or useMemo for expensive computations
The application follows a typical client-server architecture with a React frontend communicating with a Node.js backend API, which in turn interacts with a PostgreSQL database.
- User interacts with the React frontend
- Frontend makes HTTP requests to the backend API
- Backend API receives requests and processes them
- If needed, the backend queries the PostgreSQL database
- Database returns results to the backend
- Backend processes the data and sends a response to the frontend
- Frontend updates the UI based on the received data
[User] <-> [React Frontend] <-> [Express.js Backend API] <-> [PostgreSQL Database]
Important technical considerations:
- The backend uses a connection pool for efficient database interactions
- CORS is enabled on the backend to allow requests from the frontend
- The frontend uses environment variables to configure the API URL
The infrastructure for this project is defined using Terraform. Key resources include:
-
Lambda:
aws_lambda_function.api
: Hosts the Express.js backend application
-
API Gateway:
aws_apigatewayv2_api.lambda
: Provides HTTP API for the Lambda function
-
VPC:
aws_vpc.main
: Virtual Private Cloud for isolating resources
-
RDS:
aws_db_instance.default
: PostgreSQL database instance
-
Security Groups:
aws_security_group.rds
: Controls access to the RDS instance
These resources work together to create a serverless environment for the application, with the API Gateway routing requests to the Lambda function, which then interacts with the RDS database securely within the VPC.