Logs
Everything a Function or a Task writes to its output goes to CloudWatch Logs. Each line becomes a log event, with a timestamp and a message. Events are kept in log streams, one per source: Lambda gives every execution environment its own stream. Streams sit in a log group, which is what you set retention on, search, and filter. A Function’s log group is /aws/lambda/<name>.
| log stream | time | message |
|---|---|---|
| 2026/12/03/[$LATEST]4f1c | 12:00:01 | GET /orders 200 |
| 2026/12/03/[$LATEST]4f1c | 12:00:02 | ERROR payment declined |
| 2026/12/03/[$LATEST]9a2e | 12:00:01 | GET /orders 200 |
| 2026/12/03/[$LATEST]9a2e | 12:00:03 | GET /health 200 |
Keep logs as long as you need them
Section titled “Keep logs as long as you need them”A new log group keeps its events forever. Set a retention on the Function or Task, and the log group is created with it before the first invocation:
{ main: import.meta.url, logging: { retention: "2 weeks" } }The value is rounded up to a retention CloudWatch supports, and "forever" clears it. Without logging, Lambda creates the log group itself on the first invocation, with no expiry.
Read them from your terminal
Section titled “Read them from your terminal”pnpm alchemy logs --resource Api --tailbun alchemy logs --resource Api --tailnpx alchemy logs --resource Api --tailyarn alchemy logs --resource Api --tail--resource takes the logical IDs of the runtimes to read, and --tail keeps streaming new events. See alchemy logs.
Your own log group
Section titled “Your own log group”Declare a log group when something other than a runtime writes to it, such as OpenTelemetry logs or an audit trail. deletionProtectionEnabled keeps it from being deleted, and kmsKeyId encrypts it with your own key:
import * as AWS from "@alchemy.run/aws";
export const AuditLogs = AWS.Logs.LogGroup("AuditLogs", { retention: "30 days", deletionProtectionEnabled: true,});Events go into a stream, and CloudWatch Logs doesn’t create streams on write, so declare one:
const audit = yield* AuditLogs;
yield* AWS.Logs.LogStream("AuditStream", { logGroupName: audit.logGroupName, logStreamName: "audit",});A Function writes to it with the PutLogEvents binding:
Effect.gen(function* () { const putLogEvents = yield* AWS.Logs.PutLogEvents(AuditLogs); return { fetch: Effect.gen(function* () { yield* putLogEvents({ logStreamName: "audit", logEvents: [{ timestamp: yield* Clock.currentTimeMillis, message: "user.login id=123" }], }); return HttpServerResponse.text("ok"); }), };});Count log lines as a metric
Section titled “Count log lines as a metric”A metric filter turns matching log events into a CloudWatch metric. Count the error lines in a Function’s log group, then alarm on the count like any other metric:
const api = yield* Api;
yield* AWS.Logs.MetricFilter("ErrorCount", { logGroupName: Output.interpolate`/aws/lambda/${api.functionName}`, filterPattern: "?ERROR ?Error", metricTransformations: [ { metricName: "ErrorCount", metricNamespace: "MyApp", metricValue: "1", defaultValue: 0, }, ],});A filter can also read a value out of JSON log lines. filterPattern: "{ $.latencyMs = * }" with metricValue: "$.latencyMs" records each request’s latency.
Act on log lines
Section titled “Act on log lines”To run code for each matching event, consume a log group from a Function. Lambda delivers the events in batches:
import * as AWS from "@alchemy.run/aws";import * as Effect from "effect/Effect";import * as Stream from "effect/Stream";import { AuditLogs } from "./logs.ts";
export default class Alerts extends AWS.Lambda.Function<Alerts>()( "Alerts", { main: import.meta.url }, Effect.gen(function* () { yield* AWS.Logs.consumeLogEvents(AuditLogs, { filterPattern: "?ERROR ?Error" }, (events) => Stream.runForEach(events, (event) => Effect.log(`${event.logStream}: ${event.message}`)), ); return {}; }).pipe(Effect.provide(AWS.Lambda.LogGroupEventSource)),) {}This creates a subscription filter on the log group, and the permission for CloudWatch Logs to invoke the Function. To send every event somewhere else, such as a Kinesis stream for another account to read, declare the subscription filter yourself:
const audit = yield* AuditLogs;
yield* AWS.Logs.SubscriptionFilter("StreamFanout", { logGroupName: audit.logGroupName, filterPattern: "", destinationArn: stream.streamArn, roleArn: role.roleArn,});A log group can have two subscription filters at most.
Turn logs on for every resource of a kind
Section titled “Turn logs on for every resource of a kind”Some AWS resources write logs only once you turn them on, one at a time: VPC flow logs, for instance. A telemetry rule turns them on for every resource of a type in the account, including ones created later:
yield* AWS.ObservabilityAdmin.TelemetryConfig("Telemetry");
yield* AWS.ObservabilityAdmin.TelemetryRule("FlowLogs", { resourceType: "AWS::EC2::VPC", telemetryType: "Logs", telemetrySourceTypes: ["VPC_FLOW_LOGS"], destinationConfiguration: { DestinationType: "cloud-watch-logs", Retention: "30 days", },});TelemetryConfig turns on the account’s telemetry configuration, which rules need first.