Skip to content

put_query_definition async

put_query_definition(
    name: QueryDefinitionName,
    query_string: QueryDefinitionString,
    *,
    config_overrides: Optional[AsyncCloudWatchLogsClientConfig] = None,
    query_language: Optional[QueryLanguage] = None,
    query_definition_id: Optional[QueryId] = None,
    log_group_names: Optional[LogGroupNames] = None,
    client_token: Optional[ClientToken] = None,
    parameters: Optional[QueryParameterList] = None,
) -> PutQueryDefinitionResponse

Creates or updates a query definition for CloudWatch Logs Insights. For more information, see Analyzing Log Data with CloudWatch Logs Insights.

To update a query definition, specify its queryDefinitionId in your request. The values of name, queryString, and logGroupNames are changed to the values that you specify in your update operation. No current values are retained from the current query definition. For example, imagine updating a current query definition that includes log groups. If you don't specify the logGroupNames parameter in your update operation, the query definition changes to contain no log groups.

You must have the logs:PutQueryDefinition permission to be able to perform this operation.

Parameters:

  • query_language (Optional[QueryLanguage], default: None ) –

    Specify the query language to use for this query. The options are Logs Insights QL, OpenSearch PPL, and OpenSearch SQL. For more information about the query languages that CloudWatch Logs supports, see Supported query languages.

  • name (QueryDefinitionName) –

    A name for the query definition. If you are saving numerous query definitions, we recommend that you name them. This way, you can find the ones you want by using the first part of the name as a filter in the queryDefinitionNamePrefix parameter of DescribeQueryDefinitions.

  • query_definition_id (Optional[QueryId], default: None ) –

    If you are updating a query definition, use this parameter to specify the ID of the query definition that you want to update. You can use DescribeQueryDefinitions to retrieve the IDs of your saved query definitions.

    If you are creating a query definition, do not specify this parameter. CloudWatch generates a unique ID for the new query definition and include it in the response to this operation.

  • log_group_names (Optional[LogGroupNames], default: None ) –

    Use this parameter to include specific log groups as part of your query definition. If your query uses the OpenSearch Service query language, you specify the log group names inside the querystring instead of here.

    If you are updating an existing query definition for the Logs Insights QL or OpenSearch Service PPL and you omit this parameter, then the updated definition will contain no log groups.

  • query_string (QueryDefinitionString) –

    The query string to use for this definition. For more information, see CloudWatch Logs Insights Query Syntax.

  • client_token (Optional[ClientToken], default: None ) –

    Used as an idempotency token, to avoid returning an exception if the service receives the same request twice because of a network error.

  • parameters (Optional[QueryParameterList], default: None ) –

    Use this parameter to include specific query parameters as part of your query definition. Query parameters are supported only for Logs Insights QL queries. Query parameters allow you to use placeholder variables in your query string that are substituted with values at execution time. Use the {{parameterName}} syntax in your query string to reference a parameter.

Raises:

