A production-ready Next.js application for location-based chat with AWS Cognito authentication and S3 image storage, deployed on AWS ECS.
- AWS Cognito Authentication: Complete auth flow with login, register, email verification, and password reset
- Guest Mode: Allow non-registered users to browse with limited features
- S3 Image Storage: Secure image upload and storage using AWS S3
- Location-based Chat: Connect with people nearby (similar to nearby-chat platforms)
- Real-time Messaging: Chat functionality with nearby users
- Responsive Design: Beautiful, production-ready UI with Tailwind CSS and shadcn/ui
- AWS ECS Deployment: Scalable containerized deployment
- Frontend: Next.js 13+ with TypeScript and Tailwind CSS
- Authentication: AWS Cognito User Pools
- File Storage: AWS S3 for images
- Database: PostgreSQL on AWS RDS
- Container: Docker with multi-stage builds
- Orchestration: AWS ECS with Fargate
- Load Balancer: AWS Application Load Balancer
- CI/CD: GitLab CI/CD pipeline
- Container Registry: AWS ECR
- AWS Account with appropriate permissions
- GitLab account for CI/CD
- Docker installed locally
- AWS CLI configured
- Node.js 18+ installed
Create a Cognito User Pool with the following settings:
- Sign-in options: Email
- Password policy: Minimum 8 characters, require uppercase, lowercase, numbers, and special characters
- MFA: Optional (recommended for production)
- Email verification: Required
- App client: Create an app client without client secret
Create an S3 bucket for image storage:
aws s3 mb s3://co-app-images-bucket --region us-east-1Configure bucket policy for public read access to uploaded images:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::co-app-images-bucket/*"
}
]
}Create IAM roles with appropriate permissions:
- ECS Task Role: S3 access for image uploads
- ECS Execution Role: ECR and CloudWatch access
-
Clone and install dependencies:
npm install
-
Set up environment variables:
cp .env.example .env.local # Edit .env.local with your AWS configuration -
Configure AWS services:
- Set up Cognito User Pool and get the Pool ID and Client ID
- Create S3 bucket and configure permissions
- Update environment variables with your AWS resource IDs
-
Run development server:
npm run dev
# AWS Cognito
NEXT_PUBLIC_COGNITO_USER_POOL_ID=us-east-1_XXXXXXXXX
NEXT_PUBLIC_COGNITO_USER_POOL_CLIENT_ID=XXXXXXXXXXXXXXXXXXXXXXXXXX
NEXT_PUBLIC_AWS_REGION=us-east-1
# AWS S3
NEXT_PUBLIC_S3_BUCKET_NAME=co-app-images-bucket
AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-key
# Database
DATABASE_URL=postgresql://username:password@your-rds-endpoint:5432/co_app_db
# Application
NODE_ENV=production
NEXT_PUBLIC_API_URL=https://your-domain.comSet these variables in your GitLab project settings:
AWS_ACCESS_KEY_ID: AWS access key with ECR and ECS permissionsAWS_SECRET_ACCESS_KEY: AWS secret keyAWS_ACCOUNT_ID: Your AWS account IDDATABASE_URL: PostgreSQL connection string
- Build: Installs dependencies, builds Docker image, pushes to ECR
- Deploy: Forces new deployment on ECS service
- Login/Register: Full Cognito integration with email verification
- Password Reset: Secure password reset with email codes
- Guest Mode: Browse public content without registration
- Session Management: Automatic token refresh and session handling
- S3 Integration: Direct upload to S3 with presigned URLs
- File Validation: Type and size validation
- Preview: Real-time image preview before upload
- Error Handling: Comprehensive error handling and user feedback
- Browse public conversations
- View nearby activity (limited)
- No posting or direct messaging
- Upgrade prompts to encourage registration
- Cognito Security: MFA support, secure password policies
- S3 Security: Proper bucket policies and IAM roles
- Environment Variables: Secure handling of sensitive data
- HTTPS: SSL/TLS encryption for all communications
- Input Validation: Client and server-side validation
Push to main branch triggers automatic build and deployment.
chmod +x scripts/deploy.sh
./scripts/deploy.sh- Health Checks:
/api/healthendpoint for monitoring - CloudWatch Logs: Centralized logging for debugging
- ECS Service Monitoring: Auto-scaling and health monitoring
- Error Tracking: Comprehensive error handling and logging
- ECS Auto Scaling: Configure based on CPU/memory usage
- RDS Scaling: Read replicas for database scaling
- S3 Performance: Optimize for high-throughput image uploads
- Cognito Limits: Monitor authentication rate limits
- Fargate Spot: Use for non-production environments
- S3 Lifecycle: Configure lifecycle policies for old images
- RDS Reserved Instances: For predictable workloads
- CloudWatch Monitoring: Track and optimize resource usage