Skip to Content
Workloads

Function

Run a Function, call it, read its status and its logs, call one Function from another, serve HTTP, and scale to zero.

Usage

A Function is code that funcd runs when it is called: its handler answers each call. This example runs log-burst, which takes events and returns emitted, the number of its log calls; Serve HTTP requests shows a Function that takes HTTP requests.

Install the dev build of funcdctl, Git, curl, and Node.js, or Python 3.12 or later as python3. funcdctl dev runs each Function from the code in the folder, not from the manifest's image, the artifact that funcdctl push packs for a platform. --gport sets the port of the Function URLs, and --cport that of the control plane, which funcdctl reaches with --server and the token funcd-dev-token.

The manifest, examples/log-burst/burst.yaml:

# log-burst: a Function whose handler writes more than 100 log lines at each call.
# funcd keeps each console call as a log record, which funcdctl logs prints.
apiVersion: funcd.io/v1alpha1
kind: Function
metadata:
  name: log-burst
  namespace: default
  resourceGroup: rg1
spec:
  runtime: nodejs22
  handler: handle
  image: oci-layout:///mnt/funcd-deps/registry:log-burst   # funcdctl push log-burst.mjs (built from src/main.ts)
  replicas: 1
  scaling:
    minReplicas: 1

funcdctl dev runs the handler's build, log-burst.mjs, already in the repository:

$ git clone https://github.com/pyvvo/funcd-typescript
$ cd funcd-typescript
$ funcdctl dev examples/log-burst --gport 8080 --cport 8081

In another terminal, call the Function:

$ curl -s -X POST -H 'content-type: application/json' -d '{}' http://127.0.0.1:8080/function/log-burst
{"emitted":115}

The funcdctl dev terminal prints each log line. Read the Function's status:

$ funcdctl --server http://127.0.0.1:8081 --token funcd-dev-token get function log-burst -n default -o json

Its phase is Ready. Keep the example running for Read a Function's logs.

Read a Function's logs

funcdctl logs prints a Function's log records, the latest 1000 by default: each console, logging or ctx.log call, and each line of standard output as INFO or of standard error as ERROR. A record appears within funclog.segmentMaxAge, 10 seconds by default, or about a second under funcdctl dev, so repeat the command:

$ funcdctl --server http://127.0.0.1:8081 --token funcd-dev-token logs log-burst -n default --severity error
2026-11-13T15:04:11.432Z [ERROR] 0 burst stderr default
2026-11-13T15:04:11.433Z [ERROR] 0 item failed
2026-11-13T15:04:11.433Z [ERROR] 0 item failed
2026-11-13T15:04:11.433Z [ERROR] 0 item failed
2026-11-13T15:04:11.433Z [ERROR] 0 item failed

--since 15m keeps a recent period, and -o wide adds attributes. Press Ctrl-C in the first terminal to stop.

Write a handler and its contract

A Function's code exports the handler that the manifest's handler names, run by its runtime. A worker calls it with the context, through which it logs and reaches other Functions and its stores, and the event: the call's JSON body arrives as event.data (event["data"] in Python), unless the body is itself an event, as Call another Function explains. The return value is the response.

The contract file names the runtime and holds the JSON Schema of the input under input and of the result under output. funcd refuses a call whose data does not match with 422, before the handler runs, and answers 500 when the result does not match. log-burst's contract (TypeScript, Python) makes every field optional, so the first call sends {}; a call with no body gets 422.

A contract uses only the JSON Schema that every runtime supports: objects with properties and additionalProperties: false, maps, {} for any value, and oneOf with a discriminator. funcdctl push refuses $ref, anyOf, allOf, not and if, and names what to use instead.

The first example's handler:

// log-burst: each call logs at least 100 items with console.log, a warning every 10th item and an
// error every 25th, then returns how many console calls it made.
//
// Authored in TypeScript against @funcd-dev/shim; `yarn build` (vite.config.ts) bundles it to
// log-burst.mjs.
import type { CloudEvent, FunctionContext } from '@funcd-dev/shim';

export interface FuncInput {
  /** How many items to log; fewer than 100 still logs 100. */
  items?: number;
  /** A batch label echoed into the structured logs. */
  batch?: string;
  /** When set, one more log call carries a value of this many bytes, to show a record cut at the bound. */
  big?: number;
}
export interface FuncOutput {
  emitted: number;
}

