AWS SDK for PHP
Developer Guide

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 Version 3 is available here on GitHub.

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials for the AWS SDK for PHP Version 3. Then import the AWS SDK for PHP, as described in Basic Usage Patterns of the AWS SDK for PHP Version 3.

Enable Alarm Actions

Imports

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

Sample Code

$alarmName = "<ALARM_NAME>"; $client = new CloudWatchClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-08-01' ]); try { $result = $client->enableAlarmActions([ 'AlarmNames' => array($alarmName) //REQUIRED ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Disable Alarm Actions

Imports

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

Sample Code

$alarmName = "<ALARM_NAME>"; $client = new CloudWatchClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-08-01' ]); try { $result = $client->disableAlarmActions([ 'AlarmNames' => array($alarmName) //REQUIRED ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }