Implementing AI-Powered Matchmaking with Amazon GameLift FlexMatch

TutoSartup excerpt from this article:
In the first part of this two-part blog series, we showed you how to use Amazon SageMaker AI, a suite of ML tools, to create and deploy an automated ML pipeline… In this blog, we’ll show you how to use this skill value in conjunction with Amazon GameLift Servers FlexMatch (FlexMatch)… We’…

In competitive multiplayer games, skill-based matchmaking is crucial for creating fun and engaging games. Determining player skill today is difficult due to the vast array of metrics games record such as hits, misses, assists, time played, level, and more. This can make it challenging to manually determine which factors are most indicative of skill.

Instead of manually creating algorithms to determine player skill, machine learning (ML) techniques—particularly supervised learning—can automatically identify patterns across game metrics to produce more accurate skill ratings. These ML-derived ratings enable more balanced matchmaking, ultimately enhancing player satisfaction and engagement.

In the first part of this two-part blog series, we showed you how to use Amazon SageMaker AI, a suite of ML tools, to create and deploy an automated ML pipeline. This ML model produced a skill value that is a more reflective and precise rating of each player’s actual skill.

In this blog, we’ll show you how to use this skill value in conjunction with Amazon GameLift Servers FlexMatch (FlexMatch). FlexMatch handles the logic of matchmaking players while giving you, the developer, a way to adjust which matches are created through a rules-based syntax called FlexMatch rule sets.

We’ll demonstrate how to create matchmaking rules for creating teams of players with similar skills. We’ll also highlight how to test these rule sets by simulating matchmaking using the Amazon GameLift Testing Toolkit. This toolkit helps game developers to optimize their multiplayer games by providing ways to visualize Amazon GameLift infrastructure, launch virtual players, iterate on matchmaking rules, and simulate matchmaking events. By using the Amazon GameLift Testing Toolkit, you can quickly test the performance of your model and matchmaking solution before deploying your matchmaking solution to production servers.

Walkthrough

Prerequisites

For this walkthrough, you should have the following prerequisites:

  • Completed the steps included in part 1 of this series.
  • To download the code we will need for this tutorial, choose FlexMatchSimulator.cs. This will take you to the page where you can choose the “Download raw file” option (as seen in Figure 1).
Choosing the download button for the FlexMatchSimulator.cs file. The Download button on the right-hand side is circled in red to highlight its location.

Figure 1: Choosing to download the raw file.

Understanding the Amazon GameLift Testing Toolkit solution

The illustration in Figure 2 shows you the architecture Amazon GameLift Testing Toolkit deploys.

AWS Cloud architecture diagram showing the flow of a sample game system. The diagram is divided into two main sections: management console and sample game. The management console section includes the user interface, the management services, and the game listener. The user interface section includes CloudFront, Amazon S3, and Amazon Cognito. The management services section includes Amazon API Gateway, AWS Lambda, DynamoDB. The game listener section includes Amazon EventBridge and AWS Step Functions. The sample game side includes game servers, game client services, and virtual players. The game servers consist of Amazon CloudWatch, Amazon GameLift, and Amazon EC2 Instances. Game client services include API Gateway, Lambda, and DynamoDB. Virtual players are managed through AWS Fargate running multiple tasks, with Amazon Cognito handling authentication. Numbers from 1 to 9 indicate the sequence of interactions between components. Directional arrows indicate the flow of requests between services. All elements are represented by their respective AWS service icons.

Figure 2: Amazon GameLift Testing Toolkit architecture diagram.

Typically, designing and testing matchmaking systems comes very late in the game development cycle because it requires a working game client and running game servers. Additionally, to thoroughly test your game infrastructure at a scale realistic to what you expect during launch, you either need to gather a large volume of players or create virtual players. This can increase your overall cost and infrastructure management overhead.

