Skip to content

LogGroup

Source: src/AWS/Logs/LogGroup.ts

A CloudWatch Logs log group — the container for log streams and the unit that retention, encryption, metric filters, and subscriptions attach to.

ECS Task Log Group

const logs = yield* LogGroup("TaskLogs", {
retention: "7 days",
});

Encrypted Log Group with Deletion Protection

const key = yield* AWS.KMS.Key("LogsKey");
const logs = yield* LogGroup("AuditLogs", {
retention: "30 days",
kmsKeyId: key.keyArn,
deletionProtectionEnabled: true,
});

Declare a LogStream and use the PutLogEvents binding inside a Lambda function (or the batching LogEventSink for high-volume streams).

// init
const logGroup = yield* AWS.Logs.LogGroup("AuditLogs", {
retention: "30 days",
});
const stream = yield* AWS.Logs.LogStream("AuditStream", {
logGroupName: logGroup.logGroupName,
logStreamName: "audit",
});
const putLogEvents = yield* AWS.Logs.PutLogEvents(logGroup);
// runtime
yield* putLogEvents({
logStreamName: "audit",
logEvents: [{ timestamp, message: "user.login id=123" }],
});
// Subscribe a Lambda handler to matching events (creates the
// subscription filter + invoke permission automatically).
yield* AWS.Logs.consumeLogEvents(
logGroup,
{ filterPattern: "?ERROR ?Error" },
(events) =>
Stream.runForEach(events, (event) =>
Effect.log(`${event.logStream}: ${event.message}`),
),
);
yield* AWS.Logs.MetricFilter("ErrorCount", {
logGroupName: logGroup.logGroupName,
filterPattern: '"ERROR"',
metricTransformations: [{
metricName: "ErrorCount",
metricNamespace: "MyApp",
metricValue: "1",
}],
});