A warm Lambda process is a resource-reuse boundary, not a conversation boundary. When adapting a CometAPI chat handler to Lambda, my recommendation is to reuse request-independent transport resources where appropriate—but never make the last conversation or selected model a process-wide default.
AWS’s Function code guidance places two instructions next to each other: initialize SDK clients and database connections outside the handler to benefit from reuse, and do not use the execution environment to store user data, events, or other security-sensitive information across invocations. The second instruction constrains the first. It is not a reason to recreate every connection on every request.
Apply that boundary to the CometAPI request
The official CometAPI Next.js chat guide gives a concrete request contract to preserve when adapting its route to Lambda:
- The browser sends
modelandmessagesto the application’s/api/chatroute. - The server checks the model against its allowlist and validates the messages.
- The handler forwards those fields with
stream: truetohttps://api.cometapi.com/v1/chat/completionsand passesrequest.signalto the upstream fetch.
That example uses built-in fetch, not a provider SDK. AWS’s SDK-client advice therefore does not mean that the tutorial needs a new global SDK object. If an adaptation introduces one, inspect whether it retains mutable request options before sharing it.
The important distinction is between a shared routing policy and a user’s selected route. A server-side allowlist can be request-independent configuration; the current model value cannot be the previous invocation’s selection. Likewise, a reusable connection is not a place to keep a conversation.
For this integration, I would use the following ownership rule:
| Resource or value | Recommended owner |
|---|---|
| Fixed CometAPI base URL and server-side model allowlist | Request-independent configuration |
| Transport pool or SDK client, if introduced | Reusable only after confirming it does not retain user-specific request state |
Validated model and messages |
Current invocation; construct a fresh outbound payload |
| Current user’s authorization result or request-specific headers | Current invocation; never mutate shared defaults with them |
| Cancellation signal/controller, response stream and accumulated output | Current invocation; do not retain them for the next call |
In particular, avoid a module-level lastModel, shared messages array, or reusable abort controller. Missing or unsupported model input should fail validation, not inherit a value from the previous caller. Keep user authentication separate from the deployment’s CometAPI credential: the guide explicitly recommends authenticating application users, not merely protecting the upstream key.
Test reuse without using real conversations
Before accepting the Lambda adaptation, I would run a same-process regression test with a mocked upstream transport:
- Invoke the handler with synthetic conversation A and approved model A.
- Invoke it again without reloading the module, using distinct synthetic conversation B and approved model B.
- Inspect the second outbound CometAPI request. Its
messagesmust contain only B’s submitted conversation, and itsmodelmust be B’s selected ID. It must not carry A’s signal, response buffer or request-specific authorization state. - Repeat after an invalid payload, an upstream error and a cancelled first request. An invalid second model must produce no upstream request—not reuse model A.
These are proposed acceptance checks, not results from a tested deployment. Use synthetic markers in assertions rather than logging real prompts; the CometAPI guide also calls for redacting keys, full prompts and sensitive model output from production error logs.
This boundary does not prove that a particular Lambda deployment streams correctly, that cancellation stops upstream billing, or that the application has complete tenant isolation. Those require separate checks. Nor do the sources establish an actual leak. They support a narrower decision: preserve CometAPI’s per-request conversation and model selection while applying AWS’s resource-reuse guidance—not process-wide chat state.