TerraformPilot

DevOps

Terraform for Game Server Backends on AWS GameLift

Provision multiplayer game server backends with Terraform: AWS GameLift fleets, FlexMatch matchmaking, queues, and player session APIs.

LLuca Berton1 min read

Game backends for consoles (PlayStation, Xbox, Nintendo, Steam Deck) and mobile multiplayer share a common cloud problem: dedicated server fleets, fair matchmaking, and global low-latency placement. AWS GameLift solves it; Terraform makes the setup reproducible per title and per region.

Architecture

#
ComponentAWS service
Server buildGameLift Build
ComputeGameLift Fleet (EC2 or Containers)
MatchmakingGameLift FlexMatch
RoutingGameLift Queue
Player authCognito + signed session tokens
TelemetryCloudWatch + Kinesis Data Streams

Upload a Server Build

#
resource "aws_gamelift_build" "server" {
  name             = "tower-defense-${var.build_version}"
  operating_system = "AMAZON_LINUX_2023"
  version          = var.build_version
 
  storage_location {
    bucket   = aws_s3_bucket.builds.bucket
    key      = "servers/tower-defense-${var.build_version}.zip"
    role_arn = aws_iam_role.gamelift_s3.arn
  }
}

Fleet (EC2 with Spot)

#
resource "aws_gamelift_fleet" "td" {
  name              = "tower-defense"
  build_id          = aws_gamelift_build.server.id
  ec2_instance_type = "c7i.large"
  fleet_type        = "ON_DEMAND"
 
  runtime_configuration {
    server_process {
      launch_path = "/local/game/server"
      concurrent_executions = 4
      parameters  = "--port 7777"
    }
  }
 
  ec2_inbound_permission {
    from_port = 7777
    to_port   = 7787
    ip_range  = "0.0.0.0/0"
    protocol  = "UDP"
  }
 
  metric_groups = ["tower-defense"]
}
 
resource "aws_gamelift_fleet" "td_spot" {
  name              = "tower-defense-spot"
  build_id          = aws_gamelift_build.server.id
  ec2_instance_type = "c7i.large"
  fleet_type        = "SPOT"
 
  runtime_configuration {
    server_process {
      launch_path           = "/local/game/server"
      concurrent_executions = 4
    }
  }
}

Game Session Queue (Multi-Region)

#
resource "aws_gamelift_game_session_queue" "td" {
  name = "tower-defense-global"
 
  destinations = [
    aws_gamelift_fleet.td.arn,
    aws_gamelift_fleet.td_spot.arn,
  ]
 
  player_latency_policy {
    maximum_individual_player_latency_milliseconds = 200
    policy_duration_seconds                        = 60
  }
 
  player_latency_policy {
    maximum_individual_player_latency_milliseconds = 100
  }
 
  timeout_in_seconds = 60
}

FlexMatch Matchmaking

#
resource "aws_gamelift_matchmaking_rule_set" "td" {
  name          = "tower-defense-2v2"
  rule_set_body = file("${path.module}/rules/2v2.json")
}
 
resource "aws_gamelift_matchmaking_configuration" "td" {
  name                    = "tower-defense"
  game_session_queue_arns = [aws_gamelift_game_session_queue.td.arn]
  rule_set_name           = aws_gamelift_matchmaking_rule_set.td.name
  request_timeout_seconds = 30
  acceptance_required     = false
  backfill_mode           = "AUTOMATIC"
  flex_match_mode         = "WITH_QUEUE"
}

Best Practices

#
  • Spot fleets for casual modes, On-Demand for ranked — Spot can interrupt a session.
  • Multi-region queues with latency policies so players match to the nearest fleet they can tolerate.
  • Stage builds: upload via Terraform but promote between matchmaking configs (canary 5% → 100%).
  • Cap concurrent_executions based on your actual server profile; over-packing causes tick-rate drops.
  • Stream events to Kinesis for live ops dashboards (Pinpoint funnels, churn, cheating signals).
#
#Terraform#Gaming#AWS#GameLift#Multiplayer

Share this article