

# EventBridge를 사용하여 Athena 쿼리 이벤트 모니터링
<a name="athena-events"></a>

Amazon EventBridge와 함께 Amazon Athena를 사용하여 쿼리 상태에 대한 실시간 알림을 받을 수 있습니다. 사용자가 제출한 쿼리의 상태가 전환되면 Athena는 해당 쿼리 상태 전환에 대한 정보를 포함하는 이벤트를 EventBridge에 게시합니다. 원하는 이벤트에 대한 간단한 규칙을 작성하고 규칙과 일치하는 이벤트 발생 시 자동으로 작업을 수행하도록 할 수 있습니다. 예를 들어 쿼리가 종료 상태에 도달할 때 AWS Lambda 함수를 호출하는 규칙을 만들 수 있습니다. 이벤트는 최선의 작업을 기반으로 발생됩니다.

Athena에 대한 이벤트 규칙을 생성하기 전에 다음을 수행해야 합니다.
+ Eventbridge의 이벤트, 규칙, 대상을 숙지해야 합니다. 자세한 내용은 [Amazon EventBridge란 무엇입니까?](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html)를 참조하십시오. 규칙을 설정하는 방법에 대한 자세한 내용은 [Amazon EventBridge 시작하기](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-get-started.html)를 참조하세요.
+ 이벤트 규칙에 사용할 대상을 만듭니다.

**참고**  
Athena는 현재 한 가지 유형의 이벤트인 Athena 쿼리 상태 변경을 제공하지만 다른 이벤트 유형 및 세부 정보를 추가할 수도 있습니다. 프로그래밍 방식으로 이벤트 JSON 데이터를 역직렬화하는 경우, 추가 속성이 추가되었을 때 알 수 없는 속성을 처리할 수 있도록 애플리케이션이 준비되어야 합니다.

## Athena 이벤트 형식
<a name="athena-events-pattern"></a>

다음은 Amazon Athena 이벤트의 기본 패턴입니다.

```
{
    "source":[
        "aws.athena"
    ],
    "detail-type":[
        "Athena Query State Change"
    ],
    "detail":{
        "currentState":[
            "SUCCEEDED"
        ]
    }
}
```

## Athena 쿼리 상태 변경 이벤트
<a name="athena-events-athena-query-state-change"></a>

다음 예제에서는 `currentState` 값이 `SUCCEEDED`인 Athena 쿼리 상태 변경 이벤트를 보여 줍니다.

```
{
    "version":"0",
    "id":"abcdef00-1234-5678-9abc-def012345678",
    "detail-type":"Athena Query State Change",
    "source":"aws.athena",
    "account":"123456789012",
    "time":"2019-10-06T09:30:10Z",
    "region":"us-east-1",
    "resources":[

    ],
    "detail":{
        "versionId":"0",
        "currentState":"SUCCEEDED",
        "previousState":"RUNNING",
        "statementType":"DDL",
        "queryExecutionId":"01234567-0123-0123-0123-012345678901",
        "workgroupName":"primary",
        "sequenceNumber":"3"
    }
}
```

다음 예제에서는 `currentState` 값이 `FAILED`인 Athena 쿼리 상태 변경 이벤트를 보여 줍니다. `athenaError` 블록은 `currentState`가 `FAILED`인 경우에만 나타납니다. `errorCategory` 및 `errorType`에 대한 값 관련 내용은 [Athena 오류 카탈로그](error-reference.md) 단원을 참조하세요.

```
{
    "version":"0",
    "id":"abcdef00-1234-5678-9abc-def012345678",
    "detail-type":"Athena Query State Change",
    "source":"aws.athena",
    "account":"123456789012",
    "time":"2019-10-06T09:30:10Z",
    "region":"us-east-1",
    "resources":[ 
    ],
    "detail":{
        "athenaError": {
            "errorCategory": 2.0, //Value depends on nature of exception
            "errorType": 1306.0, //Type depends on nature of exception
            "errorMessage": "Amazon S3 bucket not found", //Message depends on nature of exception
            "retryable":false //Retryable value depends on nature of exception
        },
        "versionId":"0",
        "currentState": "FAILED",
        "previousState": "RUNNING",
        "statementType":"DML",
        "queryExecutionId":"01234567-0123-0123-0123-012345678901",
        "workgroupName":"primary",
        "sequenceNumber":"3"
    }
}
```

### 출력 속성
<a name="athena-events-query-state-change-output-properties"></a>

JSON 출력에는 다음 속성이 포함됩니다.


****  

