Skip to main content

Function

A Function is a serverless function — invoked per event (a queue message, a scheduled time, an HTTP request, or a direct invoke), scales to zero between invocations, and has cold-start semantics.

Quick example

import { Project, Function, Queue, Database } from '@sprintsail/sdk';

export const app = new Project('orders');
export const queue = new Queue(app, 'orders', {});
export const db = new Database(app, 'orders-db', { engine: 'postgres', version: '16' });

export const processOrder = new Function(app, 'process-order', {
handler: './src/handlers/process-order.ts',
runtime: 'nodejs22',
memory: 512,
timeout: 30,
triggers: [queue],
bindings: { db },
});

The handler:

import { db } from '../../infra.js';

export default async function handler(event: { Records: Array<{ body: string }> }) {
const order = JSON.parse(event.Records[0].body);
await db.query('INSERT INTO orders (id, amount) VALUES ($1, $2)', [order.id, order.amount]);
}

Config

interface FunctionConfig {
handler: string; // relative path to entry .ts/.js
runtime?: 'nodejs22' | 'nodejs20' | 'python3.12'; // default nodejs22
memory?: number; // MB, default 512
timeout?: number; // seconds, default 30
env?: Record<string, string>;
triggers?: Queue[]; // async event sources
bindings?: Record<string, Database | Bucket | Secret | Queue>;
}

Target mapping

TargetMaps toNotes
awsLambdaarm64. Trigger queues become event-source mappings.
sprintsail-runtimeKnative ServiceScale-to-zero. Request body is the event; handler return is the response.

Queue triggers on Sprintsail Runtime

A Function with queue triggers on the runtime is not wired into Knative Eventing in v1.0 — that's the Worker primitive's job. The runtime's Function provider deploys the Function as a request-driven Knative Service only and logs a clear note when triggers are declared. Use a Worker for queue consumption.

Runtime behavior

The handler is invoked with whatever event shape your trigger sends:

  • Queue trigger (AWS): { Records: [{ messageId, body }] } — same shape as Lambda's SQS event source mapping.
  • HTTP (only when paired with API): API Gateway v2 HTTP event.
  • Direct invoke: the raw event object.

event is typed unknown in the SDK — declare your own type in the handler.

The return value is JSON-serialised. For Knative on the runtime, it becomes the HTTP response body.

Bindings

Any resource primitive can be bound. The binding name (db above) becomes the import name in the handler. See Bindings.

Limits

  • AWS: 15-minute timeout cap; 10 GB memory cap; arm64 only.
  • Runtime: max timeout enforced as Knative timeoutSeconds. No memory cap from the SDK side.