Functions¶
Deploy function bundles and expose them through gateway routes. HTTP and websocket $connect events carry gateway request headers, including X-Forwarded-For and X-Kernstack-Viewer-* location fields.
Declare a function¶
Add an entry under resources.functions[] in stack.json (omit src when source lives at functions/{name}), add a matching gateway.routes[] entry, then run kernstack deploy. See the IAC reference.
Workflow¶
- Add a function:
kernstack add function --name api(or editstack.json). - Deploy:
kernstack deploy(orkernstack deploy --only function:api). - Call the HTTP route URL from the deploy output.
- Optionally add schedules under
resources.functions[].schedules[].
Change stream events¶
A function named in a table's change_stream.function receives batched item changes. See change streams for delivery semantics and the record format.
func handler(ctx context.Context, req sdk_types_functions.FunctionInvokeRequest) (sdk_types_functions.FunctionInvokeResponse, error) {
if req.IsChangeStreamEvent() {
for _, rec := range req.ChangeRecords {
switch rec.ChangeType {
case "INSERT", "MODIFY":
// rec.NewItem holds the item after the write; rec.OldItem is nil for new items
case "REMOVE":
// rec.OldItem holds the deleted item
}
_ = rec.Keys
}
return sdk_types_functions.FunctionInvokeResponse{StatusCode: 200}, nil
}
// ... HTTP, schedule and other triggers
}
req.ChangeTableName names the source table, so one function can serve several tables. Returning an error (or a 5xx or 429 response) retries the whole batch, and later changes wait until it succeeds or is 24 hours old. Other 4xx responses are not retried. A record can occasionally be delivered more than once, so make the handler idempotent; rec.ChangeID identifies each change.
CLI¶
| Operation | Command |
|---|---|
| Add blueprint | kernstack add function [--name NAME] |
| Serve locally | kernstack serve [--function NAME] |
| Deploy | kernstack deploy |
| Scoped deploy | kernstack deploy --only function:NAME |
| Tail logs | kernstack logs --function NAME |