Menggunakan tindakan alarm dengan CloudWatch alarm Amazon dengan AWS SDK for PHP Versi 3 - AWS SDK for PHP

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Menggunakan tindakan alarm dengan CloudWatch alarm Amazon dengan AWS SDK for PHP Versi 3

Gunakan tindakan alarm untuk membuat alarm yang secara otomatis menghentikan, menghentikan, reboot, atau memulihkan instans Amazon EC2 Anda. Anda dapat menggunakan tindakan berhenti atau menghentikan saat Anda tidak lagi membutuhkan instance untuk dijalankan. Anda dapat menggunakan tindakan reboot dan memulihkan untuk secara otomatis me-reboot instance tersebut.

Contoh berikut menunjukkan cara:

Semua kode contoh untuk AWS SDK for PHP tersedia di sini GitHub.

Kredensial

Sebelum menjalankan kode contoh, konfigurasikan AWS kredenal Anda, seperti yang dijelaskan dalam. Kredensial Kemudian imporAWS SDK for PHP, seperti yang dijelaskan dalamPenggunaan dasar.

Mengaktifkan tindakan alarm

Impor

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

Kode Sampel

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

Menonaktifkan tindakan alarm

Impor

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

Kode Sampel

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