Database Architecture
The RDS instance is deployed in a private subnet with no public internet access. Only the backend ECS tasks can connect to it through security group rules.Security Model
- RDS resides in private subnets across multiple availability zones
- No public IP address assigned
- Access restricted to backend security group only
- Credentials stored in AWS Systems Manager Parameter Store
- Encrypted at rest and in transit
Create DB Subnet Group
RDS requires a DB subnet group that spans at least two availability zones.Create RDS Security Group
Create a security group that allows PostgreSQL connections only from the backend:We’ll add the ingress rule after creating the backend security group, since they need to reference each other.
Create RDS Instance
aws rds create-db-instance \
--db-instance-identifier adma-postgres \
--db-instance-class db.t3.micro \
--engine postgres \
--engine-version "16.3" \
--master-username appuser \
--master-user-password "$DB_PASSWORD" \
--db-name urlshortener \
--db-subnet-group-name adma-rds-subnet-group \
--vpc-security-group-ids $RDS_SG \
--no-publicly-accessible \
--storage-type gp3 \
--allocated-storage 20 \
--backup-retention-period 7 \
--deletion-protection \
--region $AWS_REGION
db-instance-identifieradma-postgresdb-instance-classdb.t3.microengine-version16.3master-usernameappuserdb-nameurlshortenerno-publicly-accessiblestorage-typegp3allocated-storage20backup-retention-period7deletion-protectionFor production workloads, consider:
db.t3.smallor larger instance class- Multi-AZ deployment with
--multi-azflag - Higher storage allocation
- Longer backup retention period
aws rds describe-db-instances \
--db-instance-identifier adma-postgres \
--query 'DBInstances[0].DBInstanceStatus' \
--output text \
--region $AWS_REGION
Store Database Credentials in SSM
Never hardcode database passwords in your application code or task definitions. Use AWS Systems Manager Parameter Store:SecureString parameters are encrypted using AWS KMS. They can only be decrypted by services with the appropriate IAM permissions.Initialize the Database Schema
The Spring Boot backend uses Hibernate to automatically create the database schema. However, ShedLock (used for distributed job locking) requires manual table creation.Create ShedLock Table
Connect to your RDS instance and create the ShedLock table:ShedLock ensures that scheduled tasks (like the expired URL cleanup job) run on only one instance when your backend scales horizontally.
Connection Methods
Security Group Configuration
Configure the RDS security group to accept connections only from the backend:Monitoring and Maintenance
Enable Enhanced Monitoring
For detailed performance metrics, enable enhanced monitoring:Automated Backups
RDS automatically creates daily backups during the backup window. To modify the backup window:Performance Insights
Enable Performance Insights for query-level monitoring:Database Configuration Reference
Environment Variables for Backend
The backend application requires these environment variables (configured in the ECS task definition):| Variable | Value | Source |
|---|---|---|
DB_HOST | adma-postgres.xxxxx.region.rds.amazonaws.com | RDS endpoint |
DB_PORT | 5432 | Default PostgreSQL port |
DB_NAME | urlshortener | Database name |
DB_USERNAME | appuser | Master username |
DB_PASSWORD | (secret) | SSM Parameter Store |
Connection Pool Settings
The Spring Boot backend uses HikariCP with these default settings:application.yml
Troubleshooting
Cannot Connect to RDS
Problem: Connection timeout when connecting to RDS Solutions:- Verify the instance is in
availablestatus - Check security group rules allow inbound traffic on port 5432
- Ensure you’re connecting from a resource within the VPC
- Verify the DB subnet group uses private subnets
Authentication Failed
Problem:password authentication failed for user "appuser"
Solutions:
- Verify the password stored in SSM Parameter Store
- Check for special characters that might need escaping
- Reset the master password if necessary:
Database Disk Full
Problem: Storage is full Solution: Increase allocated storage:RDS supports storage autoscaling. Enable it to automatically increase storage when needed.
Cost Optimization
Free Tier Eligibility
Thedb.t3.micro instance with 20 GB of storage is eligible for the AWS free tier:
- 750 hours per month of db.t3.micro usage
- 20 GB of General Purpose (SSD) storage
- 20 GB of backup storage
Production Considerations
For production workloads:- Use Multi-AZ deployment for high availability (doubles the cost)
- Consider Reserved Instances for 1-3 year commitments (up to 69% savings)
- Enable storage autoscaling to avoid manual intervention
- Use Amazon RDS Proxy for connection pooling at scale
Next Steps
With your RDS database configured:- Set up ECS Fargate - Create task definitions and services
- Configure Load Balancer - Set up application routing
- Enable HTTPS/SSL - Secure your application