AWS.CloudTrail reference
CancelQuery
Section titled “CancelQuery”Source:
src/AWS/CloudTrail/CancelQuery.ts
Runtime binding for cloudtrail:CancelQuery.
Cancels a running CloudTrail Lake query. Cancelling a query that already
finished fails with the typed InactiveQueryException. Provide the
implementation with Effect.provide(AWS.CloudTrail.CancelQueryHttp).
CancelQuery: Querying CloudTrail Lake
Section titled “CancelQuery: Querying CloudTrail Lake”// init — bind the operation to the event data storeconst cancelQuery = yield* AWS.CloudTrail.CancelQuery(store);
// runtimeconst result = yield* cancelQuery({ QueryId: queryId });console.log(result.QueryStatus); // CANCELLEDDescribeQuery
Section titled “DescribeQuery”Source:
src/AWS/CloudTrail/DescribeQuery.ts
Runtime binding for cloudtrail:DescribeQuery.
Reads a CloudTrail Lake query’s status, statistics, and error message by
QueryId — the polling half of the start/poll/results flow. Provide the
implementation with Effect.provide(AWS.CloudTrail.DescribeQueryHttp).
DescribeQuery: Querying CloudTrail Lake
Section titled “DescribeQuery: Querying CloudTrail Lake”// init — bind the operation to the event data storeconst describeQuery = yield* AWS.CloudTrail.DescribeQuery(store);
// runtimeconst status = yield* describeQuery({ QueryId: queryId });console.log(status.QueryStatus); // QUEUED | RUNNING | FINISHED | ...EventDataStore
Section titled “EventDataStore”Source:
src/AWS/CloudTrail/EventDataStore.ts
A CloudTrail Lake event data store — an immutable collection of events that can be queried with SQL via CloudTrail Lake.
Deleting an event data store schedules it for deletion
(PENDING_DELETION); AWS purges it after a seven-day wait period during
which it incurs no cost. If a store with the same name is still pending
deletion, the reconciler restores it instead of creating a duplicate.
EventDataStore: Creating Event Data Stores
Section titled “EventDataStore: Creating Event Data Stores”Basic Event Data Store
import * as AWS from "alchemy/AWS";
const store = yield* AWS.CloudTrail.EventDataStore("Lake", { retentionPeriod: "7 days", terminationProtectionEnabled: false,});Single-Region Store with Custom Selectors
const store = yield* AWS.CloudTrail.EventDataStore("S3DataEvents", { multiRegionEnabled: false, retentionPeriod: "30 days", terminationProtectionEnabled: false, advancedEventSelectors: [ { name: "S3 data events", fieldSelectors: [ { field: "eventCategory", equals: ["Data"] }, { field: "resources.type", equals: ["AWS::S3::Object"] }, ], }, ],});GenerateQuery
Section titled “GenerateQuery”Source:
src/AWS/CloudTrail/GenerateQuery.ts
Runtime binding for cloudtrail:GenerateQuery.
Generates a CloudTrail Lake SQL statement from a natural-language prompt
against the bound EventDataStore (the store list is injected from
the binding). Provide the implementation with
Effect.provide(AWS.CloudTrail.GenerateQueryHttp).
GenerateQuery: Querying CloudTrail Lake
Section titled “GenerateQuery: Querying CloudTrail Lake”// init — bind the operation to the event data storeconst generateQuery = yield* AWS.CloudTrail.GenerateQuery(store);
// runtimeconst result = yield* generateQuery({ Prompt: "What are my top errors in the past month?",});console.log(result.QueryStatement);GetQueryResults
Section titled “GetQueryResults”Source:
src/AWS/CloudTrail/GetQueryResults.ts
Runtime binding for cloudtrail:GetQueryResults.
Reads one page of a finished CloudTrail Lake query’s result rows — use
NextToken/MaxQueryResults to paginate large results. Provide the
implementation with Effect.provide(AWS.CloudTrail.GetQueryResultsHttp).
GetQueryResults: Querying CloudTrail Lake
Section titled “GetQueryResults: Querying CloudTrail Lake”// init — bind the operation to the event data storeconst getQueryResults = yield* AWS.CloudTrail.GetQueryResults(store);
// runtimeconst page = yield* getQueryResults({ QueryId: queryId });console.log(page.QueryResultRows?.length);ListInsightsData
Section titled “ListInsightsData”Source:
src/AWS/CloudTrail/ListInsightsData.ts
Runtime binding for cloudtrail:ListInsightsData.
An account-level operation (no resource argument) that reads the raw
Insights events recorded for an insight source — empty when Insights has
recorded no anomalies. Rate-limited by AWS to two requests per second,
per account, per Region. Provide the implementation with
Effect.provide(AWS.CloudTrail.ListInsightsDataHttp).
ListInsightsData: Reading Insights Events
Section titled “ListInsightsData: Reading Insights Events”// init — account-level binding takes no resourceconst listInsightsData = yield* AWS.CloudTrail.ListInsightsData();
// runtimeconst result = yield* listInsightsData({ InsightSource: "s3.amazonaws.com", DataType: "InsightsEvents", MaxResults: 10,});console.log((result.Events ?? []).map((e) => e.EventName));ListInsightsMetricData
Section titled “ListInsightsMetricData”Source:
src/AWS/CloudTrail/ListInsightsMetricData.ts
Runtime binding for cloudtrail:ListInsightsMetricData.
An account-level operation (no resource argument) that reads the Insights
metric time series (API call rate / error rate) for a given event source
and event name — empty when Insights has recorded no anomalies. Provide
the implementation with
Effect.provide(AWS.CloudTrail.ListInsightsMetricDataHttp).
ListInsightsMetricData: Reading Insights Metrics
Section titled “ListInsightsMetricData: Reading Insights Metrics”// init — account-level binding takes no resourceconst listInsightsMetricData = yield* AWS.CloudTrail.ListInsightsMetricData();
// runtimeconst result = yield* listInsightsMetricData({ EventSource: "s3.amazonaws.com", EventName: "PutObject", InsightType: "ApiCallRateInsight",});console.log(result.Timestamps?.length, result.Values?.length);ListPublicKeys
Section titled “ListPublicKeys”Source:
src/AWS/CloudTrail/ListPublicKeys.ts
Runtime binding for cloudtrail:ListPublicKeys.
An account-level operation (no resource argument) that returns the public
keys used to sign CloudTrail digest files in the region — the building
block for verifying log-file integrity at runtime. Provide the
implementation with Effect.provide(AWS.CloudTrail.ListPublicKeysHttp).
ListPublicKeys: Verifying Log Integrity
Section titled “ListPublicKeys: Verifying Log Integrity”// init — account-level binding takes no resourceconst listPublicKeys = yield* AWS.CloudTrail.ListPublicKeys();
// runtimeconst result = yield* listPublicKeys();console.log(result.PublicKeyList?.map((k) => k.Fingerprint));ListQueries
Section titled “ListQueries”Source:
src/AWS/CloudTrail/ListQueries.ts
Runtime binding for cloudtrail:ListQueries.
Lists the CloudTrail Lake queries that ran against the bound
EventDataStore in the last seven days — the store is injected from
the binding. Provide the implementation with
Effect.provide(AWS.CloudTrail.ListQueriesHttp).
ListQueries: Querying CloudTrail Lake
Section titled “ListQueries: Querying CloudTrail Lake”// init — bind the operation to the event data storeconst listQueries = yield* AWS.CloudTrail.ListQueries(store);
// runtimeconst result = yield* listQueries({ MaxResults: 10 });console.log(result.Queries?.map((q) => q.QueryId));LookupEvents
Section titled “LookupEvents”Source:
src/AWS/CloudTrail/LookupEvents.ts
Runtime binding for cloudtrail:LookupEvents.
An account-level operation (no resource argument) that searches the last
90 days of management events recorded in the region — the classic “who did
what” audit read, available without any trail or event data store. Provide
the implementation with Effect.provide(AWS.CloudTrail.LookupEventsHttp).
LookupEvents: Reading Event History
Section titled “LookupEvents: Reading Event History”// init — account-level binding takes no resourceconst lookupEvents = yield* AWS.CloudTrail.LookupEvents();
// runtimeconst result = yield* lookupEvents({ LookupAttributes: [ { AttributeKey: "EventName", AttributeValue: "CreateBucket" }, ], MaxResults: 10,});console.log(result.Events?.map((e) => e.EventName));StartQuery
Section titled “StartQuery”Source:
src/AWS/CloudTrail/StartQuery.ts
Runtime binding for cloudtrail:StartQuery.
Starts a CloudTrail Lake SQL query against the bound
EventDataStore and returns the QueryId to poll with
DescribeQuery / GetQueryResults. Provide the implementation
with Effect.provide(AWS.CloudTrail.StartQueryHttp).
StartQuery: Querying CloudTrail Lake
Section titled “StartQuery: Querying CloudTrail Lake”// init — bind the operation to the event data storeconst startQuery = yield* AWS.CloudTrail.StartQuery(store);
// runtime — the callback receives the store's ID for the FROM clauseconst { QueryId } = yield* startQuery({ QueryStatement: (id) => `SELECT eventID, eventName FROM ${id} LIMIT 10`,});Source:
src/AWS/CloudTrail/Trail.ts
An AWS CloudTrail trail that records AWS API activity and delivers log files to an S3 bucket.
The destination bucket must carry a bucket policy that allows the
cloudtrail.amazonaws.com service principal to call s3:GetBucketAcl
on the bucket and s3:PutObject under AWSLogs/{accountId}/*, both
scoped with an aws:SourceArn condition on the trail’s ARN.
Trail: Creating Trails
Section titled “Trail: Creating Trails”Basic Trail
import * as AWS from "alchemy/AWS";
const bucket = yield* AWS.S3.Bucket("TrailLogs", { bucketName: `audit-logs-${accountId}`, forceDestroy: true, policy: [ { Effect: "Allow", Principal: { Service: "cloudtrail.amazonaws.com" }, Action: ["s3:GetBucketAcl"], Resource: `arn:aws:s3:::audit-logs-${accountId}`, Condition: { StringEquals: { "aws:SourceArn": trailArn } }, }, { Effect: "Allow", Principal: { Service: "cloudtrail.amazonaws.com" }, Action: ["s3:PutObject"], Resource: `arn:aws:s3:::audit-logs-${accountId}/AWSLogs/${accountId}/*`, Condition: { StringEquals: { "s3:x-amz-acl": "bucket-owner-full-control", "aws:SourceArn": trailArn, }, }, }, ],});
const trail = yield* AWS.CloudTrail.Trail("Audit", { trailName: "audit-trail", s3BucketName: bucket.bucketName,});Multi-Region Trail with Log File Validation
const trail = yield* AWS.CloudTrail.Trail("Audit", { trailName: "org-audit-trail", s3BucketName: bucket.bucketName, isMultiRegionTrail: true, enableLogFileValidation: true,});Trail: Controlling Logging
Section titled “Trail: Controlling Logging”const trail = yield* AWS.CloudTrail.Trail("Audit", { trailName: "audit-trail", s3BucketName: bucket.bucketName, isLogging: false,});Trail: Selecting Events
Section titled “Trail: Selecting Events”const trail = yield* AWS.CloudTrail.Trail("Audit", { trailName: "audit-trail", s3BucketName: bucket.bucketName, advancedEventSelectors: [ { name: "Management events only", fieldSelectors: [ { field: "eventCategory", equals: ["Management"] }, ], }, ], insightSelectors: [{ insightType: "ApiCallRateInsight" }],});