# Reduce Cold Starts for a NestJS API on Serverless Framework

Yes. An agent measures your NestJS Lambda's bundle and bootstrap, trims the bundle, moves work off the first request, adds provisioned concurrency and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/reduce-cold-starts/serverless/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent clones the service and measures before it changes anything. In a service like `aws-node-typescript-nest` in serverless/examples, `serverless.yml` is a Framework 4 service on `nodejs24.x` and `arm64` with one function, `main`, behind an `http` proxy route, and a `build.esbuild` block pointing at `esbuild.config.js`, which only adds a `createRequire` banner so CommonJS dependencies can call `require` inside the ESM bundle, and lists `@nestjs/microservices`, `@nestjs/websockets`, `class-validator` and `class-transformer` as external. `src/main.ts` creates an Express app, boots `AppModule` through `NestFactory.create` with the `ExpressAdapter`, wraps it with `@codegenie/serverless-express` and caches the handler in a module variable, so every cold start pays the full Nest bootstrap on the first request. `package.json` carries `@nestjs/core` and `@nestjs/common` at 11.1 and a `bench.js` that times a thousand GET requests against `sls offline`, which exercises warm invocations only. There is no `memorySize`, no `provisionedConcurrency` and no `minify` setting.

It then makes the cold path shorter in the order that matters. It runs `sls package` and records the size of the zip and the bundled handler, then tightens `esbuild.config.js` with `minify`, `treeShaking` and `target` set to the Node version the runtime ships, and `keepNames: true`, because Nest derives module and provider tokens partly from class names and the Nest CLI's own build keeps class names for that reason; the agent runs the controller test and the e2e test against the minified bundle to check injection still resolves. It moves the bootstrap out of the handler body and starts it at module load, so the Nest initialisation overlaps the runtime's own startup instead of waiting for the first event, and keeps the cached promise so warm invocations reuse it. It sets `memorySize` on the function, since CPU scales with memory and the bootstrap is CPU-bound, and adds `provisionedConcurrency` on `main` as a commented option with the number of warm instances the team should choose from its traffic, because the Framework creates the version and alias that provisioned concurrency needs.

Each change is measured with the same harness. The agent writes a small script that imports the bundled handler and times the module load plus the first invocation with a fake API Gateway event, runs it several times in a fresh Node process each, and reports the before-and-after numbers in the pull request. It keeps `bench.js` for the warm path and notes that `sls offline` cannot show a cold start. The repository's own `aws-node-container-snapstart` example, which turns on `snapStart` for a container-image function, is cited as the route to take if initialisation still dominates after bundling, with the trade-offs it lists.

It runs the unit test and the e2e test with `jest` and `jest --config test/jest-e2e.json`, `sls package` again to confirm the smaller artifact, and `sls print` to show the resolved function configuration with the new memory and the provisioned concurrency option, and it does not deploy.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Reduce cold starts for the NestJS function in this Serverless Framework service. Read serverless.yml, the esbuild config, the handler, package.json and the tests first, and record the packaged bundle size and the time to load the module and serve a first request with a fake event. Then tighten the esbuild settings without breaking Nest's dependency injection, start the Nest bootstrap at module load instead of on the first request while keeping the cached handler, set a memory size, and add provisioned concurrency as a documented option on the function. Measure again with the same script, run the unit and e2e tests and sls package, and open a pull request with the before and after numbers, what each change contributed, and the command output. Do not deploy.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that changes `serverless.yml`, `esbuild.config.js` and `src/main.ts`, adds the cold-start timing script under `scripts/`, and leaves the controller and service alone. The description has a table of bundle size and first-request time before and after each step, the memory size chosen and why, and the provisioned concurrency line with the question the team has to answer to set its value.

The run's record keeps every `sls package` output, the timing runs, the unit and e2e suites run against the minified bundle, so a reviewer can see each number came from a command rather than a claim.

Before merging, a reviewer deploys to a staging stage, invokes the function after an idle period to see a real cold start in the CloudWatch init duration, and decides the provisioned concurrency count from the traffic graph and the account's concurrency limit.

## Run it on every Serverless service

A backend engineer hands this off from the terminal: one `POST /o/{org}/sessions` with this body and the service repository in `resources`. The key's `webhook_url` reports the finished run with the pull request in `result`, and a retried request carries the same `request_id`, a UUID, so it returns the run already started.

A team with many services sends one request per repository, and each run measures that service's own bundle and bootstrap, so a plain Express handler and a Nest application get different changes and different numbers.

## Why run it on Rush?

Cold start work is measurement first and settings second, and the measurement needs a packaged bundle, a fresh Node process and the same script before and after every change. The agent does all of that in its own sandbox, checks that a minified bundle still resolves Nest's injection before it reaches a deploy, and ties each setting to the number it moved.

On Rush the timing runs and the package output are in the run's record beside the pull request, and a follow-up to the same run can try one more setting, such as a different memory size, without re-establishing the baseline. The same request serves every Serverless Framework service a team runs.

## Frequently asked questions

### Why keep class names when minifying?

Nest builds module and provider tokens partly from class names, and the Nest CLI's own build keeps class names when it minifies for that reason. esbuild's `keepNames` does the same while still minifying everything else, and the agent runs the existing controller and e2e tests against the minified bundle to confirm injection resolves.

### Does moving the bootstrap to module load change behaviour?

The same bootstrap runs, just earlier. It now starts while the runtime initialises instead of when the first event arrives, and a failure during bootstrap surfaces as an initialisation error rather than a timed-out first request, which is easier to see in the logs.

### Should I turn on provisioned concurrency?

Only if cold starts still matter after bundling and the traffic justifies warm instances sitting idle. The pull request adds the setting as a documented option with the question to answer, and a reviewer sets the count from the traffic graph rather than the agent guessing.

### Can the run measure a real cold start?

It measures module load and first invocation in a fresh Node process, which is the part the code controls. The runtime's own startup and the deployment package download happen on AWS, so the pull request asks the reviewer to confirm the init duration in CloudWatch after a staging deploy.

## More Serverless Framework jobs

- [Upgrade the Lambda Runtime in a Serverless Service](https://getrush.ai/run/upgrade-lambda-runtime/serverless/)
- [Write Tests for AWS Lambda Functions in a Serverless Service](https://getrush.ai/run/write-lambda-tests/serverless/)
- [Add API Key Auth to a Serverless Framework API](https://getrush.ai/run/add-api-key-auth/serverless/)

## The same job on other stacks

- [Reduce Cold Starts in Cloud Functions for Firebase](https://getrush.ai/run/reduce-cold-starts/firebase-functions/)

## More jobs like this: Hand off a long task

- [Reduce a Docker Image's Size for a Flask App](https://getrush.ai/run/reduce-image-size/docker/)
- [Make Turborepo CI Check Only the Packages a Pull Request Changed](https://getrush.ai/run/run-ci-on-changed-packages/turborepo/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