The Amazon GameLift Testing Toolkit provides game developers with a test harness and visualization tool for Amazon GameLift and FlexMatch. This tool allows game developers to begin testing their game’s matchmaking logic early in the development process, since it does not require running game servers, virtual players, or a completed game client. It instead creates virtual players and runs simulated matchmaking sessions. To learn more about the architecture that the Amazon GameLift Testing Toolkit deploys, see the Amazon GameLift Testing Toolkit Guidance. For a more in-depth guide into what you can do with the Amazon GameLift Testing Toolkit, see the Amazon GameLift Testing Toolkit documentation.

Deploy the Amazon GameLift Testing Toolkit solution

First, download and follow the instructions in the Amazon GameLift Testing Toolkit repository. After you follow the instructions, navigate to the resulting Amazon CloudFront URL and sign in, you will see a page that looks like Figure 3.

Amazon GameLift Testing Toolkit landing page. The page has a dark background with a gear icon and character sprite in the top left corner. The AWS for Games logo is visible in the top right. The main content area is divided into three sections: "Matchmaking Configurations", "Queues", and "Fleets", each separated by dotted lines.

Figure 3: Amazon GameLift Testing Toolkit landing page.

Configuring player profiles in the testing toolkit

In order to perform matchmaking, we will be using Amazon GameLift Servers FlexMatch. To test FlexMatch on the Amazon GameLift Testing Toolkit we will need:

  1. A pool of player profiles to simulate matchmaking between
  2. The rule set(s) we want to test

For a more in-depth guide on how FlexMatch works, including more capabilities and in-depth instructions on how to test FlexMatch with the Amazon GameLift Testing Toolkit, read: Fine-tune online game matchmaking with Amazon GameLift FlexMatch rule sets and the Amazon GameLift Testing Toolkit.

For this specific blog, create the components we need by following these instructions:

  1. From the Amazon GameLift Testing Toolkit dashboard, choose the gear icon on the top left corner.
  2. Choose FlexMatch Simulator.
Player Profiles menu in the Amazon GameLift Testing Toolkit. The left sidebar contains navigation options including "Virtual Player Tasks", "Manage FlexMatch Rule Sets", "FlexMatch Simulator" (highlighted in orange), "Purge Data", and "Logout". The main content area shows a table of player profiles with columns for Name, # Attributes, Edit, and Delete. Four example player profiles are listed (player140, player120, player110, player10), each with 1 attribute and orange "EDIT PROFILE" and "DELETE PROFILE" buttons. Above the table are tabs for "Player Profiles", "Latency Profiles", and "Manage Rule Sets", with an orange "CREATE PLAYER PROFILE" button. The table has pagination controls showing "Showing 1 to 4 of 4 entries" with Previous and Next buttons.

Figure 4: Amazon GameLift Testing Toolkit player profiles menu.

  1. Choose Player Profiles. You will see something similar to Figure 4, which shows you all your player profiles.
  2. Choose Create Player Profile. You should see a form to give the player profile name and add any number of attributes to it, as shown in Figure 5.
Player Profile creation form in the Amazon GameLift Testing Toolkit. The interface shows a left sidebar with "Simulate Matchmaking" options, including "Player Profiles" (highlighted in orange), "Latency Profiles", and "Manage Rule Sets". The main form contains two text input fields: one requesting "a unique name for your player profile" and another optional field for entering "a team name to assign these players to a specific team". At the bottom of the form are two orange buttons: "ADD ATTRIBUTE" and "CREATE PROFILE". A "BACK" button appears at the top of the form.

Figure 5: Amazon GameLift Testing Toolkit player profile creation menu.

  1. For the name, enter Good Player.
  2. Choose Add Attributes.
  3. Enter the following values with the Number attribute type. Choose Add Attribute to add each new one:
    • kills: 1755500
    • hits: 3672500
    • timePlayed: 1250
    • gamesPlayed: 7830
    • assists: 2125
    • misses: 16017200
    • shots: 19689500
    • deaths: 100420
    • winRate: 60
  1. Choose Create Profile.
