Skip to content
Writing

· 2 min read

Running Next.js and NestJS on AWS without a platform team

How I host a Next.js frontend and a NestJS API on AWS as the only person on infrastructure: hosting options, NestJS on Lambda, background jobs, secrets, CI/CD and the alarms that matter.

Jose Rai

Senior software engineer

For a long stretch I owned both the apps and the infrastructure of a product: a Next.js frontend, a NestJS API, a Postgres database and a handful of background jobs, all on AWS, with no platform team behind me. The goal was never clever infrastructure. It was boring, cheap and observable, so I could spend my time on the product. This is the setup I'd build again.

Hosting the Next.js frontend

There are three sensible ways to run Next.js on AWS, and the right one depends on how much control you need:

  • Amplify Hosting. Managed builds, previews and SSR with almost no setup. The best default for a small team.
  • OpenNext on Lambda, CloudFront and S3. More control over caching and cost, at the price of owning the deploy tooling.
  • A container on ECS or App Runner. Build with output: "standalone" and run it like any Node server. Predictable, and the easiest to debug.
ts
// next.config.ts: a self-contained server bundle for containers
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  output: "standalone",
};

export default nextConfig;

NestJS on Lambda

For an API with spiky traffic, Lambda behind API Gateway is hard to beat on cost. NestJS runs there through an Express adapter. The key detail is caching the bootstrapped app outside the handler, so only cold starts pay for Nest's startup:

ts
import { NestFactory } from "@nestjs/core";
import serverlessExpress from "@codegenie/serverless-express";
import type { Callback, Context, Handler } from "aws-lambda";
import { AppModule } from "./app.module";

let server: Handler | undefined;

async function bootstrap(): Promise<Handler> {
  const app = await NestFactory.create(AppModule);
  await app.init();
  return serverlessExpress({ app: app.getHttpAdapter().getInstance() });
}

export const handler: Handler = async (event: unknown, context: Context, callback: Callback) => {
  server ??= await bootstrap();
  return server(event, context, callback);
};
  • Keep the bundle small. Cold start time tracks bundle size. Bundle the handler and leave dev tooling out of it.
  • Pool database connections. Hundreds of concurrent Lambdas will exhaust Postgres connections. RDS Proxy sits in between and fixes it.
  • Pay for warm only where it counts. Provisioned concurrency on the few latency-critical functions, not everywhere.

Background work

Anything slow or retryable moves out of the request path: image processing on S3 events, emails and webhooks through SQS, nightly jobs on EventBridge schedules. Every handler is idempotent, because AWS delivers at least once, and every queue has a dead-letter queue with an alarm on it.

Secrets and configuration

No secrets in the repo, and none baked into images. Configuration lives in SSM Parameter Store, secrets in Secrets Manager, one path per environment. Functions read them at cold start with IAM permissions scoped to exactly their own paths.

CI/CD

  • GitHub Actions authenticates to AWS with OIDC and short-lived roles. No long-lived access keys anywhere.
  • Build once, then promote the same artifact from staging to production.
  • Every pull request gets a preview environment, and the preview is torn down when it merges.
  • Database migrations run as their own step before the new code goes live, and are always backward compatible for one release.

The alarms that matter

Structured JSON logs go to CloudWatch, so they can be queried instead of read. On top of that, a short list of alarms that each point at something to do: API error rate, p95 latency, dead-letter queue depth, database CPU and connections, and an AWS Budgets alert so a runaway function shows up as an email, not an invoice.

None of this needs a platform team. It needs a few deliberate defaults, written down once and applied to every service, so that running the system takes an hour a week instead of a person.