

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 untuk PHP Versi 3
<a name="cw-examples-using-alarm-actions"></a>

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:
+ Aktifkan tindakan untuk alarm tertentu menggunakan [EnableAlarmActions](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-monitoring-2010-08-01.html#enablealarmactions).
+ Nonaktifkan tindakan untuk alarm tertentu menggunakan [DisableAlarmActions](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-monitoring-2010-08-01.html#disablealarmactions).

Semua kode contoh untuk AWS SDK untuk PHP tersedia [di sini GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code).

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

Sebelum menjalankan kode contoh, konfigurasikan AWS kredenal Anda, seperti yang dijelaskan dalam. [Mengautentikasi dengan AWS menggunakan AWS SDK untuk PHP Versi 3](credentials.md) Kemudian impor AWS SDK untuk PHP, seperti yang dijelaskan dalam[Menginstal AWS SDK untuk PHP Versi 3](getting-started_installation.md).

## Mengaktifkan tindakan alarm
<a name="enable-alarm-actions"></a>

 **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
<a name="disable-alarm-actions"></a>

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