

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

# Uso de acciones de alarma con las alarmas de Amazon CloudWatch con AWS SDK para PHP versión 3
<a name="cw-examples-using-alarm-actions"></a>

Use acciones de alarma para crear alarmas que detengan, terminen, reinicien o recuperen automáticamente sus instancias de Amazon EC2. Puede utilizar las acciones parar o terminar cuando ya no necesita que se ejecute una instancia. Puede usar las acciones reiniciar y recuperar para reiniciar automáticamente esas instancias.

Los siguientes ejemplos muestran cómo:
+ Habilitar acciones para alarmas específicas utilizando [EnableAlarmActions](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-monitoring-2010-08-01.html#enablealarmactions).
+ Deshabilitar acciones para alarmas específicas utilizando [DisableAlarmActions](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-monitoring-2010-08-01.html#disablealarmactions).

Todo el código de ejemplo de AWS SDK para PHP está disponible [aquí en GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code).

## Credenciales
<a name="examplecredentials"></a>

Antes de ejecutar el código de ejemplo, configure sus credenciales de AWS, como se indica en [Autenticación AWS con la AWS SDK para PHP versión 3](credentials.md). A continuación, importe AWS SDK para PHP, como se indica en [Instalación de la AWS SDK para PHP versión 3](getting-started_installation.md).

## Habilitación de acciones de alarma
<a name="enable-alarm-actions"></a>

 **Importaciones** 

```
require 'vendor/autoload.php';

use Aws\CloudWatch\CloudWatchClient;
use Aws\Exception\AwsException;
```

 **Código de muestra** 

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

## Deshabilitación de acciones de alarma
<a name="disable-alarm-actions"></a>

 **Importaciones** 

```
require 'vendor/autoload.php';

use Aws\CloudWatch\CloudWatchClient;
use Aws\Exception\AwsException;
```

 **Código de muestra** 

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