Skip to content

StartQuery

Source: src/AWS/Logs/StartQuery.ts

Runtime binding for logs:StartQuery (CloudWatch Logs Insights).

Bind this operation to a LogGroup inside a function runtime to get a callable that starts an Insights query scoped to the group, automatically injecting the log group name. Pair with GetQueryResults to poll for results.

Start an Insights Query

const startQuery = yield* AWS.Logs.StartQuery(logGroup);
const { queryId } = yield* startQuery({
queryString: "fields @timestamp, @message | limit 10",
startTime: startEpochSeconds,
endTime: endEpochSeconds,
});

Wire into a Lambda Function

// Insights queries are asynchronous: start one, then poll with the
// GetQueryResults binding. Provide both HTTP layers with Layer.mergeAll.
export default InsightsFunction.make(
{ main: import.meta.url, url: true },
Effect.gen(function* () {
const logGroup = yield* AWS.Logs.LogGroup("AppLogs", {});
const startQuery = yield* AWS.Logs.StartQuery(logGroup);
const getQueryResults = yield* AWS.Logs.GetQueryResults(logGroup);
return {
fetch: Effect.gen(function* () {
const now = yield* Clock.currentTimeMillis;
const { queryId } = yield* startQuery({
queryString: "fields @timestamp, @message | limit 10",
startTime: Math.floor(now / 1000) - 3600,
endTime: Math.floor(now / 1000),
});
return HttpServerResponse.json({ queryId });
}),
};
}).pipe(
Effect.provide(
Layer.mergeAll(AWS.Logs.StartQueryHttp, AWS.Logs.GetQueryResultsHttp),
),
),
);