createScheduledQuery method

Future<CreateScheduledQueryResponse> createScheduledQuery({
  1. required String executionRoleArn,
  2. required String name,
  3. required QueryLanguage queryLanguage,
  4. required String queryString,
  5. required String scheduleExpression,
  6. String? description,
  7. DestinationConfiguration? destinationConfiguration,
  8. List<String>? logGroupIdentifiers,
  9. int? scheduleEndTime,
  10. int? scheduleStartTime,
  11. int? startTimeOffset,
  12. ScheduledQueryState? state,
  13. Map<String, String>? tags,
  14. String? timezone,
})

Creates a scheduled query that runs CloudWatch Logs Insights queries at regular intervals. Scheduled queries enable proactive monitoring by automatically executing queries to detect patterns and anomalies in your log data. Query results can be delivered to Amazon S3 for analysis or further processing.

May throw AccessDeniedException. May throw ConflictException. May throw InternalServerException. May throw ResourceNotFoundException. May throw ServiceQuotaExceededException. May throw ThrottlingException. May throw ValidationException.

Parameter executionRoleArn : The ARN of the IAM role that grants permissions to execute the query and deliver results to the specified destination. The role must have permissions to read from the specified log groups and write to the destination.

Parameter name : The name of the scheduled query. The name must be unique within your account and region. Valid characters are alphanumeric characters, hyphens, underscores, and periods. Length must be between 1 and 255 characters.

Parameter queryLanguage : The query language to use for the scheduled query. Valid values are CWLI, PPL, and SQL.

Parameter queryString : The query string to execute. This is the same query syntax used in CloudWatch Logs Insights. Maximum length is 10,000 characters.

Parameter scheduleExpression : A cron expression that defines when the scheduled query runs. The expression uses standard cron syntax and supports minute-level precision. Maximum length is 256 characters.

Parameter description : An optional description for the scheduled query to help identify its purpose and functionality.

Parameter destinationConfiguration : Configuration for where to deliver query results. Currently supports Amazon S3 destinations for storing query output.

Parameter logGroupIdentifiers : An array of log group names or ARNs to query. You can specify between 1 and 50 log groups. Log groups can be identified by name or full ARN.

Parameter scheduleEndTime : The end time for the scheduled query in Unix epoch format. The query will stop executing after this time.

Parameter scheduleStartTime : The start time for the scheduled query in Unix epoch format. The query will not execute before this time.

Parameter startTimeOffset : The time offset in seconds that defines the lookback period for the query. This determines how far back in time the query searches from the execution time.

Parameter state : The initial state of the scheduled query. Valid values are ENABLED and DISABLED. Default is ENABLED.

Parameter tags : Key-value pairs to associate with the scheduled query for resource management and cost allocation.

Parameter timezone : The timezone for evaluating the schedule expression. This determines when the scheduled query executes relative to the specified timezone.

Implementation

Future<CreateScheduledQueryResponse> createScheduledQuery({
  required String executionRoleArn,
  required String name,
  required QueryLanguage queryLanguage,
  required String queryString,
  required String scheduleExpression,
  String? description,
  DestinationConfiguration? destinationConfiguration,
  List<String>? logGroupIdentifiers,
  int? scheduleEndTime,
  int? scheduleStartTime,
  int? startTimeOffset,
  ScheduledQueryState? state,
  Map<String, String>? tags,
  String? timezone,
}) async {
  _s.validateNumRange(
    'scheduleEndTime',
    scheduleEndTime,
    0,
    1152921504606846976,
  );
  _s.validateNumRange(
    'scheduleStartTime',
    scheduleStartTime,
    0,
    1152921504606846976,
  );
  final headers = <String, String>{
    'Content-Type': 'application/x-amz-json-1.1',
    'X-Amz-Target': 'Logs_20140328.CreateScheduledQuery'
  };
  final jsonResponse = await _protocol.send(
    method: 'POST',
    requestUri: '/',
    exceptionFnMap: _exceptionFns,
    // TODO queryParams
    headers: headers,
    payload: {
      'executionRoleArn': executionRoleArn,
      'name': name,
      'queryLanguage': queryLanguage.value,
      'queryString': queryString,
      'scheduleExpression': scheduleExpression,
      if (description != null) 'description': description,
      if (destinationConfiguration != null)
        'destinationConfiguration': destinationConfiguration,
      if (logGroupIdentifiers != null)
        'logGroupIdentifiers': logGroupIdentifiers,
      if (scheduleEndTime != null) 'scheduleEndTime': scheduleEndTime,
      if (scheduleStartTime != null) 'scheduleStartTime': scheduleStartTime,
      if (startTimeOffset != null) 'startTimeOffset': startTimeOffset,
      if (state != null) 'state': state.value,
      if (tags != null) 'tags': tags,
      if (timezone != null) 'timezone': timezone,
    },
  );

  return CreateScheduledQueryResponse.fromJson(jsonResponse.body);
}