

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# AWS SDK for PHP 버전 3을 사용하여 Amazon CloudWatch Events로 이벤트 전송
<a name="cw-examples-sending-events"></a>

CloudWatch Events는 다양한 대상으로의 Amazon Web Services(AWS) 리소스 변경 사항을 설명하는 시스템 이벤트의 스트림을 거의 실시간으로 전달합니다. 단순한 규칙을 사용하여 이벤트를 하나 이상의 대상 함수 또는 스트림에 일치시키고 라우팅할 수 있습니다.

다음 예제에서는 다음과 같은 작업을 하는 방법을 보여줍니다.
+ [PutRule](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-events-2015-10-07.html#putrule)을 사용하여 규칙을 생성합니다.
+ [PutTargets](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-events-2015-10-07.html#puttargets)을 사용하여 규칙에 대상을 추가합니다.
+ [PutEvents](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-events-2015-10-07.html#putevents)를 사용하여 CloudWatch 이벤트에 사용자 지정 이벤트를 전송할 수 있습니다.

에 대한 모든 예제 코드는 GitHub에서 AWS SDK for PHP 확인할 수 있습니다. [ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)

## 자격 증명
<a name="examplecredentials"></a>

예제 코드를 실행하기 전에에 설명된 대로 AWS 자격 증명을 구성합니다[AWS SDK for PHP 버전 3을 AWS 사용하여 로 인증](credentials.md). 그런 다음 AWS SDK for PHP에 설명된 대로를 가져옵니다[AWS SDK for PHP 버전 3 설치](getting-started_installation.md).

## 규칙 생성
<a name="create-a-rule"></a>

 **가져오기** 

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

use Aws\Exception\AwsException;
```

 **샘플 코드** 

```
$client = new Aws\cloudwatchevents\cloudwatcheventsClient([
    'profile' => 'default',
    'region' => 'us-west-2',
    'version' => '2015-10-07'
]);

try {
    $result = $client->putRule([
        'Name' => 'DEMO_EVENT', // REQUIRED
        'RoleArn' => 'IAM_ROLE_ARN',
        'ScheduleExpression' => 'rate(5 minutes)',
        'State' => 'ENABLED',
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```

## 규칙에 대상 추가
<a name="add-targets-to-a-rule"></a>

 **가져오기** 

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

use Aws\Exception\AwsException;
```

 **샘플 코드** 

```
$client = new Aws\cloudwatchevents\cloudwatcheventsClient([
    'profile' => 'default',
    'region' => 'us-west-2',
    'version' => '2015-10-07'
]);

try {
    $result = $client->putTargets([
        'Rule' => 'DEMO_EVENT', // REQUIRED
        'Targets' => [ // REQUIRED
            [
                'Arn' => 'LAMBDA_FUNCTION_ARN', // REQUIRED
                'Id' => 'myCloudWatchEventsTarget' // REQUIRED
            ],
        ],
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```

## 사용자 지정 이벤트 전송
<a name="send-custom-events"></a>

 **가져오기** 

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

use Aws\Exception\AwsException;
```

 **샘플 코드** 

```
$client = new Aws\cloudwatchevents\cloudwatcheventsClient([
    'profile' => 'default',
    'region' => 'us-west-2',
    'version' => '2015-10-07'
]);

try {
    $result = $client->putEvents([
        'Entries' => [ // REQUIRED
            [
                'Detail' => '<string>',
                'DetailType' => '<string>',
                'Resources' => ['<string>'],
                'Source' => '<string>',
                'Time' => time()
            ],
        ],
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    error_log($e->getMessage());
}
```