

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

# 設定FlexMatch事件通知
<a name="match-notification"></a>

您可以使用事件通知來追蹤個別配對請求的狀態。生產中或生產前具有大量配對活動的所有遊戲都應使用事件通知。

設定事件通知有兩種選項。
+ 讓您的配對建構器將事件通知發佈至 Amazon Simple Notification Service (Amazon SNS) 主題。
+ 使用自動發佈的 Amazon EventBridge 事件及其工具套件來管理事件。

如需 Amazon GameLift Servers 發出的FlexMatch事件清單，請參閱 [FlexMatch 配對事件](match-events.md)。

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

**注意**  
 Amazon GameLift Servers 會使用內建的重試邏輯自動處理 Amazon SNS 交付失敗和限流。當 Amazon SNS 傳回限流錯誤或暫時失敗時， Amazon GameLift Servers 會在嘗試之間以漸進式延遲重試通知交付。這有助於確保可靠地傳送事件通知。不過，如果在所有重試嘗試後仍持續失敗，或是授權失敗或遺失主題等無法重試的錯誤，則通知可能會遺失。

**Topics**
+ [設定 EventBridge 事件](#match-notification-cwe)
+ [教學課程：設定 Amazon SNS 主題](match-notification-sns.md)
+ [使用伺服器端加密設定 SNS 主題](queue-notification-sns-sse.md)
+ [設定主題訂閱以叫用 Lambda 函數](match-notification-lambda.md)

## 設定 EventBridge 事件
<a name="match-notification-cwe"></a>

Amazon GameLift Servers 會自動將所有配對事件發佈至 Amazon EventBridge。使用 EventBridge，您可以設定規則，將配對事件路由到目標進行處理。例如，您可以設定規則，將事件 "PotentialMatchCreated" 路由到處理玩家接受度的 AWS Lambda 函數。如需詳細資訊，請參閱[什麼是 Amazon EventBridge？](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html)

**注意**  
當您設定配對建構器時，請保持通知目標欄位空白，或者如果您想要同時使用 EventBridge 和 Amazon SNS 主題。

# 教學課程：設定 Amazon SNS 主題
<a name="match-notification-sns"></a>

您可以讓 Amazon GameLift Servers 將FlexMatch配對建構器產生的所有事件發佈至 Amazon SNS 主題。

**建立Amazon GameLift Servers事件通知的 SNS 主題**

1. 開啟 [Amazon SNS 主控台](https://console.aws.amazon.com/sns)。

1. 在導覽窗格中，選擇 **Topics** (主題)。

1. 在 **Topics** (主題) 頁面上，選擇 **Create topic** (建立主題)。

1. 在 主控台中建立主題。如需詳細資訊，請參閱[《Amazon Simple Notification Service 開發人員指南》中的使用 建立主題 AWS 管理主控台](https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html#create-topic-aws-console)。 **

1. 在主題**的詳細資訊**頁面上，選擇**編輯**。

1. （選用） 在主題的**編輯**頁面上，展開**存取政策**，然後從下列 AWS Identity and Access Management (IAM) 政策陳述式將粗體語法新增至現有政策的結尾。（為了清楚起見，此處會顯示整個政策。) 請務必將 Amazon Resource Name (ARN) 詳細資訊用於您自己的 SNS 主題和Amazon GameLift Servers配對組態。

------
#### [ 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": "111122223333"
           }
         }
       },
       {
         "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:matchmakingconfiguration/your_configuration_name"
           }
         }
       }
     ]
   }
   ```

------

1. 選擇**儲存變更**。

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

您可以使用伺服器端加密 (SSE) 將敏感資料存放在加密主題中。SSE 使用在 AWS Key Management Service () 中管理的金鑰來保護 Amazon SNS 主題中的訊息內容AWS KMS。如需使用 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:matchmakingconfiguration/your_configuration_name" 
      },
      "StringEquals": { 
        "kms:EncryptionContext:aws:sns:topicArn": "arn:aws:sns:your_region:your_account:your_sns_topic_name" 
      }
  }
}
```

# 設定主題訂閱以叫用 Lambda 函數
<a name="match-notification-lambda"></a>

您可以使用發佈至 Amazon SNS 主題的事件通知來叫用 Lambda 函數。設定配對建構器時，請務必將通知目標設定為 SNS 主題的 ARN。

下列 AWS CloudFormation 範本會將名為 的 SNS 主題訂閱設定為`MyFlexMatchEventTopic`叫用名為 的 Lambda 函數`FlexMatchEventHandlerLambdaFunction`。範本會建立 IAM 許可政策，Amazon GameLift Servers允許 寫入 SNS 主題。範本接著會新增 SNS 主題的許可，以叫用 Lambda 函數。

```
FlexMatchEventTopic:
  Type: "AWS::SNS::Topic"
  Properties:
    KmsMasterKeyId: alias/aws/sns #Enables server-side encryption on the topic using an AWS managed key 
    Subscription:
      - Endpoint: !GetAtt FlexMatchEventHandlerLambdaFunction.Arn
        Protocol: lambda
    TopicName: MyFlexMatchEventTopic

FlexMatchEventTopicPolicy:
  Type: "AWS::SNS::TopicPolicy"
  DependsOn: FlexMatchEventTopic
  Properties:
    PolicyDocument:
      Version: "2012-10-17"
      Statement:
        - Effect: Allow
          Principal:
            Service: gamelift.amazonaws.com
          Action:
            - "sns:Publish"
          Resource: !Ref FlexMatchEventTopic
    Topics:
      - Ref: FlexMatchEventTopic

FlexMatchEventHandlerLambdaPermission:
  Type: "AWS::Lambda::Permission"
  Properties:
    Action: "lambda:InvokeFunction"
    FunctionName: !Ref FlexMatchEventHandlerLambdaFunction
    Principal: sns.amazonaws.com
    SourceArn: !Ref FlexMatchEventTopic
```