Skip to content

Commit 9ba32a3

Browse files
author
claudfuen
committed
feat: add AWS SSL profiles and migration log retrieval script
- Introduced `aws-ssl-profiles` dependency for improved SSL verification with AWS RDS. - Added a new script `get-migration-logs.sh` to fetch recent migration logs from AWS CloudWatch. - Updated Dockerfile to enhance logging and error reporting during migration execution. - Refactored database connection to utilize AWS CA bundle for SSL in production environments.
1 parent 759f877 commit 9ba32a3

File tree

6 files changed

+74
-14
lines changed

6 files changed

+74
-14
lines changed

apps/infra/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ const db = new aws.rds.Instance("pathfinder-db", {
147147
password: dbPassword.result, // Use the generated secure password
148148
vpcSecurityGroupIds: [dbSecurityGroup.id],
149149
dbSubnetGroupName: dbSubnetGroup.name,
150-
150+
151+
// Use default parameter group (requires SSL - AWS best practice)
152+
applyImmediately: true, // Apply parameter changes immediately (requires restart)
153+
151154
skipFinalSnapshot: true, // For dev - set to false in production
152155
deletionProtection: false, // For dev - set to true in production
153156
backupRetentionPeriod: 7, // Keep backups for 7 days
@@ -461,10 +464,10 @@ const service = new awsx.ecs.FargateService("pathfinder-service", {
461464
name: "PORT",
462465
value: "3000",
463466
},
464-
{
465-
name: "DATABASE_URL",
466-
value: pulumi.interpolate`postgresql://${db.username}:${db.password}@${db.endpoint}/${db.dbName}`,
467-
},
467+
{
468+
name: "DATABASE_URL",
469+
value: pulumi.interpolate`postgresql://${db.username}:${db.password}@${db.endpoint}/${db.dbName}`,
470+
},
468471
{
469472
name: "ENABLE_DEBUG_ENDPOINTS",
470473
value: "true", // Temporary: for debugging environment variables

apps/web/Dockerfile.migration

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@ FROM oven/bun:1.2.18-alpine
33

44
WORKDIR /app
55

6-
# Copy only what's needed for migrations
6+
# Copy package files first for better caching
77
COPY package.json bun.lock* ./
8-
COPY src/db ./src/db
9-
COPY src/env.ts ./src/env.ts
10-
COPY scripts ./scripts
8+
9+
# Install all dependencies (including dev deps like tsx if needed)
10+
RUN bun install --frozen-lockfile
11+
12+
# Copy the entire src directory to avoid import path issues
13+
COPY src/ ./src/
14+
COPY scripts/ ./scripts/
1115
COPY drizzle.config.ts ./
1216

13-
# Install only production dependencies
14-
RUN bun install --frozen-lockfile --production
17+
# Verify migration files exist and show structure
18+
RUN echo "=== Directory structure ===" && \
19+
find . -type f -name "*.ts" -o -name "*.sql" | head -20 && \
20+
echo "=== Migration files ===" && \
21+
ls -la src/db/migrations/ && \
22+
echo "=== Environment check ===" && \
23+
printenv | grep -E "(DATABASE_URL|NODE_ENV)" || echo "No DB env vars yet"
24+
25+
# Set environment for better error reporting
26+
ENV NODE_ENV=production
1527

16-
# Default command runs migrations
17-
CMD ["bun", "run", "scripts/run-migrations.ts"]
28+
# Default command runs migrations with verbose output
29+
CMD ["sh", "-c", "echo 'Starting migration container...' && bun run scripts/run-migrations.ts"]

apps/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"dependencies": {
2222
"@t3-oss/env-nextjs": "^0.13.8",
23+
"aws-ssl-profiles": "^1.1.2",
2324
"axios": "^1.10.0",
2425
"dotenv": "^17.1.0",
2526
"drizzle-orm": "^0.44.2",

apps/web/src/db/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import awsCaBundle from "aws-ssl-profiles";
12
import { drizzle } from "drizzle-orm/node-postgres";
23
import { Pool } from "pg";
34
import { env } from "../env";
@@ -6,7 +7,8 @@ import * as schema from "./schema";
67
// Create a connection pool using validated environment variables
78
const pool = new Pool({
89
connectionString: env.DATABASE_URL,
9-
// ssl: env.NODE_ENV === "production" ? { rejectUnauthorized: false } : false,
10+
// Use AWS RDS CA bundle for proper SSL verification
11+
ssl: env.DATABASE_URL.includes("localhost") ? false : awsCaBundle,
1012
});
1113

1214
// Create the database instance

bun.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

get-migration-logs.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
LOG_GROUP="pathfinder-logs-a5c296d"
4+
REGION="us-east-1"
5+
6+
echo "Getting recent log streams for migration..."
7+
aws logs describe-log-streams \
8+
--log-group-name "$LOG_GROUP" \
9+
--order-by LastEventTime \
10+
--descending \
11+
--region "$REGION" \
12+
--max-items 10 \
13+
--output table \
14+
--query 'logStreams[].{StreamName:logStreamName,LastEvent:lastEventTime}'
15+
16+
echo ""
17+
echo "Getting recent migration task logs..."
18+
MIGRATION_STREAM=$(aws logs describe-log-streams \
19+
--log-group-name "$LOG_GROUP" \
20+
--order-by LastEventTime \
21+
--descending \
22+
--region "$REGION" \
23+
--max-items 10 \
24+
--query 'logStreams[?contains(logStreamName, `migration-task`)].logStreamName' \
25+
--output text | head -1)
26+
27+
if [ -n "$MIGRATION_STREAM" ]; then
28+
echo "Found migration stream: $MIGRATION_STREAM"
29+
echo "Migration logs:"
30+
echo "=================================="
31+
aws logs get-log-events \
32+
--log-group-name "$LOG_GROUP" \
33+
--log-stream-name "$MIGRATION_STREAM" \
34+
--region "$REGION" \
35+
--query 'events[].message' \
36+
--output text
37+
else
38+
echo "No migration log stream found"
39+
fi

0 commit comments

Comments
 (0)