翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
AWS SDK for PHP バージョン 3 での Amazon アラームでの CloudWatch アラームアクションの使用
アラームアクションを使用して、Amazon EC2 インスタンスを自動的に停止、終了、再起動、または復旧するアラームを作成します。今後インスタンスを実行する必要がなくなったときに、停止または終了アクションを使用できます。再起動と復元アクションを使用して、自動的にそのインスタンスを再起動できます。
以下の例では、次の方法を示しています。
-
を使用して、指定したアラームのアクションを有効にしますEnableAlarmActions。
-
を使用して、指定したアラームのアクションを無効にしますDisableAlarmActions。
のすべてのサンプルコード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();