Skip to content

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>.

/aws/lambda/api · log groupfilterPattern "?ERROR ?Error"
log streamtimemessage
2026/12/03/[$LATEST]4f1c12:00:01GET /orders 200
2026/12/03/[$LATEST]4f1c12:00:02ERROR payment declined
2026/12/03/[$LATEST]9a2e12:00:01GET /orders 200
2026/12/03/[$LATEST]9a2e12:00:03GET /health 200

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:

src/api.ts
{ 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.

Terminal window
pnpm alchemy logs --resource Api --tail

--resource takes the logical IDs of the runtimes to read, and --tail keeps streaming new events. See alchemy logs.

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:

src/logs.ts
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:

alchemy.run.ts
const audit = yield* AuditLogs;
yield* AWS.Logs.LogStream("AuditStream", {
logGroupName: audit.logGroupName,
logStreamName: "audit",
});

A Function writes to it with the PutLogEvents binding:

src/api.ts
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");
}),
};
});

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:

alchemy.run.ts
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.

To run code for each matching event, consume a log group from a Function. Lambda delivers the events in batches:

src/alerts.ts
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:

alchemy.run.ts
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.

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:

alchemy.run.ts
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.