Skip to content

set_alarm_state

set_alarm_state(
    *,
    config_overrides: Optional[CloudWatchClientConfig] = None,
    alarm_name: Optional[AlarmName] = None,
    state_value: Optional[StateValue] = None,
    state_reason: Optional[StateReason] = None,
    state_reason_data: Optional[StateReasonData] = None,
) -> None

Temporarily sets the state of an alarm for testing purposes. When the updated state differs from the previous value, the action configured for the appropriate state is invoked. For example, if your alarm is configured to send an Amazon SNS message when an alarm is triggered, temporarily changing the alarm state to ALARM sends an SNS message.

Metric alarms returns to their actual state quickly, often within seconds. Because the metric alarm state change happens quickly, it is typically only visible in the alarm's History tab in the Amazon CloudWatch console or through DescribeAlarmHistory.

If you use SetAlarmState on a composite alarm, the composite alarm is not guaranteed to return to its actual state. It returns to its actual state only once any of its children alarms change state. It is also reevaluated if you update its configuration.

If an alarm triggers EC2 Auto Scaling policies or application Auto Scaling policies, you must include information in the StateReasonData parameter to enable the policy to take the correct action.

Parameters:

  • alarm_name (Optional[AlarmName], default: None ) –

    The name of the alarm.

  • state_value (Optional[StateValue], default: None ) –

    The value of the state.

  • state_reason (Optional[StateReason], default: None ) –

    The reason that this alarm is set to this specific state, in text format.

  • state_reason_data (Optional[StateReasonData], default: None ) –

    The reason that this alarm is set to this specific state, in JSON format.

    For SNS or EC2 alarm actions, this is just informational. But for EC2 Auto Scaling or application Auto Scaling alarm actions, the Auto Scaling policy uses the information in this field to take the correct action.

Raises:

Source code in docs_src/capo_cloudwatch/_services/cloud_watch.py
def set_alarm_state(
    self,
    *,
    config_overrides: Optional[CloudWatchClientConfig] = None,
    alarm_name: Optional["capo_cloudwatch.types.alarm_name.AlarmName"] = None,
    state_value: Optional["capo_cloudwatch.types.state_value.StateValue"] = None,
    state_reason: Optional["capo_cloudwatch.types.state_reason.StateReason"] = None,
    state_reason_data: Optional[
        "capo_cloudwatch.types.state_reason_data.StateReasonData"
    ] = None,
) -> None:
    """<p>Temporarily sets the state of an alarm for testing purposes. When the updated state differs from the previous value, the action configured for the appropriate state is invoked. For example, if your alarm is configured to send an Amazon SNS message when an alarm is triggered, temporarily changing the alarm state to <code>ALARM</code> sends an SNS message.</p> <p>Metric alarms returns to their actual state quickly, often within seconds. Because the metric alarm state change happens quickly, it is typically only visible in the alarm's <b>History</b> tab in the Amazon CloudWatch console or through <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_DescribeAlarmHistory.html">DescribeAlarmHistory</a>.</p> <p>If you use <code>SetAlarmState</code> on a composite alarm, the composite alarm is not guaranteed to return to its actual state. It returns to its actual state only once any of its children alarms change state. It is also reevaluated if you update its configuration.</p> <p>If an alarm triggers EC2 Auto Scaling policies or application Auto Scaling policies, you must include information in the <code>StateReasonData</code> parameter to enable the policy to take the correct action.</p>

    Args:
        alarm_name: <p>The name of the alarm.</p>
        state_value: <p>The value of the state.</p>
        state_reason: <p>The reason that this alarm is set to this specific state, in text format.</p>
        state_reason_data: <p>The reason that this alarm is set to this specific state, in JSON format.</p> <p>For SNS or EC2 alarm actions, this is just informational. But for EC2 Auto Scaling or application Auto Scaling alarm actions, the Auto Scaling policy uses the information in this field to take the correct action.</p>

    Raises:
        capo_cloudwatch.errors.invalid_format_fault.InvalidFormatFault: <p>Data was not syntactically valid JSON.</p>
        capo_cloudwatch.errors.resource_not_found.ResourceNotFound: <p>The named resource does not exist.</p>
        capo_cloudwatch.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    def _handler(
        req: "OperationRequest[capo_cloudwatch.types.set_alarm_state_input.SetAlarmStateInput]",
    ) -> OperationResponse[None]:
        import capo_cloudwatch._operations.granite_service_version20100801.set_alarm_state

        output, http_response = (
            capo_cloudwatch._operations.granite_service_version20100801.set_alarm_state.set_alarm_state(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("SetAlarmState")
    )
    input_: capo_cloudwatch.types.set_alarm_state_input.SetAlarmStateInput = {}
    if alarm_name is not None:
        input_["alarm_name"] = alarm_name
    if state_value is not None:
        input_["state_value"] = state_value
    if state_reason is not None:
        input_["state_reason"] = state_reason
    if state_reason_data is not None:
        input_["state_reason_data"] = state_reason_data

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output