Build Serverless FlexMatch

How to build Serverless FlexMatch

In this Lab, we are starting FlexMatch Matchmaking based on Serverless Architecture.

With FlexMatch, you can implement session based matchmaker easily without provisioning servers for it. To use FlexMatch, you need to call FlexMatch API or SDK on game server or game client side. In this Lab, we are using Lambda. This approach can allow cost-efficient serverless model on your game server backend.

Configuring GameLift Queue

We are going to use GameLift Queue for matchmaking.

  1. Click “Create a queue” on GameLift Console.
  2. Put the name on queue like below. And choose Alias that we made before on destination. Click “Create queue” to make.

Flex-Match

  1. Nextly, we are making rule for FlexMatch. Click “Create matchmaking rule set” button.

  2. Set rule set name as you want, and put rule set policy below on “Rule set box”. Rule set is provided from GomokuRuleSet.json under the rootFolder/Ruleset directory on a given source code. Copy the code and paste it to console box.

{
    "ruleLanguageVersion" : "1.0",
    "playerAttributes" :
    [
        {
            "name" : "score",
            "type" : "number",
            "default" : 1000
        }
    ],
    "teams" :
    [
        {
            "name" : "blue",
            "maxPlayers" : 1,
            "minPlayers" : 1
        },
        {
            "name" : "red",
            "maxPlayers" : 1,
            "minPlayers" : 1
        }
    ],
    "rules" :
    [
        {   "name": "EqualTeamSizes",
            "type": "comparison",
            "measurements": [ "count(teams[red].players)" ],
            "referenceValue": "count(teams[blue].players)",
            "operation": "="
        },
        {
            "name" : "FairTeamSkill",
            "type" : "distance",
            "measurements" : [ "avg(teams[*].players.attributes[score])" ],
            "referenceValue" : "avg(flatten(teams[*].players.attributes[score]))",
            "maxDistance" : 300
        }
    ],
    "expansions" :
    [
        {
            "target" : "rules[FairTeamSkill].maxDistance",
            "steps" :
            [
                {
                    "waitTimeSeconds" : 10,
                    "value" : 500
                },
                {
                    "waitTimeSeconds" : 20,
                    "value" : 800
                },
                {
                    "waitTimeSeconds" : 30,
                    "value" : 1000
                }
            ]
        }
    ]
}

This rule matches players that have score differences under 300, and if match cannot be accepted in time, mitigate matchmaking rule and find players!
Click “Validate rule set”, and check rule set is fine. And click “Create rule set”.

Flex-Match

  1. Next thing to do is linking GameLift Queue and Matchmaking rule set. Click “Create matchmaking configuration” on the menu. Select queue and rule set like below, and create configuration.
  • Please check you choose right region and queue.

Flex-Match

Creating Lambda function

If you deploy CloudFormation stack, Lambda functions have already been made. You should just put required part of these resources.

GameLift FlexMatch configuration was completed!
Next step is building Lambda function and API Gateway that game client makes request for FlexMatch. In this lab, we are going to make two Lambda functions. First one is handling matchmaking requests from client, second one is checking matchmaking results.
Let’s make first Lambda function. It will not be difficult!

  1. Move to Lambda Console. https://console.aws.amazon.com/lambda

  2. Click “Create function” button.

  3. Check “Author from scratch”.

  4. Put the Name of the function “game-match-request”.

  5. Choose Python 3.9 as Runtime.

  6. Choose Role on Permissions Tab. Check “Use an existing role” and select “Gomok-game-match-request” as IAM Role. Click “Create function” button.

Flex-Match

  1. Find Lambda/MatchRequest.py at your source code. Copy the contents and paste it to Console. Check Region and Match Configuration is configured correctly.

Flex-Match

  • Sample Image is just for reference. This can be different from your Lab environment. Also please check Handler and function name is same.
  1. Set Basic settings on your function.
  • Memory : 128MB
  • Timeout : 1 min
  1. Click “Save” button and make function. This function receives Matchmaking requests from game client, reads user data from DynamoDB and send Matchmaking requests to GameLift.

  2. Click “Create function” button and let’s make second function.

  3. Check “Author from scratch” and start with empty function.

  4. Put “game-match-status” as Name, and select Python 3.9 as Runtime.

  5. Choose Role on Permissions Tab. Check “Use an existing role” and select “Gomok-game-match-status” as IAM Role. Click “Create function” button.

  6. Find Lambda/MatchStatus.py at your source code. Copy source code and paste it to Lambda Code textbox.

Flex-Match

  1. Like above, set Basic settings on your function.
  • Memory : 128MB
  • Timeout : 1 min

Please check Lambda functions Handler. Lambda function can access this handler when it is executed.

  1. Click “Save” button and make function. This function check matchmaking request is done by using TicketId that game clients sent, and send Game server’s IP address and Port information to game client as response.

API Gateway Configuration

Let’s make API for these Lambda functions.

  1. Move to API Gateway console. (https://console.aws.amazon.com/apigateway)

  2. Choose API Gateway that is already made, and click “Create Resource” button.

Flex-Match

  1. Write Resource name matchrequest on the box. Check “Enable API Gateway CORS” and click “Create Resource” button.

Flex-Match

  1. Create new method for this resource. Click “Actions” button and “Create Method”. Make API for “POST” method.

  2. Select Integration type with Lambda function, and check correct region. Choose game-match-request for Lambda function and click “Save” button.

Flex-Match

  1. Make second API for second Lambda function. Click “Actions” on the root path and “Create Resource”.

  2. Create API as we had. Resource Name will be matchstatus and enable API Gateway CORS.

Flex-Match

  1. Making POST method, and integrate with game-match-status Lambda function. Click “Save” button.

  2. Now we finish to make APIs for Lambda functions. Let’s deploy this API. Click “Actions” button and “Deploy API”.

Flex-Match

  1. After you deploy your API, you can find Invoke URL. This URL will be used for game client.

Flex-Match