135 lines
3.5 KiB
TypeScript
135 lines
3.5 KiB
TypeScript
/// <reference path="./.sst/platform/config.d.ts" />
|
|
|
|
import { readFileSync } from 'fs';
|
|
|
|
export default $config({
|
|
app(input) {
|
|
return {
|
|
name: 'shoplist',
|
|
region: input?.region || 'us-east-1',
|
|
};
|
|
},
|
|
async run() {
|
|
// Create VPC for database
|
|
const vpc = new sst.aws.Vpc('ShopListVpc', {
|
|
cidr: '10.0.0.0/16',
|
|
});
|
|
|
|
// Create RDS PostgreSQL database
|
|
const database = new sst.aws.Postgres('ShopListDB', {
|
|
vpc,
|
|
engine: 'postgres16',
|
|
version: '16.1',
|
|
});
|
|
|
|
// Create Redis for caching and WebSocket adapter
|
|
const redis = new sst.aws.Redis('ShopListCache', {
|
|
vpc,
|
|
engine: 'redis7',
|
|
});
|
|
|
|
// Create S3 bucket for file uploads (future)
|
|
const bucket = new sst.aws.Bucket('ShopListUploads', {
|
|
access: 'private',
|
|
});
|
|
|
|
// Create IAM role for Lambda functions
|
|
const role = new aws.iam.Role('ShopListLambdaRole', {
|
|
assumeRolePolicy: aws.iam
|
|
.getPolicyDocument({
|
|
statements: [
|
|
{
|
|
actions: ['sts:AssumeRole'],
|
|
principals: {
|
|
service: 'lambda.amazonaws.com',
|
|
},
|
|
},
|
|
],
|
|
})
|
|
.then((doc) => doc.json),
|
|
});
|
|
|
|
// Attach necessary policies
|
|
new aws.iam.RolePolicyAttachment('ShopListLambdaVPCAccess', {
|
|
role: role.name,
|
|
policyArn: 'arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole',
|
|
});
|
|
|
|
// Create Next.js API server as Lambda + API Gateway
|
|
const api = new sst.aws.NextJs('ShopListAPI', {
|
|
vpc,
|
|
environment: {
|
|
// Database
|
|
DATABASE_URL: $interpolate`postgresql://${database.username}:${database.password}@${database.host}:${database.port}/${database.database}`,
|
|
|
|
// Auth
|
|
BETTER_AUTH_SECRET: $secret('BETTER_AUTH_SECRET'),
|
|
BETTER_AUTH_URL: $interpolate`https://${api.url}`,
|
|
|
|
// OpenRouter (for AI suggestions)
|
|
OPENROUTER_API_KEY: $secret('OPENROUTER_API_KEY'),
|
|
|
|
// Redis (for WebSocket adapter)
|
|
REDIS_URL: $interpolate`redis://${redis.host}:${redis.port}`,
|
|
|
|
// Node environment
|
|
NODE_ENV: 'production',
|
|
},
|
|
warm: 20,
|
|
memory: 1024,
|
|
timeout: 30,
|
|
architecture: 'arm64',
|
|
});
|
|
|
|
// Create custom domain (optional)
|
|
const domain = process.env.SHOPLIST_DOMAIN
|
|
? new sst.aws.Domain('ShopListDomain', {
|
|
domain: process.env.SHOPLIST_DOMAIN,
|
|
targets: [{ type: 'alias', dns: api.url }],
|
|
})
|
|
: null;
|
|
|
|
// Create Cognito user pool for additional auth (optional)
|
|
const cognito = new aws.cognito.UserPool('ShopListUsers', {
|
|
autoVerifiedAttributes: ['email'],
|
|
mfaConfiguration: 'OPTIONAL',
|
|
policies: {
|
|
passwordPolicy: {
|
|
minimumLength: 12,
|
|
requireLowercase: true,
|
|
requireNumbers: true,
|
|
requireSymbols: true,
|
|
requireUppercase: true,
|
|
},
|
|
},
|
|
});
|
|
|
|
// Create CloudWatch log group for API
|
|
const apiLogs = new aws.cloudwatch.LogGroup('ShopListAPILogs', {
|
|
retentionInDays: 30,
|
|
});
|
|
|
|
// Outputs for reference
|
|
return {
|
|
api: {
|
|
url: api.url,
|
|
domain: domain?.domain,
|
|
},
|
|
database: {
|
|
host: database.host,
|
|
port: database.port,
|
|
name: database.database,
|
|
},
|
|
redis: {
|
|
host: redis.host,
|
|
port: redis.port,
|
|
},
|
|
bucket: {
|
|
name: bucket.name,
|
|
},
|
|
vpc: {
|
|
id: vpc.id,
|
|
},
|
|
};
|
|
},
|
|
});
|