Leverage fully-managed containers to host multiplayer games at global scale on Amazon GameLift
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:
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.
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.
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.
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.
Now review the configuration and select Submit to create the fleet.
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:
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:
Now create the Container Group Definition. Select Group definitions, and select Create container group.
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.
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.
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.
Then, review the configuration and select Submit to create the container group definition.
You’re now ready to deploy the game servers to the fleet. Open Fleets under the Containers’ Group definitions and select your fleet.
Then select Edit.
Select Add container group, and select the container group you just created, and then select the initial version 1.
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.
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:
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
Author: Juho Jantunen