Amazon GameLift Testing Toolkit interface showing a player profile creation screen. The profile named "Good Player" includes multiple numerical attributes: 1,755,500 kills, 3,672,500 hits, 1,250 time played, 7,830 games played, 2,125 assists, 16,017,200 misses, 19,689,500 shots, 100,420 deaths, and a 60% win rate. Each attribute has a remove button, type selector set to "Number," and value type selector set to "Value." Options to add additional attributes or create the profile are shown at the bottom.

Figure 6: Amazon GameLift Testing Toolkit player profile for Good Player profile.

  1. Choose Create Player Profile.
  2. For the name, enter Bad Player.
  3. Choose Add Attributes.
  4. Enter the following values with the Number attribute type. Choose Add Attribute to add each new one:
    • kills: 10
    • hits: 60
    • timePlayed: 5
    • gamesPlayed: 12
    • assists: 2
    • misses: 160
    • shots: 196
    • deaths: 100
    • winRate: 30
Amazon GameLift Testing Toolkit interface showing a player profile creation screen. The profile named "Bad Player" includes multiple numerical attributes: 10 kills, 60 hits, 5 time played, 12 games played, 2 assists, 160 misses, 196 shots, 100 deaths, and a 30% win rate. Each attribute has a remove button, type selector set to "Number," and value type selector set to "Value." Options to add additional attributes or create the profile are shown at the bottom.

Figure 7: Amazon GameLift Testing Toolkit player profile for Bad Player profile.

  1. Choose Create Profile.

Configuring your matchmaking rules for your FlexMatch matchmaker

  1. Open the Amazon GameLift Servers console dashboard.
  2. Choose Matchmaking rule sets under the FlexMatch section of the menu on the left side.
Amazon GameLift Dashboard in the AWS console page showing the main navigation menu on the left with "Matchmaking rule sets" circled in red under the FlexMatch section.

Figure 8: Choosing matchmaking rule sets in the Amazon GameLift dashboard menu.

  1. Choose Create matchmaking rule set.
Choosing create matchmaking rule set in the matchmaking rule sets AWS console page. At the top right, three buttons are shown: "Delete," "Clone," and an orange "Create matchmaking rule set" button which is circled in red to highlight its location.

Figure 9: Choosing create matchmaking rule set.

  1. Select From scratch and choose Next.
Selecting from scratch template in the create matchmaking rule set menu. Three radio buttons are shown, “Start from sample” on the left, “Clone from existing” in the middle, and “From scratch” on the right which is circled in red, with a “1.” next to it, indicating to first select this option. At the bottom right, an orange “Next” button is circled in right with a 2. next to it, indicating to choose this button after selecting the “From scratch” button.

Figure 10: Selecting from scratch template in the create matchmaking rule set menu.

  1. Give your rule set the name: FairTeams.
  2. Copy and paste the following JSON into your rule set:
{
    "name": "two_team_game",
    "ruleLanguageVersion": "1.0",
    "playerAttributes": [
        {
            "name": "skill",
            "type": "number",
            "default": 0
        }
    ],
    "teams": [
        {
            "name": "squads",
            "minPlayers": 4,
            "maxPlayers": 4,
            "quantity": 2
        }
    ],
    "rules": [
        {
            "name": "FairTeamSkill",
            "description": "The average team skill across each team is within 5 points.",
            "type": "distance",
            "measurements": [
                "avg(teams[*].players.attributes[skill])"
            ],
            "referenceValue": "avg(flatten(teams[*].players.attributes[skill]))",
            "maxDistance": 5
        },
        {
            "name": "SimilarPlayerSkill",
            "description": "The best player and the worst player on a team can have a maximum skill difference of 5 points.",
            "type": "distance",
            "measurements": [
                "max(flatten(teams[*].players.attributes[skill]))"
            ],
            "referenceValue": "min(flatten(teams[*].players.attributes[skill]))",
            "maxDistance": 5
        },
        {
            "name": "CloseTeamSizes",
            "description": "Only launch a game when the team sizes equal",
            "type": "distance",
            "measurements": [
                "max(count(teams[*].players))"
            ],
            "referenceValue": "min(count(teams[*].players))",
            "maxDistance": 0
        }
    ],
    "expansions": [
        {
            "target": "rules[FairTeamSkill].maxDistance",
            "steps": [
                {
                    "waitTimeSeconds": 60,
                    "value": 10
                },
                {
                    "waitTimeSeconds": 120,
                    "value": 15
                }
            ]
        },
        {
            "target": "rules[SimilarPlayerSkill].maxDistance",
            "steps": [
                {
                    "waitTimeSeconds": 60,
                    "value": 10
                },
                {
                    "waitTimeSeconds": 120,
                    "value": 15
                }
            ]
        }
    ]
}
  1. Choose Create.