export function handle(_ctx: FunctionContext, event: CloudEvent<FuncInput>): FuncOutput {
  const items = Math.max(100, event.data?.items ?? 100); // at least 100 items
  const batch = event.data?.batch ?? 'default';
  let emitted = 0;

  // a line on standard output becomes an INFO record, one on standard error an ERROR record.
  process.stdout.write(`burst stdout ${batch}\n`);
  process.stderr.write(`burst stderr ${batch}\n`);
  if (event.data?.big) {
    console.log('big value', { big: 'x'.repeat(event.data.big) });
    emitted++;
  }

  for (let i = 0; i < items; i++) {
    // INFO: the object argument becomes the record's attributes, which funcdctl logs -o wide shows.
    console.log('processing item', { i, batch });
    emitted++;

    // a WARN record every 10th item and an ERROR record every 25th.
    if (i % 10 === 0) {
      console.warn('slow item', { i, batch, latencyMs: 120 });
      emitted++;
    }
    if (i % 25 === 0) {
      console.error('item failed', { i, batch, reason: 'simulated' });
      emitted++;
    }
  }

  // a final summary record, at INFO.
  console.log('burst complete', { batch, emitted });
  emitted++;

  return { emitted };
}

To bundle a handler of your own, @funcd-dev/vite-plugin writes dist/<name>.mjs, dependencies inlined, and copies <name>.contract.yaml beside it, where funcdctl dev finds them.

Call another Function

A Function calls another of its namespace over a link declared under spec.bindings.links: an alias that its code uses and a target. ctx.invoke takes the alias and an input, and returns the target's result.

  • An input with a top-level data or specversion key is the event itself; any other input becomes event.data.
  • funcd checks the input against the target's contract. A failed call throws, or raises a RuntimeError in Python, with the HTTP status and the response.
  • The target must exist when the caller is applied, and a link cannot close a cycle.
  • A target that serves HTTP is called with ctx.fetch, in TypeScript only; ctx.invoke refuses it.
  • An add-on object's engine is called through funcd, under an alias of spec.bindings.addon, with ctx.addon.fetch: Call its engine from a Function.
  • A run of a Job is started and read through funcd, under an alias of spec.bindings.job, with ctx.job.start and ctx.job.get: Start a run from a Function.

In the TypeScript example fn-to-fn, front calls greeter over this link (complete file); the Python method is also invoke:

apiVersion: funcd.io/v1alpha1
kind: Function
metadata:
  name: front
  namespace: default
  resourceGroup: rg1
spec:
  runtime: nodejs22
  handler: handle
  image: registry.example.com/front:1.0.0
  bindings:
    links:
      - alias: greeter
        target: greeter

The handler of front passes { data: { name } }, so greeter gets { name }:

import type { Handler } from '@funcd-dev/shim';

// A TYPE-only import of the callee's contract — erased at build, so front.mjs does NOT bundle
// greeter's code. It borrows greeter's input/output types so the invoke is type-checked, aliased
// because this module declares its OWN FuncInput/FuncOutput (its contract).
import type { FuncInput as GreeterInput, FuncOutput as GreeterOutput } from '../greeter/main.ts';

/**
 * What front accepts. `name` is OPTIONAL here (front is permissive) — so front can forward a payload
 * to greeter that greeter's stricter contract rejects, demonstrating that a target's 422 propagates
 * back through context.invoke.
 */
export interface FuncInput {
  name?: string;
}

/** What front returns — greeter's greeting, wrapped. */
export interface FuncOutput {
  via: string;
  greeting: string;
}

/**
 * front: the caller. Its Function declares a link under spec.bindings.links, with the alias greeter and
 * the target greeter, and the handler calls it with context.invoke. The link is the grant: without it,
 * context.invoke fails with 403. front forwards the caller's name as it is, so a missing name reaches
 * greeter, whose contract refuses it with 422, and the awaited invoke throws.
 */
export const handle: Handler<FuncInput, FuncOutput> = async (context, event) => {
  const reply = await context.invoke<{ data: GreeterInput }, GreeterOutput>('greeter', {
    data: { name: event.data?.name as string },
  });
  return { via: 'front', greeting: reply.greeting };
};

From the funcd-typescript repository, start the example:

$ funcdctl dev examples/fn-to-fn --gport 8080 --cport 8081

In another terminal, call front with a name:

$ curl -s -X POST -H 'content-type: application/json' -d '{"name":"ada"}' http://127.0.0.1:8080/function/front
{"via":"front","greeting":"Hello, ada!"}

greeter's contract requires a name, so a call without one gets 422 from greeter, and front, which does not catch the error, answers 500:

$ curl -s -X POST -H 'content-type: application/json' -d '{}' http://127.0.0.1:8080/function/front
{"error":"context.invoke(\"greeter\") failed: 422 {\"error\":\"event data does not match the input contract\",\"details\":[{\"path\":\"/name\",\"message\":\"missing property 'name'\"}]}"}

Press Ctrl-C in the first terminal to stop.

Serve HTTP requests

