Using alarm actions with Amazon CloudWatch alarms with Amazon SDK for PHP Version 3 - Amazon SDK for PHP
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Using alarm actions with Amazon CloudWatch alarms with Amazon SDK for PHP Version 3

Use alarm actions to create alarms that automatically stop, terminate, reboot, or recover your Amazon EC2 instances. You can use the stop or terminate actions when you no longer need an instance to be running. You can use the reboot and recover actions to automatically reboot those instances.

The following examples show how to:

All the example code for the Amazon SDK for PHP is available here on GitHub.

Credentials

Before running the example code, configure your Amazon credentials, as described in Credentials. Then import the Amazon SDK for PHP, as described in Basic usage.

Enable alarm actions

Imports

require 'vendor/autoload.php'; use Aws\CloudWatch\CloudWatchClient; use Aws\Exception\AwsException;

Sample Code

function enableAlarmActions($cloudWatchClient, $alarmNames) { try { $result = $cloudWatchClient->enableAlarmActions([ 'AlarmNames' => $alarmNames ]); if (isset($result['@metadata']['effectiveUri'])) { return 'At the effective URI of ' . $result['@metadata']['effectiveUri'] . ', actions for any matching alarms have been enabled.'; } else { return'Actions for some matching alarms ' . 'might not have been enabled.'; } } catch (AwsException $e) { return 'Error: ' . $e->getAwsErrorMessage(); } } function enableTheAlarmActions() { $alarmNames = array('my-alarm'); $cloudWatchClient = new CloudWatchClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-08-01' ]); echo enableAlarmActions($cloudWatchClient, $alarmNames); } // Uncomment the following line to run this code in an AWS account. // enableTheAlarmActions();

Disable alarm actions

Imports

require 'vendor/autoload.php'; use Aws\CloudWatch\CloudWatchClient; use Aws\Exception\AwsException;

Sample Code

function disableAlarmActions($cloudWatchClient, $alarmNames) { try { $result = $cloudWatchClient->disableAlarmActions([ 'AlarmNames' => $alarmNames ]); if (isset($result['@metadata']['effectiveUri'])) { return 'At the effective URI of ' . $result['@metadata']['effectiveUri'] . ', actions for any matching alarms have been disabled.'; } else { return 'Actions for some matching alarms ' . 'might not have been disabled.'; } } catch (AwsException $e) { return 'Error: ' . $e->getAwsErrorMessage(); } } function disableTheAlarmActions() { $alarmNames = array('my-alarm'); $cloudWatchClient = new CloudWatchClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-08-01' ]); echo disableAlarmActions($cloudWatchClient, $alarmNames); } // Uncomment the following line to run this code in an AWS account. // disableTheAlarmActions();