Integrating the Amazon GameLift Testing Toolkit with Amazon SageMaker AI

In this portion of the tutorial, you will be modifying the FlexMatch Simulator backend code to send player attributes to your newly created Amazon SageMaker AI endpoint to inference the player’s “skill” before matchmaking occurs.

Figure 11 is the architecture you will be implementing in the lab. The diagram shows the flow of how a player’s matchmaking request is handled. The matchmaking request triggers Amazon API Gateway, invoking an Amazon Web Services (AWS) Lambda function, which receives the relevant player data from Amazon DynamoDB. The data is then passed to the Amazon SageMaker AI endpoint, which generates a comprehensive skill value that is then passed to FlexMatch for matchmaking.

Architecture diagram showing the flow of a player-made matchmaking request as implemented in this blog. The sequence starts with a Player icon connecting to the Amazon GameLift Testing Toolkit, which connects to a Request Handler (Amazon API Gateway). The flow then splits: one path goes to Request Matchmaking (AWS Lambda) which connects to Player Data (Amazon DynamoDB), while the main path continues through Request Matchmaking to a Skill Inference Endpoint (Amazon SageMaker AI), and finally to Matchmaking (Amazon GameLift FlexMatch). All components are represented by their respective AWS service icons and connected by directional arrows indicating the flow of the request.

Figure 11: Matchmaking workflow using matchmaking simulator.

Figure 12 shows how you would implement this solution in an actual game by connecting FlexMatch to an Amazon GameLift queue. The queue triggers Amazon GameLift to either place players in servers or spin up additional game servers for the newly created matches.

Architecture diagram showing incorporation of GameLift for deployment of game servers. The sequence starts with a Player icon connecting to the Amazon GameLift Testing Toolkit, which connects to a Request Handler (Amazon API Gateway). The flow then splits: one path goes to Request Matchmaking (AWS Lambda) which connects to Player Data (Amazon DynamoDB), while the main path continues through Request Matchmaking to a Skill Inference Endpoint (Amazon SageMaker AI), to Matchmaking (Amazon GameLift FlexMatch). The path continues from Matchmaking to a Matchmaking Queue, which receives the results of the matchmaking requests and either places a player into an existing Game Server or spins up a Game Server to host the match, as indicated by the path from Matchmaking Queue to Game Server. All components are represented by their respective AWS service icons and connected by directional arrows indicating the flow of the request.

Figure 12: Matchmaking workflow as part of a game backend.

  1. Navigate to your development environment with the agttworkshop.
  2. We need to modify the matchmaking simulation in order to inference each player’s “skill” attribute before matchmaking. To do this we will modify /amazon-gamelift-testing-toolkit/ManagementConsole/Backend/ManagementService/FlexMatchSimulator.cs to feed the player stats to the SageMaker AI endpoint and simulate matchmaking with the new skill attribute. Replace the contents of the current cs file with the version you downloaded in the Prerequisites section of this blog.
  3. Navigate to the directory /amazon-gamelift-testing-toolkit/ManagementConsole/Backend/ManagementService and install the dependencies by executing the following commands in your command line:

cd ManagementConsole/Backend
dotnet add package AWSSDK.DynamoDBv2 --version 3.7.101.31
dotnet add package AWSSDK.SagemakerRuntime --version 3.7.101.33
dotnet add package AWSSDK.SagemakerModel --version 3.7.101.33

  1. Now that we have made our changes, we need to rebuild and re-deploy the solution. In your command line, execute the following:

yarn build-toolkit
yarn deploy-toolkit

  1. Once the changes are deployed, open the AWS Lambda console and choose the Function that starts with AGTT-ManagementConsoleSta-FlexMatchSimulatorLambda.
Choosing the FlexMatch simulator Lambda function. In the AWS Lambda console,the function with starting with “AGTT-ManagementConsoleSta-FlexMatchSimulatorLambda” is circled in red to highlight its location.

Figure 13: Choosing the FlexMatch simulator Lambda function.

  1. In the section under Function overview, select the Configuration tab, then select Permissions from the left side menu. Select the role associated with the Execution role.
Navigating to the Execution role of the FlexMatch simulator Lambda function. On the console page of the FlexMathc simulator Lambda function that was selected in the previous step, the Configuration tab is chosen and circled in red to indicate its location, with a “1.” next to it to indicate to choose this first. The Permissions section is selected in the left menu and circled in red to indicate its location, with a “2.” next to it to indicate to choose this second. Finally, the role name is circled in red to indicate its location, with a “3.” next to it to indicate to choose this third.

Figure 14: Navigating to the execution role of the FlexMatch simulator Lambda function.

  1. Choose Add permissions, then choose Create inline policy.
Choosing Add permissions then choosing Create inline policy. In the Permissions menu of the Lambda function selected in the last step, the “Add permissions” dropdown menu on the right has been chosen. It is circled in red to indicate its location, with a “1.” next to it to indicate to choose this first. In the dropdown menu, “Create inline policy” is circled in red to indicate its location, with a “2.” next to it to indicate to choose this second.

Figure 15: Choosing Add permissions then choosing Create inline policy.

  1. For service, choose SageMaker. For actions select InvokeEndpoint for All Resources for the purpose of this demonstration. Choose Next.
An AWS IAM policy editor interface showing SageMaker permissions configuration. The screen displays steps numbered in red: (1) "InvokeEndpoint" is circled, (2) "All" resources option is circled, and (3) "Next" button is circled in red at the bottom right. The interface shows a Visual/JSON toggle and various permission settings for SageMaker endpoint invocation.

Figure 16: Specifying the SageMaker permissions for the FlexMatch simulator Lambda execution role.

  1. For the name of the inline policy enter InvokeSageMaker. Choose Create policy.
  2. Return to the Amazon GameLift Testing Toolkit dashboard and choose the gear icon in the top left corner of the page.
  3. Choose FlexMatch Simulator.
  4. Choose Simulate Matchmaking.
    • For the rule set choose FairTeams.
    • For the Player Profiles, under Profile, choose Good Player. For number of players, enter 8. Choose Add Player Profile.
    • For the second profile, under Profile, choose Bad Player. For number of players, enter 8. Choose Add Player Profile.
  5. Choose Run Simulation. A successful result will display: Status: Simulation Complete (see Figure 17).
Amazon GameLift Testing Toolkit Simulate Matchmaking results overview page. The menu shows results including: 16 total players with 100% successful matching rate (16/16), 20 total events, 2 game sessions attempted and matched, and a breakdown of matchmaking event types. The status shows "Simulation Complete" in green, with orange buttons at the bottom for viewing detailed results and matchmaking tickets.

Figure 17: Amazon GameLift Testing Toolkit simulation matchmaking results overview page.

  1. To view the details of the match you can choose SHOW SIMULATION RESULTS. On the table, shown in Figure 18, you’ll see the number of each player profile placed on each team.
    • Choosing Match Info on each of the items in the table will show you more details about who was placed together in a match.

Reviewing the results, you can see the good players were matched up in the same session as the other good players, and the bad players were matched up in the same session as the other bad players.

The table has columns for Date, Players, Latency Profiles, Average Time, and Match Info. Two matches are displayed: one with "squads1 - 4 Good Player" and "squads2 - 4 Good Player" taking 1.4 seconds, and another with "squads1 - 4 Bad Player" and "squads2 - 4 Bad Player" taking 1.1 seconds. Each row has an orange "MATCH INFO" button. Pagination shows "Showing 1 to 2 of 2 entries" with Previous, 1, and Next navigation options.