A Function whose protocol is http gets each HTTP request, with its method, path, headers and body, and answers as a web server does. It runs on Node.js, never shares a worker, and has no contract check: its contract file names the runtime and protocol: http, with no input or output. Only funcd's edge and another Function's ctx.fetch reach it.

The Function, in examples/http-api/devices.yaml:

# devices-api: a Function that serves HTTP. Each request reaches its Hono app.
apiVersion: funcd.io/v1alpha1
kind: Function
metadata:
  name: devices-api
  namespace: default
  resourceGroup: rg1
spec:
  runtime: nodejs22
  handler: handle
  protocol: http
  image: oci-layout:///mnt/funcd-deps/registry:devices
  replicas: 1
  scaling:
    minReplicas: 1

Its handler wraps a Hono app with http from @funcd-dev/shim; a route reaches the context, with its bindings, as c.env.funcd:

// devices-api: a Function that serves HTTP. http(app) makes the Hono app its export, which the shim calls
// with every request under spec.protocol: http.
import { http } from '@funcd-dev/shim';

import { app } from './app.ts';

export const handle = http(app);

The app gets the path after the Function's address, such as /devices for /function/devices-api/devices, and the removed part in X-Forwarded-Prefix. Start the example:

$ funcdctl dev examples/http-api --gport 8080

In another terminal, create a device, then list the devices:

$ curl -s -X POST -H 'content-type: application/json' -d '{"name":"truck-7"}' http://127.0.0.1:8080/function/devices-api/devices
{"id":"1","name":"truck-7"}
$ curl -s http://127.0.0.1:8080/function/devices-api/devices
[{"id":"1","name":"truck-7"}]

The folder's Route api serves the same app at /api/devices. Press Ctrl-C in the first terminal to stop.

Scale a Function to zero

A Function that sets neither replicas nor scaling.minReplicas runs no worker until its first call, which waits for a worker to start: a cold start. With scaling.idleTimeout, funcd stops the workers after that long without a call, and the Function is Idle until the next one. This Function stops after 5 minutes:

apiVersion: funcd.io/v1alpha1
kind: Function
metadata:
  name: pdf-render
  namespace: default
  resourceGroup: rg1
spec:
  runtime: nodejs22
  handler: handle
  image: registry.example.com/pdf-render:1.0.0
  scaling:
    idleTimeout: 5m
  • funcd looks for idle Functions every invoke.reclaimInterval, 30 seconds by default, so workers can outlive idleTimeout by that much. A call in flight keeps the Function awake.
  • A call waiting for a worker gets 503 after invoke.activationTimeout, 30 seconds by default; the calls that arrive during a start share that wait.
  • minReplicas above 0 keeps that many workers, and the Function never goes Idle. replicas above 0 with minReplicas 0 starts workers before the first call, but after Idle only a call starts one again.
  • Extra replicas are spares, not capacity: every call goes to the lowest-numbered worker that listens.

Access

A Function calls the targets of its links with no role. For each call, funcd asks Cedar whether the caller may perform link::invoke on the target, which this built-in policy allows in every namespace:

Built-in link policy
@id("link-invoke-binding")
permit(principal, action == Action::"link::invoke", resource)
  when { principal has links && principal.links.contains(resource) };
  • ctx.invoke and ctx.fetch reach only the caller's own links: an alias that no link declares fails with 403, whatever the role, with a message such as caller default/front declares no link \"nope\" (no link is no grant). A namespace Policy can forbid a call that a link allows, also with 403.
  • A client outside funcd calls through the edge, at /function/<name> or a Route, as spec.ingress allows. An auth.mode of authenticated needs server.auth.edge: true and a credential that may get the namespace's Functions, else 401 or 403; under funcdctl dev, such an address answers 401.

Limits and quotas

  • A call through the edge carries at most 1 MiB, or gets 413, and must start its response within the Function's timeout, or invoke.defaultTimeout, 60 seconds by default, a cold start included. Past it, the caller gets 504 while the handler goes on, so a retry can repeat its effects; a Python handler gets an exception a second later. ctx.remainingMs (remaining_ms) returns the time left, or null with no deadline.
  • A handler that blocks its worker, such as a loop that never awaits or a time.sleep, fails its liveness check, and funcd replaces the worker after runtime.livenessTimeout, 30 seconds by default.
  • A call over a link waits up to the link's timeout, 30 seconds by default and 5 minutes at most, a cold start included, then fails with 503. Its input holds at most 1 MiB, or gets 413; a larger result gets 500 when the target's contract checks results, else 413, as through ctx.fetch.
  • At most invoke.maxNestedInFlight link calls to one Function run at once, 10 by default; one more gets 429.
  • resources sets the CPU and memory of a Function's own workers, by default functions.defaultResources, 1 CPU and 512 MiB; only containerd enforces them, and kills a worker over its memory.
  • A log record over funclog.maxRecordBytes, 64 KiB by default, is cut.

