Set up FlexMatch event notifications - Amazon GameLift

Set up FlexMatch event notifications

If you're using GameLift FlexMatch matchmaking in your game, you need a way to track the status of individual matchmaking requests. Implementing event notifications is a fast and efficient method for tracking matchmaking events. All games in production, or in pre-production with high-volume matchmaking activity, should use event notifications.

There are two options for setting up event notifications. You can use Amazon CloudWatch Events, which has a suite of tools available for managing events and taking action on them. Or, you can set up your own Amazon Simple Notification Service (Amazon SNS) topics and configure your matchmaker to send matchmaking event notifications directly to the topics.

For a list of the FlexMatch events that GameLift emits, see FlexMatch matchmaking events.

Set up CloudWatch Events

GameLift automatically posts all matchmaking events to CloudWatch Events. With CloudWatch Events, you can set up rules to have matchmaking events routed to a range of targets, including SNS topics and other AWS services for processing. For example, you might set a rule to route the event "PotentialMatchCreated" to an AWS Lambda function that handles player acceptances. For more information about how to use CloudWatch Events, including a collection of tutorials, see Getting Started with Amazon CloudWatch Events in the Amazon CloudWatch Events User Guide.

If you plan to use CloudWatch Events, when configuring your matchmakers, you can keep the notification target field empty, or reference an SNS topic if you want to use both options.

You can access GameLift matchmaking events in CloudWatch Events in the CloudWatch console. For more information, see Sign in to the Amazon CloudWatch Console. CloudWatch Events identifies each matchmaking event by the service (GameLift), the matchmaking name, and the matchmaking ticket.

Set up an Amazon SNS topic

You can have GameLift publish all events that a FlexMatch matchmaker generates to an Amazon SNS topic.

To create an SNS topic for GameLift event notifications

  1. Open the Amazon SNS console.

  2. In the navigation pane, choose Topics.

  3. On the Topics page, choose Create topic.

  4. Create a topic in the console. For more information, see To create a topic using the AWS Management Console in the Amazon Simple Notification Service Developer Guide.

  5. On the Details page for your topic, choose Edit.

  6. On the Edit page for your topic, expand Access policy - optional, and then add the bold syntax from the following AWS Identity and Access Management (IAM) policy statement to the end of your existing policy. (The entire policy is shown here for clarity.) Be sure to use the Amazon Resource Name (ARN) details for your own SNS topic and GameLift matchmaking configuration.

    { "Version": "2008-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:your_region:your_account:your_topic_name", "Condition": { "StringEquals": { "AWS:SourceAccount": "your_account" } } }, { "Sid": "__console_pub_0", "Effect": "Allow", "Principal": { "Service": "" }, "Action": "SNS:Publish", "Resource": "arn:aws:sns:your_region:your_account:your_topic_name", "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws:gamelift:your_region:your_account:matchmakingconfiguration/your_matchmaking_configuration_name" } } } ] }
  7. Choose Save changes.

Configure a topic subscription to invoke a Lambda function

You can invoke a Lambda function using event notifications published to your Amazon SNS topic. When configuring the matchmaker, be sure to set the notification target to your SNS topic's ARN.

The following AWS CloudFormation template configures a subscription to an SNS topic named MyFlexMatchEventTopic to invoke a Lambda function named FlexMatchEventHandlerLambdaFunction. The template creates an IAM permissions policy that allows GameLift to write to the SNS topic. Finally, it adds permissions for the SNS topic to invoke the Lambda function.

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: Action: - "sns:Publish" Resource: !Ref FlexMatchEventTopic Topics: - Ref: FlexMatchEventTopic FlexMatchEventHandlerLambdaPermission: Type: "AWS::Lambda::Permission" Properties: Action: "lambda:InvokeFunction" FunctionName: !Ref FlexMatchEventHandlerLambdaFunction Principal: SourceArn: !Ref FlexMatchEventTopic