Figure 18: Amazon GameLift Testing Toolkit simulation matchmaking detailed results page.

Next, we’ll show you how to clean up what you’ve deployed during this blog series demonstrations.

Cleaning up

Deleting the Guidance for AI-Driven Player Insights

Delete the S3 bucket

  1. Open the Amazon S3 console and select the radio button for the bucket that was created by the PlayerSkills-Stack, then choose Empty.
  2. Once the bucket is empty, select the radio button again and choose Delete.

Once you have deleted the S3 bucket, there are two options for deleting the deployed guidance:

  1. Using the AWS Management Console
    • Open the AWS CloudFormation console, and select the AWS Region into which you have deployed the guidance.
    • Select the radio button for the deployed stack (such as, PlayerSkills-Stack).
    • Choose Delete to start the stack deletion.
    • Choose Delete to confirm stack deletion.
  2. Using the CDK AWS Command Line Interface (AWS CLI)
    • Using your terminal on your device, change to the root of the cloned repository: cd ~ /player-insights
    • Run the command to delete the CloudFormation stack: cdk destroy
    • When prompted, “Are you sure you want to delete”, enter Y to confirm stack deletion.

Delete the Amazon SageMaker AI endpoint

  1. Navigate to the Amazon SageMaker AI console and under the Inference section, choose
  2. Select the radio button next to the endpoint you created, and choose Actions, then Delete.

Delete the Amazon SageMaker AI domain

  1. Navigate to the Amazon SageMaker AI console. Under the left side menu, choose Domains.
  2. Choose your domain, and choose the User Profiles
  3. Select your user profile, and then select Delete.
  4. Navigate back to the Domain Settings tab.
  5. Scroll down to the Delete domain section, and choose the Delete domain

Delete the Amazon GameLift Testing Toolkit

Delete the S3 buckets

  1. Navigate to the S3 console and empty all S3 buckets with a name beginning with AGTT- by selecting the radio button for the bucket, then choosing Empty.
  2. For each bucket you emptied, select the radio button again and choose Delete.

There are two options for deleting the Amazon GameLift Testing Toolkit:

  1. Using deployment script
    • You can remove the toolkit using the deployment script by opening a terminal window, navigate to the toolkit’s root directory, and then run the following command (remember to enter in the profile name you created):

cd ~/amazon-gamelift-testing-toolkit
yarn delete-toolkit [--profile <profileName>]

  1. Using the AWS Management Console
    • Navigate to the AWS CloudFormation console.
    • Select Stacks.
    • Select the AGTT-ManagementConsoleStack-WebStack stack, choose Delete, and wait until complete.
    • Select the AGTT-ManagementConsoleStack-BackendStack stack, choose Delete, and wait until complete.
    • Select the AGTT-ManagementConsoleStack-SecurityStack stack, choose Delete, and wait until complete.
    • Select the AGTT-ManagementConsoleStack-DataStack stack and choose Delete.

Conclusion

In this final blog of our two-part series, we demonstrated how to use Amazon GameLift Servers FlexMatch to match players based on the skill value produced by the ML model created from the first blog. We also highlighted how to simulate matchmaking using the Amazon GameLift Testing Toolkit in order to test both the model and your matchmaking parameters—without deploying full blown servers to your production workload.

You now know how to create a machine learning-powered matchmaking system that considers multiple skill factors to create more balanced, competitive, and engaging matches. By leveraging Amazon SageMaker AI, with Amazon GameLift Servers FlexMatch, you accomplished this without needing to perform extensive data science operations or having to develop and modify complex machine learning algorithms. As you gather more data and user feedback, you can continue to improve your matchmaking system, making it even more accurate and valuable for your users.

Contact an AWS Representative to know how we can help accelerate your business.

Further reading

Implementing AI-Powered Matchmaking with Amazon GameLift FlexMatch
Author: Christina Defoor