| 속성 | 설명 | 
| --- | --- | 
| athenaError | currentState가 FAILED인 경우에만 나타납니다. 오류 범주, 오류 유형, 오류 메시지, 오류를 일으킨 작업을 재시도할 수 있는지 여부 등 발생한 오류에 대한 정보가 포함되어 있습니다. 이러한 각 필드의 값은 오류의 특성에 따라 달라집니다. errorCategory 및 errorType에 대한 값 관련 내용은 [Athena 오류 카탈로그](error-reference.md) 단원을 참조하세요. | 
| versionId | 세부 정보 객체 스키마의 버전 번호입니다. | 
| currentState | 이벤트 발생 시 쿼리가 다른 상태로 전환됩니다. | 
| previousState | 이벤트 발생 시 쿼리가 다른 상태에서 전환됩니다. | 
| statementType | 실행된 쿼리 문의 유형입니다. | 
| queryExecutionId | 실행된 쿼리에 대한 고유 식별자입니다. | 
| workgroupName | 쿼리가 실행된 작업 그룹의 이름입니다. | 
| sequenceNumber | 실행된 단일 쿼리와 관련된 수신 이벤트의 중복 제거 및 순서 지정을 수행할 수 있는 단순 증가 수치입니다. 동일한 상태 전환에 대해 중복 이벤트가 게시되면 sequenceNumber 값은 동일합니다. 드물지만 다시 대기열에 올리는 쿼리와 같이 쿼리에 상태 전환이 두 번 이상 발생하는 경우 sequenceNumber를 사용하여 동일한 currentState 및 previousState 값을 가진 이벤트의 순서를 지정할 수 있습니다. | 

## 예제
<a name="athena-events-examples"></a>

다음 예제에서는 구독한 Amazon SNS 주제에 이벤트를 게시합니다. Athena가 쿼리되면 이메일을 받게 됩니다. 이 예제에서는 Amazon SNS 주제가 존재하고 사용자가 이를 구독했다고 가정합니다.

**Amazon SNS 주제에 Athena 이벤트를 게시하려면**

1. Amazon SNS 주제의 대상을 만듭니다. 다음 예제와 같이 Amazon SNS 주제에 게시할 수 있는 `events.amazonaws.com` 권한을 EventBridge 이벤트 서비스 보안 주체에 부여합니다.

   ```
   {
       "Effect":"Allow",
       "Principal":{
           "Service":"events.amazonaws.com"
       },
       "Action":"sns:Publish",
       "Resource":"arn:aws:sns:us-east-1:111111111111:your-sns-topic"
   }
   ```

1. 다음 예제와 같이 AWS CLI `events put-rule` 명령을 사용하여 Athena 이벤트에 대한 규칙을 만듭니다.

   ```
   aws events put-rule --name {ruleName} --event-pattern '{"source": ["aws.athena"]}'
   ```

1. 다음 예제와 같이 AWS CLI `events put-targets` 명령을 사용하여 Amazon SNS 주제 대상을 규칙에 연결합니다.

   ```
   aws events put-targets --rule {ruleName} --targets Id=1,Arn=arn:aws:sns:us-east-1:111111111111:your-sns-topic
   ```

1. Athena를 쿼리하고 호출되는 대상을 관찰합니다. Amazon SNS 주제에서 해당 이메일을 수신해야 합니다.

## Amazon Athena에서 AWS User Notifications 사용
<a name="monitoring-user-notifications"></a>

[AWS User Notifications](https://docs.aws.amazon.com/notifications/latest/userguide/what-is.html)을 사용하여 Amazon Athena 이벤트에 대한 알림을 받을 전송 채널을 설정할 수 있습니다. 이벤트가 지정한 규칙과 일치하면 알림을 받습니다. 이메일, [채팅 애플리케이션의 Amazon Q Developer](https://docs.aws.amazon.com/chatbot/latest/adminguide/what-is.html) 채팅 알림 또는 [AWS Console Mobile Application](https://docs.aws.amazon.com/consolemobileapp/latest/userguide/what-is-consolemobileapp.html) 푸시 알림을 비롯한 여러 채널을 통해 이벤트에 대한 알림을 받을 수 있습니다. [콘솔 알림 센터](https://console.aws.amazon.com/notifications/)에서도 알림을 볼 수 있습니다. 사용자 알림은 집계를 지원하므로 특정 이벤트 중에 받는 알림 수를 줄일 수 있습니다.

자세한 내용은 [https://docs.aws.amazon.com/notifications/latest/userguide/what-is.html](https://docs.aws.amazon.com/notifications/latest/userguide/what-is.html)를 참조하세요.