

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 設定遊戲工作階段放置的事件通知
<a name="queue-notification"></a>

您可以使用事件通知來監控個別置放請求的狀態。我們建議為具有大量置放活動的所有遊戲設定事件通知。

設定事件通知有兩種選項。
+ 使用佇列將事件通知Amazon GameLift Servers發佈至 Amazon Simple Notification Service (Amazon SNS) 主題。
+ 使用自動發佈的 Amazon EventBridge 事件及其工具套件來管理事件。

如需 發出的遊戲工作階段置放事件清單Amazon GameLift Servers，請參閱 [遊戲工作階段置放事件](queue-events.md)。

**重要**  
對於大量置放系統，我們建議使用標準 （非 FIFO) Amazon SNS 主題，而不是 FIFO 主題。FIFO 主題的發佈限制低於標準主題，這可能會導致在高負載期間調節例外狀況。如果您使用 FIFO 主題遇到限流，可能會遺失佇列置放通知。

## 設定 SNS 主題
<a name="queue-notification-sns"></a>

若要Amazon GameLift Servers讓 將遊戲工作階段佇列產生的所有事件發佈至主題，請將通知目標欄位設定為主題。

**設定 Amazon GameLift Servers 事件通知用的 SNS 主題**

1. 登入 AWS 管理主控台 ，並在 [https://console.aws.amazon.com/sns/v3/home](https://console.aws.amazon.com/sns/v3/home)：// 開啟 Amazon SNS 主控台。

1. 在 SNS **主題**頁面中，選擇**建立主題**，然後依照指示建立您的主題。

1. 在**存取政策**下，執行下列動作：

   1. 選擇**進階**方法。

   1. 將 JSON 物件的下列粗體區段新增至現有政策。

------
#### [ JSON ]

****  

      ```
      {
        "Version":"2012-10-17",		 	 	 
        "Id": "__default_policy_ID",
        "Statement": [
          {
            "Sid": "__default_statement_ID",
            "Effect": "Allow",
            "Principal": {
              "AWS": "*"
            },
            "Action": [
              "SNS:GetTopicAttributes",
              "SNS:SetTopicAttributes",
              "SNS:AddPermission",
              "SNS:RemovePermission",
              "SNS:DeleteTopic",
              "SNS:Subscribe",
              "SNS:ListSubscriptionsByTopic",
              "SNS:Publish"
            ],
            "Resource": "arn:aws:sns:us-east-1:111122223333:your_topic_name",
            "Condition": {
              "StringEquals": {
                "AWS:SourceAccount": "your_account"
              }
            }
          },
          {
            "Sid": "__console_pub_0",
            "Effect": "Allow",
            "Principal": { 
              "Service": "gamelift.amazonaws.com" 
            },
            "Action": "sns:Publish",
            "Resource": "arn:aws:sns:us-east-1:111122223333:your_topic_name",
            "Condition": {
              "ArnLike": {
              "aws:SourceArn": "arn:aws:gamelift:us-east-1:111122223333:gamesessionqueue/your_queue_name"
              }
            }
          }
        ]
      }
      ```

------

   1. （選用） 透過將條件新增至資源政策，將其他存取控制新增至主題。

1. 請選擇**建立主題**。

1. 建立 SNS 主題之後，請在建立佇列期間將其新增至佇列，或編輯現有的佇列以新增該主題。

## 使用伺服器端加密設定 SNS 主題
<a name="queue-notification-sns-sse"></a>

使用伺服器端加密 (SSE) 可讓您儲存加密主題中的敏感資料。SSE 使用 AWS Key Management Service (AWS KMS) 中管理的金鑰來保護 Amazon SNS 主題中的訊息內容。如需使用 Amazon SNS 進行伺服器端加密的詳細資訊，請參閱《*Amazon Simple Notification Service 開發人員指南*》中的[靜態加密](https://docs.aws.amazon.com/sns/latest/dg/sns-server-side-encryption.html)。

若要使用伺服器端加密設定 SNS 主題，請檢閱下列主題：
+ 《 *AWS Key Management Service 開發人員指南*》中的[建立金鑰](https://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html) 
+ 《*Amazon Simple Notification Service 開發人員指南*》中的[為主題啟用 SSE](https://docs.aws.amazon.com/sns/latest/dg/sns-enable-encryption-for-topic.html) 

建立 KMS 金鑰時，請使用下列 KMS 金鑰政策：

```
{ 
    "Effect": "Allow", 
    "Principal": { 
        "Service": "gamelift.amazonaws.com" 
     },
    "Action": [
        "kms:Decrypt",
        "kms:GenerateDataKey"
    ],
    "Resource": "*",
    "Condition": {
        "ArnLike": { 
            "aws:SourceArn": "arn:aws:gamelift:your_region:your_account:gamesessionqueue/your_queue_name" 
        },
        "StringEquals": { 
            "kms:EncryptionContext:aws:sns:topicArn": "arn:aws:sns:your_region:your_account:your_sns_topic_name" 
        }
    }
}
```

## 設定 EventBridge
<a name="queue-notification-cwe"></a>

Amazon GameLift Servers 會自動將所有遊戲工作階段置放事件發佈至 EventBridge。使用 EventBridge，您可以設定規則，將事件路由到目標進行處理。例如，您可以設定規則，將事件路由`PlacementFulfilled`到 AWS Lambda 函數，處理連線到遊戲工作階段之前的任務。如需 EventBridge 的詳細資訊，請參閱*《Amazon EventBridge 使用者指南》*中的[什麼是 Amazon EventBridge？](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html)。

以下是要與Amazon GameLift Servers佇列搭配使用的 EventBridge 規則範例：

比對所有Amazon GameLift Servers佇列中的事件

```
{
    "source": [
        "aws.gamelift"
    ],
    "detail-type": [
        "GameLift Queue Placement Event"
    ]
}
```

符合來自特定佇列的事件

```
{
    "source": [
        "aws.gamelift"
    ],
    "detail-type": [
        "GameLift Queue Placement Event"
    ],
    "resources": [
        "arn:aws:gamelift:your_region:your_account:gamesessionqueue/your_queue_name"
    ]
}
```

## 使用 進行參考實作 AWS CDK
<a name="queue-notification-toolkit"></a>

如需使用 Amazon SNS AWS Lambda和 Amazon DynamoDB 進行事件型遊戲工作階段置放的完整參考實作，請參閱 Amazon GameLift Toolkit 中的[事件型遊戲工作階段置放指南](https://github.com/amazon-gamelift/amazon-gamelift-toolkit/tree/main/event-based-session-placement)。本指南包含部署完整事件驅動遊戲工作階段置放系統的 AWS CDK 範本，其中包含處理遊戲工作階段置放事件的 AWS Lambda 函數、用於追蹤遊戲工作階段置放狀態的 Amazon DynamoDB 資料表，以及Amazon GameLift Servers佇列組態的最佳實務。