SetAlarmState.php - AWS Code Sample

SetAlarmState.php

SetAlarmState.php demonstrates how to set the state of an alarm in Amazon CloudWatch.

<?php /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ require 'vendor/autoload.php'; use Aws\CloudWatch\CloudWatchClient; use Aws\Exception\AwsException; /* //////////////////////////////////////////////////////////////////////////// * Purpose: Sets the state of the specified alarm in Amazon CloudWatch. * * Prerequisites: At least one CloudWatch alarm. * * Inputs: * - $cloudWatchClient: An initialized CloudWatch client. * - $cloudWatchRegion: The alarm's AWS Region. * - $alarmName: The alarm's name. * - $stateValue: The alarm's new state. * - $stateReason: The reason for the alarm. * * Returns: Information about the alarm's state change request; * otherwise, the error message. * ///////////////////////////////////////////////////////////////////////// */ function setAlarmState($cloudWatchClient, $cloudWatchRegion, $alarmName, $stateValue, $stateReason) { try { $result = $cloudWatchClient->setAlarmState([ 'AlarmName' => $alarmName, 'StateValue' => $stateValue, 'StateReason' => $stateReason ]); if (isset($result['@metadata']['effectiveUri'])) { if ($result['@metadata']['effectiveUri'] == 'https://monitoring.' . $cloudWatchRegion . '.amazonaws.com') { return 'Successfully changed state of specified alarm.'; } else { return 'Could not change state of specified alarm.'; } } else { return 'Could not change state of specified alarm.'; } } catch (AwsException $e) { return 'Error: ' . $e->getAwsErrorMessage(); } } function setTheAlarmState() { $alarmName = 'my-ec2-resources'; $stateValue = 'OK'; $stateReason = 'AWS SDK for PHP example code set the state of the alarm ' . $alarmName . ' to ' . $stateValue; $cloudWatchRegion = 'us-east-1'; $cloudWatchClient = new CloudWatchClient([ 'profile' => 'default', 'region' => $cloudWatchRegion, 'version' => '2010-08-01' ]); echo setAlarmState($cloudWatchClient, $cloudWatchRegion, $alarmName, $stateValue, $stateReason); } // Uncomment the following line to run this code in an AWS account. // setTheAlarmState();

Sample Details

Service: cloudwatch

Last tested: 2020-05-08

Author: pccornel (AWS)

Type: full-example