Leverage fully-managed containers to host multiplayer games at global scale on Amazon GameLift

TutoSartup excerpt from this article:
Amazon GameLift, a purpose-built solution for global game server hosting, recently launched containers support in general availability… This feature allows you to upload your game server as a container image and host globally across Amazon Web Services (AWS) Regions and Local Zones… The introd…

Containers have become increasingly popular for hosting game servers, and for a good reason. The benefits include isolated resources per game session, and consistency across environments from your workstation to development and production. Amazon GameLift, a purpose-built solution for global game server hosting, recently launched containers support in general availability. This feature allows you to upload your game server as a container image and host globally across Amazon Web Services (AWS) Regions and Local Zones.

The introduction of containers on Amazon GameLift brings multiple additional improvements. With container fleets, the fleet creation is separated from the deployment of game server builds. This means you can provision a global fleet and deploy new versions of your game server on it with rolling updates. In most instances, the creation of a fleet takes about three minutes. The time to deploy a new game server version on the fleet after this is approximately five minutes. This makes production updates faster, and reduces the need to host separate customized development environments for quick iteration.

The hosting of the container images is done on Amazon Elastic Container Registry (Amazon ECR). Containers introduce a new Amazon GameLift resource called Container Group Definition. This definition tells Amazon GameLift which container image to use, as well as the vCPU and memory resource needs of your game server. You also configure the inbound container port, which typically is a single port allowing UDP or TCP traffic to your game server process. You can run additional per-instance containers on each Amazon Elastic Compute Cloud (Amazon EC2) instance hosted by Amazon GameLift. This is a flexible way to add supporting services such as log management and monitoring tools. These tools can also be run as sidecar containers with each game server individually.

The new container fleets can be configured without specifically defining a game server deployment. They have sensible default configurations for things like automatically mapping public ports to your game server ports. At minimum, you only need to define the fleet AWS Identity and Access Management (IAM) Role to access the Amazon GameLift service.

After your fleet is up and running, you can deploy one or more container groups on it. Amazon GameLift automatically assigns each Amazon EC2 instance with the maximum amount of game server containers it can fit per instance. In addition, you can add more container groups for supporting per-instance services. Log output from your game server containers is automatically sent to Amazon CloudWatch Logs in near real-time. You can also choose to send logs to Amazon Simple Storage Service (Amazon S3), or use a sidecar or per-instance log collector for any third-party tools.

Getting started with containers on Amazon GameLift

To get started, use AWS Command Line Interface (AWS CLI) to create an IAM Role that is assumed by the containers in the fleet:

