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

Using alarm actions with Amazon CloudWatch alarms with AWS 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 AWS SDK for PHP is available here on GitHub.

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials. Then import the AWS 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();