Source code in docs_src/capo_cloudwatch_logs/_services/async_cloud_watch_logs.py
async def put_query_definition(
    self,
    name: "capo_cloudwatch_logs.types.query_definition_name.QueryDefinitionName",
    query_string: "capo_cloudwatch_logs.types.query_definition_string.QueryDefinitionString",
    *,
    config_overrides: Optional[AsyncCloudWatchLogsClientConfig] = None,
    query_language: Optional[
        "capo_cloudwatch_logs.types.query_language.QueryLanguage"
    ] = None,
    query_definition_id: Optional[
        "capo_cloudwatch_logs.types.query_id.QueryId"
    ] = None,
    log_group_names: Optional[
        "capo_cloudwatch_logs.types.log_group_names.LogGroupNames"
    ] = None,
    client_token: Optional[
        "capo_cloudwatch_logs.types.client_token.ClientToken"
    ] = None,
    parameters: Optional[
        "capo_cloudwatch_logs.types.query_parameter_list.QueryParameterList"
    ] = None,
) -> "capo_cloudwatch_logs.types.put_query_definition_response.PutQueryDefinitionResponse":
    """<p>Creates or updates a query definition for CloudWatch Logs Insights. For more information, see <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html">Analyzing Log Data with CloudWatch Logs Insights</a>.</p> <p>To update a query definition, specify its <code>queryDefinitionId</code> in your request. The values of <code>name</code>, <code>queryString</code>, and <code>logGroupNames</code> are changed to the values that you specify in your update operation. No current values are retained from the current query definition. For example, imagine updating a current query definition that includes log groups. If you don't specify the <code>logGroupNames</code> parameter in your update operation, the query definition changes to contain no log groups.</p> <p>You must have the <code>logs:PutQueryDefinition</code> permission to be able to perform this operation.</p>

    Args:
        query_language: <p>Specify the query language to use for this query. The options are Logs Insights QL, OpenSearch PPL, and OpenSearch SQL. For more information about the query languages that CloudWatch Logs supports, see <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_AnalyzeLogData_Languages.html">Supported query languages</a>.</p>
        name: <p>A name for the query definition. If you are saving numerous query definitions, we recommend that you name them. This way, you can find the ones you want by using the first part of the name as a filter in the <code>queryDefinitionNamePrefix</code> parameter of <a href="https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_DescribeQueryDefinitions.html">DescribeQueryDefinitions</a>.</p>
        query_definition_id: <p>If you are updating a query definition, use this parameter to specify the ID of the query definition that you want to update. You can use <a href="https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_DescribeQueryDefinitions.html">DescribeQueryDefinitions</a> to retrieve the IDs of your saved query definitions.</p> <p>If you are creating a query definition, do not specify this parameter. CloudWatch generates a unique ID for the new query definition and include it in the response to this operation.</p>
        log_group_names: <p>Use this parameter to include specific log groups as part of your query definition. If your query uses the OpenSearch Service query language, you specify the log group names inside the <code>querystring</code> instead of here.</p> <p>If you are updating an existing query definition for the Logs Insights QL or OpenSearch Service PPL and you omit this parameter, then the updated definition will contain no log groups.</p>
        query_string: <p>The query string to use for this definition. For more information, see <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html">CloudWatch Logs Insights Query Syntax</a>.</p>
        client_token: <p>Used as an idempotency token, to avoid returning an exception if the service receives the same request twice because of a network error.</p>
        parameters: <p>Use this parameter to include specific query parameters as part of your query definition. Query parameters are supported only for Logs Insights QL queries. Query parameters allow you to use placeholder variables in your query string that are substituted with values at execution time. Use the <code>{{parameterName}}</code> syntax in your query string to reference a parameter.</p>

    Raises:
        capo_cloudwatch_logs.errors.invalid_parameter_exception.InvalidParameterException: <p>A parameter is specified incorrectly.</p>
        capo_cloudwatch_logs.errors.limit_exceeded_exception.LimitExceededException: <p>You have reached the maximum number of resources that can be created.</p>
        capo_cloudwatch_logs.errors.resource_not_found_exception.ResourceNotFoundException: <p>The specified resource does not exist.</p>
        capo_cloudwatch_logs.errors.service_unavailable_exception.ServiceUnavailableException: <p>The service cannot complete the request.</p>
        capo_cloudwatch_logs.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_cloudwatch_logs.types.put_query_definition_request.PutQueryDefinitionRequest]",
    ) -> AsyncOperationResponse[
        "capo_cloudwatch_logs.types.put_query_definition_response.PutQueryDefinitionResponse"
    ]:
        import capo_cloudwatch_logs._operations.logs_20140328.put_query_definition

        (
            output,
            http_response,
        ) = await capo_cloudwatch_logs._operations.logs_20140328.put_query_definition.async_put_query_definition(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("PutQueryDefinition")
    )
    input_: capo_cloudwatch_logs.types.put_query_definition_request.PutQueryDefinitionRequest = {
        "name": name,
        "query_string": query_string,
    }
    if query_language is not None:
        input_["query_language"] = query_language
    if query_definition_id is not None:
        input_["query_definition_id"] = query_definition_id
    if log_group_names is not None:
        input_["log_group_names"] = log_group_names
    if client_token is None:
        client_token = str(uuid.uuid4())
    input_["client_token"] = client_token
    if parameters is not None:
        input_["parameters"] = parameters

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output