Status

Read status.phase and the conditions with the first example's command. Ready says whether a worker serves, RevisionReady whether the latest spec's revision serves, and ShapeValid whether its worker loaded the handler; each phase row gives a reason of Ready.

Phase or conditionReasonMeaning
PendingWhat the Function waits for, such as KVStoreNotFound or SecretNotFoundThe Function waits, such as for a store or a Secret that it binds and that does not exist yet. It leaves Pending by itself once the wait is over.
DeployingShimNotReadyWorkers are starting, and none serves yet, such as for a new Function or one that a call wakes.
ReadyA worker is ready, and calls reach it.
IdleNoReplicasNo worker runs. The next call starts one.
DegradedRestartingThe Function has served, but no worker is ready now, such as while funcd replaces a worker that stopped. Calls wait for a ready worker.
FailedShapeInvalidThe handler cannot load, or the first worker did not become ready within runtime.bootTimeout, 1 minute by default. The Function stays Failed until its spec changes.
FailedRuntimeUnavailableThe Function's runtime is missing.
FailedArtifactUnresolvedThe image cannot be resolved.
FailedStartFailedA worker cannot start. funcd tries again after a growing wait.
FailedAnother reason, such as NoMatchingPlatform, ContractMissing, ContractInvalid or ConfigResolveFailedThe Function cannot run. The message of the Ready condition says why.
Ready conditionCrashLoopBackOffA worker stopped before it listened, and funcd waits before it starts the worker again.
RevisionReady conditionProgressingThe revision of the latest spec is starting beside the serving one.
RevisionReady conditionShapeInvalidThe revision of the latest spec cannot load its handler. After a change, the serving revision keeps the calls and the phase.
ShapeValid conditionShapeInvalidNo worker of the latest spec has loaded the handler: the code has no export that handler names, the code fails to load, or no worker became ready within runtime.bootTimeout. The message gives the load error, or the handler did not become ready within 1m0s in the last case.
IngressReady conditionReports the Function's own Route. Only a Function whose spec.ingress sets a host or a path has it.

funcd refuses the calls of a Failed Function at once with 503. status.currentRevision names the latest spec's revision.

Changes and deletion

  • funcd runs each version of a Function's spec as a revision, named after the Function and a growing number, such as log-burst-2; funcdctl get revisions -n <namespace> lists them.
  • A new revision's workers start beside the serving ones and take the calls once all are ready. An old worker stops once idle, and is cut off after runtime.drainGrace, 30 seconds by default. A revision that cannot load its handler takes no call.
  • A revision keeps the image digest of its creation, so push new code under a new tag, or set imageDigest.
  • A delete stops the workers at once, cutting their calls, and removes the revisions, with a Lake or a ConfigMap that the Function declares by name. A KVStore, Bucket or SQLDatabase that it declares by name stays, unless its deletion is delete.
  • funcd refuses to delete the target of another Function's link; remove the link first.

CLI errors

funcdctl dev stops at its start, before it serves any Function, when it meets one of these local errors:

ErrorAction
no node interpreter found for the node handlerPut Node.js on the PATH, or set FUNCD_NODE to the path of node.
no python interpreter found for the python handlerPut Python 3.12 or later on the PATH as python3, or set FUNCD_PYTHON to its path.
cannot load the runtime shim (needs Python ≥3.12; set FUNCD_PYTHON)Put Python 3.12 or later on the PATH as python3, or set FUNCD_PYTHON to its path.
no build of "<name>" or no contract file for "<name>"Build the Function, or name its build and its contract file after it, where Run it locally says that funcdctl dev looks. The message names the paths that it looked at.
A Function's manifest and contract file disagree on the runtime or protocol.Name the same runtime and protocol in both files.
A resource is in another namespace: funcdctl dev runs namespace default only.Set the resource's namespace to default.

Run it locally

  • funcdctl dev finds a Function's build by its name: <name>.mjs, in the folder or its dist folder, for Node.js; for Python, the entry that [tool.funcd-bundle.functions] of pyproject.toml names, else src/main.py in a folder of the Function's name, else <name>.py. The contract is <name>.contract.yaml or <name>.contract.json.
  • A saved change to the build or the contract starts a new revision within about a second.
  • It sets replicas and scaling.minReplicas to 1, so a Function never goes Idle.
  • It runs workers as local processes, with no isolation or limits, prints their logs, runs the namespace default only, and its edge has no authenticator.
  • Its data stays in memory unless --persist keeps it in .funcd-dev; delete that folder after it stops to start from nothing.

Examples

On this page