Skip to content

PutLogEvents

Source: src/AWS/Logs/PutLogEvents.ts

Runtime binding for logs:PutLogEvents.

Bind this operation to a LogGroup inside a function runtime to get a callable that writes log events to a stream of the group (e.g. custom audit trails), automatically injecting the log group name. The target log stream must already exist (declare an AWS.Logs.LogStream). Sequence tokens are no longer required by CloudWatch Logs.

Write an Audit Event

const putLogEvents = yield* AWS.Logs.PutLogEvents(logGroup);
yield* putLogEvents({
logStreamName: stream.logStreamName,
logEvents: [{ timestamp: now, message: "user.login id=123" }],
});

Wire into a Lambda Function

// Bind in the init phase, call in the handler, and provide the
// PutLogEventsHttp layer on the Function's init Effect.
export default AuditFunction.make(
{ main: import.meta.url, url: true },
Effect.gen(function* () {
const logGroup = yield* AWS.Logs.LogGroup("AuditLogs", {
retention: "30 days",
});
const stream = yield* AWS.Logs.LogStream("AuditStream", {
logGroupName: logGroup.logGroupName,
});
const putLogEvents = yield* AWS.Logs.PutLogEvents(logGroup);
const LogStreamName = yield* stream.logStreamName;
return {
fetch: Effect.gen(function* () {
const timestamp = yield* Clock.currentTimeMillis;
yield* putLogEvents({
logStreamName: yield* LogStreamName,
logEvents: [{ timestamp, message: "audit.event" }],
});
return HttpServerResponse.text("ok");
}),
};
}).pipe(Effect.provide(AWS.Logs.PutLogEventsHttp)),
);