aws iam create-role 
    --role-name GameLiftFleetRole 
    --assume-role-policy-document '{
        "Version": "2012-10-17",
        "Statement": [{
            "Effect": "Allow",
            "Principal": {
                "Service": "gamelift.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }]
    }'
aws iam attach-role-policy --policy-arn arn:aws:iam::aws:policy/GameLiftContainerFleetPolicy --role-name GameLiftFleetRole

The managed IAM policy definition includes the minimum required permissions for your game server to access Amazon GameLift and other required resources. You might want to customize the role if your game server or supporting containers need to access additional AWS resources.

Then, create a container fleet in the Amazon GameLift console. Select Fleets under the Container’s menu, and select Create container fleet.

Selecting the Fleet under containers menu, and then selecting “Create container fleet” from the top right

Figure 1: Create container fleet in the AWS Management Console.

In the first view, define the description for the fleet, and the IAM role to be used with your containers. Set the description to testfleet, and select the role named GameLiftFleetRole that was created previously. Leave the log group setting on CloudWatch to send near real-time log output of the game servers to the CloudWatch service. Then select Next.

Setting the description to “testfleet”, the IAM role “GameLiftFleetRole”, and then selecting “Next”.

Figure 2: Define fleet description.

Next, select the Amazon EC2 instance type, and whether you’re deploying an On-demand or a Spot fleet. Amazon GameLift has a built-in algorithm to minimize spot interruptions when you assign both a Spot and an On-demand fleet behind the same Amazon GameLift queue.

For now, select On-Demand, and leave the instance type to the default c5.large. If your game server utilizes 0.5 vCPU and 1 GB of memory, this instance type would automatically host four game server processes. You could also add additional locations to host the game servers globally, but start with just a single home region and select Next.

Reviewing the fleet type and instance type settings and selecting “Next”.

Figure 3: Define fleet type and instance type.

Container fleets are automatically configured for suitable inbound ports open for traffic, so you don’t need to customize this. Leave all the rest of the settings as-is and select Next.

Reviewing the additional settings and then selecting “Next”.

Figure 4: Define additional settings

Now review the configuration and select Submit to create the fleet.

Reviewing the configuration and then selecting “Submit”.

Figure 5: Submit the fleet configuration

As you noticed, there are many customization options available. However, you don’t need to customize anything to create a fleet with sensible defaults. While it’s creating, you can upload your game server container image to Amazon ECR.

You’re expected to integrate the latest 5.2.0 version of the Amazon GameLift Server SDK to your game server. A game server is typically a headless server build of your game. A Dockerfile defines how the container image should be built. Following is an example Dockerfile that copies over game server data and defines the entry point to the game server. Customize this to execute your game server binary instead:

FROM --platform=linux/amd64 amazonlinux:latest

# Create app directory
WORKDIR /local/game

# Copy over the game server files
COPY * ./

EXPOSE 1935

# Run the game server on port 1935
ENTRYPOINT ./TestGameServer -p 1935

Using the AWS CLI, create an Amazon ECR repository. Then build your container image, and upload it to the repository by running the following commands. Remember to replace the account ID in the commands with your AWS account ID:

aws ecr create-repository --repository-name gamelift-sample-server --region us-west-2 --output text
aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin 111222333444.dkr.ecr.us-west-2.amazonaws.com/gamelift-sample-server
docker buildx build ./ --platform=linux/amd64 -t 111222333444.dkr.ecr.us-west-2.amazonaws.com/gamelift-sample-server:v1
docker push 111222333444.dkr.ecr.us-west-2.amazonaws.com/gamelift-sample-server:v1

Now create the Container Group Definition. Select Group definitions, and select Create container group.

Selecting Group definitions in the navigation bar, and selecting "Create Container Group" to start creating a new container group.

Figure 6: Create container group definition.

 

Set the name to sample-game-server. You should set the resource configuration to match the requirements of your game server. In our example we set the resource requirements to 1024 MiB memory and 1 vCPU. Keep the selection of Per game server to indicate this is the main game server group. It is hosted as multiple copies of the container on an instance. Then select Next.

Setting the container group name to “sample-game-server”, setting the memory limit to 1024 and vCPU limit to 1.

Figure 7: Define group details.

Add the reference to the Amazon ECR Image URI we previously uploaded. Then set the server SDK version to 5.2.0, which is the minimum version we should integrate with to support container fleets. Match the protocol and port to the ones used by your game server. For our example we set the protocol to TCP and define a single port 1935 that we’re using on each container to host the game server. Then select Next.

Setting the ECR image URI, SDK version to “5.2.0”, and the container port for TCP to 1935.

Figure 8: Add container definitions.

For supporting containers, you could add dependencies between the containers to control the startup and shutdown sequences. As in our example we only have the game server, select Next.

Reviewing container dependencies and selecting “Next”.

Figure 9: Configure optional dependencies.

Then, review the configuration and select Submit to create the container group definition.

Reviewing the container group and selecting “Submit” to create .

Figure 10: Review and create the container group.

You’re now ready to deploy the game servers to the fleet. Open Fleets under the Containers’ Group definitions and select your fleet.

Selecting “Fleets” under Container menu and selecting the previously created fleet.

Figure 11: Open your existing container fleet

Then select Edit.

Selecting “Edit” for the container fleet.

Figure 12: Edit the container fleet.

Select Add container group, and select the container group you just created, and then select the initial version 1.

Selecting the “sample-game-server” container group version 1.

Figure 13: Add the container group details.

 

There are many deployment configuration options available to define exactly how the deployment happens. For example, in a development environment you can choose “Ignore protection” to replace running game servers immediately. In a production environment you would choose “With protection” to protect running sessions from interruption.

Keep everything to default and select Edit.

Reviewing the rest of the fleet settings and selecting “Edit”.

Figure 14: Define the deployment settings.

This triggers a deployment on the fleet, which you can follow from the Deployments tab. Once the fleet becomes active, you can test creating a game session on the fleet. Game session creation triggers the OnStartGameSession callback on one of the game server processes, which is then expected to activate itself by calling ActivateGameSession. After this, the session is ready to host players. When creating a session, you receive the public IP and port to use for connecting to the game server.

Let’s create a game session for a maximum of 10 players in the fleet home location. Run the following command with your Fleet ID on the AWS CLI:

aws gamelift create-game-session 
--fleet-id containerfleet-abcd1234-abcd-1234-abcd-abcd1234 
--location us-west-2 
--maximum-player-session-count 10

Now you could start creating player sessions for this game session and have clients connect to it to play the game.

In addition to manually creating sessions, Amazon GameLift FlexMatch can be used to create game session and player sessions through matchmaking. All the features and APIs that you’ve used in the past for game session placement with Amazon GameLift are available for containers as well. This makes migration to containers straightforward.

Conclusion

We covered how you can leverage the new Amazon GameLift containers feature to host multiplayer game servers at scale in the cloud. We also covered the benefits of running your game servers on containers. These benefits include faster deployment times, isolated environments, and consistent configuration across systems. All these features combined streamline your game server development flow, and reduce the time it takes to get new features out to delight your players.

Amazon GameLift container fleets are now generally available in all regions supported by Amazon GameLift, excluding China. Visit the Amazon GameLift containers developer guide to learn more.

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

Further Reading

Leverage fully-managed containers to host multiplayer games at global scale on Amazon GameLift
Author: Juho Jantunen