Worker
A Worker is stateless code that runs on the cloud's serverless runtime and costs nothing while idle. It fits HTTP APIs, webhooks, queue consumers, storage event handlers, and scheduled jobs.
import * as alien from "@alienplatform/core"
const api = new alien.Worker("api")
.code({ type: "source", src: "./api", toolchain: { type: "typescript" } })
.publicEndpoint("api")
.permissions("execution")
.build()
export default new alien.Stack("acme")
.add(api, "live")
.permissions({ profiles: { execution: {} } })
.build().permissions("execution") names the Worker's permission profile, the set of cloud permissions it runs with. execution grants nothing here, because the Worker uses no other resource.
| Platform | What Alien creates | Scales to zero |
|---|---|---|
| AWS | Lambda function from a container image, ARM64 (Graviton) | Yes |
| Google Cloud | Cloud Run service (gen2 execution environment) | Yes |
| Azure | Container App in the stack's Container Apps environment | Private Workers only. A Worker with a public endpoint keeps one replica running. |
| Kubernetes | Deployment and Service, one replica | No |
| Machines | Not supported. Use a Daemon or a stateless Container. | |
| Local | Native process extracted from the built image | No |
A Worker is always "live": Alien creates and updates it on every release, and the build refuses any other lifecycle. See Frozen and live. Triggers per platform are in Events and triggers.
When to use another type
- Container: long-running processes, persistent connections such as WebSockets, state on disk, or more than one hour per request.
- Daemon: one process per machine.
Use it from code
Register handlers from @alienplatform/sdk in the entry module. The default export is an HTTP app that serves the public endpoint.
import { Hono } from "hono"
import { command, onQueueMessage, waitUntil } from "@alienplatform/sdk"
import { z } from "zod"
const app = new Hono()
app.get("/health", c => c.json({ ok: true }))
const ResizeInput = z.object({ id: z.string() })
command("resize", ResizeInput, async ({ id }, ctx) => {
return { id, attempt: ctx.attempt }
})
onQueueMessage<{ imageId: string }>("tasks", async message => {
console.log(message.payload.imageId, message.attemptCount)
})
export default appEvery handler and type is listed in the API reference.
Common patterns
Public API. Add .publicEndpoint("api") and export an HTTP app. Add .readinessProbe({ path: "/health" }) so each deploy waits until the new version answers. See Networking.
Queue consumer. Add a queue trigger and register onQueueMessage. Make the handler idempotent, because delivery is at least once. For throughput, opt into batches. See Events and triggers.
Scheduled job. Add .trigger({ type: "schedule", cron: "0 3 * * *" }) and register onCronEvent. Schedules run in UTC.
Operations you trigger in each environment. Enable .commandsEnabled(true) and register command() handlers, for example to run a migration or collect diagnostics in one customer's environment. Your own backend calls them through Alien, so the Worker needs no public endpoint and the environment needs no inbound access. See Commands.
Call another Worker. Call its public endpoint, or send it a command:
import { CommandsClient } from "@alienplatform/commands"
const commands = new CommandsClient({ managerUrl, deploymentId, token })
const result = await commands.target("image-processor").invoke("resize", payload)From Rust or Python, link the target Worker and call it through its binding. See Worker binding.
Background work after the response. Use waitUntil for logging, analytics, and cache updates.
Options
| Method | Type | Default | Notes |
|---|---|---|---|
.code(code) | WorkerCode | required | Prebuilt image or source build. See Toolchains. |
.permissions(profile) | string | required | Permission profile name. |
.memoryMb(mb) | number | 512 | 128 to 32,768. AWS allows up to 10,240. Google Cloud requires at least 512. Azure uses fixed pairs from 512 to 4,096 and rounds up. See Memory. |
.timeoutSeconds(sec) | number | 180 | 1 to 3,600. AWS Lambda stops at 900. |
.concurrencyLimit(n) | number | undefined | platform default | Reserved concurrency on Lambda, max instances on Cloud Run, max replicas on Container Apps. |
.publicEndpoint(name, opts?) | string, { hostLabel?, wildcardSubdomains? } | none (private) | Named HTTPS endpoint. Repeatable. hostLabel: "@" serves the domain apex. wildcardSubdomains defaults to false. Can't change after create. |
.readinessProbe(probe) | { method?, path? } | none | Checked after each deploy. Needs a public endpoint. Method defaults to GET, path to /. |
.commandsEnabled(on) | boolean | false | Lets the Worker receive commands. |
.trigger(trigger) | WorkerTrigger | none | Repeatable. See Events and triggers. |
.environment(vars) | Record<string, string> | {} | Replaces the whole map. Names reserved by Alien are refused. |
.link(resource) | Resource | none | Repeatable. Injects the resource's binding and grants the profile's permissions on it. |
.enabled(input) | boolean stack input | always | See Using resources. |
The Worker ID is part of cloud resource names. Use lowercase letters, digits, and single hyphens. It can be at most 23 characters on Azure and 55 on AWS.
Environment variables
.environment() sets fixed values. Per-deployment values and secrets are set when the deployment is created.
Alien injects ALIEN_* variables, including one ALIEN_<NAME>_BINDING per linked resource. See Environment variables for the injected names, the CLI flags, and the reserved names.
Permissions
| Set | Grants |
|---|---|
worker/execute | Runtime needs of the Worker itself, such as image pull and logs. |
worker/invoke | Invoking the Worker from another workload. |
worker/dispatch-command | Dispatching commands to the Worker. Added when commands are enabled. |
worker/provision, worker/management, worker/heartbeat | Used by initial setup and the management identity. |