在第 3 AWS SDK for PHP 版中搭配 Amazon CloudWatch 警示使用警示動作 - AWS SDK for PHP

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

在第 3 AWS SDK for PHP 版中搭配 Amazon CloudWatch 警示使用警示動作

使用警示動作建立自動停止、終止、重新開機或復原 Amazon EC2 執行個體的警示。當執行個體不再需要執行,您可以使用停止或終止動作。您可以使用重新啟動和恢復動作來自動重新啟動這些執行個體。

下列範例示範如何:

所有的範例程式碼都可以AWS SDK for PHP在這裡取得 GitHub。

登入資料

在執行範例程式碼之前,請依照中的說明設定您的AWS認證憑證。然後匯入AWS SDK for PHP,如中所述基本使用

啟用警示動作

匯入

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

範例程式碼

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();

停用警示動作

匯入

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

範例程式碼

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();