Usar ações de alarmes com alarmes do Amazon CloudWatch com o AWS SDK para PHP versão 3
Use ações de alarmes para criar alarmes que param, encerram, reiniciam ou recuperam suas instâncias do Amazon EC2. Use as ações parar ou encerrar quando não for mais necessário que uma instância seja executada. Use as ações reiniciar e recuperar para reiniciar essas instâncias automaticamente.
Os exemplos a seguir mostram como:
-
Habilitar ações para alarmes especificados usando EnableAlarmActions.
-
Desabilitar ações para alarmes especificados usando DisableAlarmActions.
O código de exemplo completo do AWS SDK para PHP está disponível aqui no GitHub
Credenciais
Antes de executar o código de exemplo, configure suas credenciais da AWS, conforme descrito em Autenticar com a AWS usando o AWS SDK para PHP versão 3. Em seguida, importe o AWS SDK para PHP, conforme descrito em Instalar o AWS SDK para PHP versão 3.
Habilitar ações de alarme
Importações
require 'vendor/autoload.php'; use Aws\CloudWatch\CloudWatchClient; use Aws\Exception\AwsException;
Código de exemplo
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();
Desabilitar ações de alarme
Importações
require 'vendor/autoload.php'; use Aws\CloudWatch\CloudWatchClient; use Aws\Exception\AwsException;
Código de exemplo
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();