Skip to content

Commit 834fab4

Browse files
authored
feat(gamelift): add BuildFleet L2 Construct for GameLift (#22835)
Following aws/aws-cdk-rfcs#436 I have written the Gamelift BuildFleet L2 resource which create a Fleet resource. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent f2e5447 commit 834fab4

27 files changed

+2891
-24
lines changed

packages/@aws-cdk/aws-gamelift/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ node_modules
66
*.generated.ts
77
dist
88
.jsii
9+
.DS_Store
910

1011
.LAST_BUILD
1112
.nyc_output
@@ -23,5 +24,4 @@ junit.xml
2324
!**/*.snapshot/**/asset.*/**
2425

2526
#include game build js file
26-
!test/my-game-build/*.js
2727
!test/my-game-script/*.js

packages/@aws-cdk/aws-gamelift/README.md

Lines changed: 253 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ configuration or game server fleet management system.
5252

5353
## GameLift Hosting
5454

55-
### Defining a GameLift Fleet
56-
57-
GameLift helps you deploy, operate, and scale dedicated game servers for
58-
session-based multiplayer games. It helps you regulate the resources needed to
59-
host your games, finds available game servers to host new game sessions, and
60-
puts players into games.
61-
6255
### Uploading builds and scripts to GameLift
6356

6457
Before deploying your GameLift-enabled multiplayer game servers for hosting with the GameLift service, you need to upload
@@ -110,6 +103,258 @@ new gamelift.Script(this, 'Script', {
110103
});
111104
```
112105

106+
### Defining a GameLift Fleet
107+
108+
#### Creating a custom game server fleet
109+
110+
Your uploaded game servers are hosted on GameLift virtual computing resources,
111+
called instances. You set up your hosting resources by creating a fleet of
112+
instances and deploying them to run your game servers. You can design a fleet
113+
to fit your game's needs.
114+
115+
```ts
116+
new gamelift.BuildFleet(this, 'Game server fleet', {
117+
fleetName: 'test-fleet',
118+
content: gamelift.Build.fromAsset(this, 'Build', path.join(__dirname, 'CustomerGameServer')),
119+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C4, ec2.InstanceSize.LARGE),
120+
runtimeConfiguration: {
121+
serverProcesses: [{
122+
launchPath: 'test-launch-path',
123+
}],
124+
},
125+
});
126+
```
127+
128+
### Managing game servers launch configuration
129+
130+
GameLift uses a fleet's runtime configuration to determine the type and number
131+
of processes to run on each instance in the fleet. At a minimum, a runtime
132+
configuration contains one server process configuration that represents one
133+
game server executable. You can also define additional server process
134+
configurations to run other types of processes related to your game. Each
135+
server process configuration contains the following information:
136+
137+
* The file name and path of an executable in your game build.
138+
139+
* Optionally Parameters to pass to the process on launch.
140+
141+
* The number of processes to run concurrently.
142+
143+
A GameLift instance is limited to 50 processes running concurrently.
144+
145+
```ts
146+
declare const build: gamelift.Build;
147+
// Server processes can be delcared in a declarative way through the constructor
148+
const fleet = new gamelift.BuildFleet(this, 'Game server fleet', {
149+
fleetName: 'test-fleet',
150+
content: build,
151+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C4, ec2.InstanceSize.LARGE),
152+
runtimeConfiguration: {
153+
serverProcesses: [{
154+
launchPath: '/local/game/GameLiftExampleServer.x86_64',
155+
parameters: '-logFile /local/game/logs/myserver1935.log -port 1935',
156+
concurrentExecutions: 100,
157+
}]
158+
}
159+
});
160+
```
161+
162+
See [Managing how game servers are launched for hosting](https://docs.aws.amazon.com/gamelift/latest/developerguide/fleets-multiprocess.html)
163+
in the *Amazon GameLift Developer Guide*.
164+
165+
### Defining an instance type
166+
167+
GameLift uses Amazon Elastic Compute Cloud (Amazon EC2) resources, called
168+
instances, to deploy your game servers and host game sessions for your players.
169+
When setting up a new fleet, you decide what type of instances your game needs
170+
and how to run game server processes on them (using a runtime configuration). All instances in a fleet use the same type of resources and the same runtime
171+
configuration. You can edit a fleet's runtime configuration and other fleet
172+
properties, but the type of resources cannot be changed.
173+
174+
```ts
175+
declare const build: gamelift.Build;
176+
new gamelift.BuildFleet(this, 'Game server fleet', {
177+
fleetName: 'test-fleet',
178+
content: build,
179+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C5, ec2.InstanceSize.LARGE),
180+
runtimeConfiguration: {
181+
serverProcesses: [{
182+
launchPath: '/local/game/GameLiftExampleServer.x86_64',
183+
}]
184+
}
185+
});
186+
```
187+
188+
### Using Spot instances
189+
190+
When setting up your hosting resources, you have the option of using Spot
191+
Instances, On-Demand Instances, or a combination.
192+
193+
By default, fleet are using on demand capacity.
194+
195+
```ts
196+
declare const build: gamelift.Build;
197+
new gamelift.BuildFleet(this, 'Game server fleet', {
198+
fleetName: 'test-fleet',
199+
content: build,
200+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C4, ec2.InstanceSize.LARGE),
201+
runtimeConfiguration: {
202+
serverProcesses: [{
203+
launchPath: '/local/game/GameLiftExampleServer.x86_64',
204+
}]
205+
},
206+
useSpot: true
207+
});
208+
```
209+
210+
### Allowing Ingress traffic
211+
212+
The allowed IP address ranges and port settings that allow inbound traffic to
213+
access game sessions on this fleet.
214+
215+
New game sessions are assigned an IP address/port number combination, which
216+
must fall into the fleet's allowed ranges. Fleets with custom game builds must
217+
have permissions explicitly set. For Realtime Servers fleets, GameLift
218+
automatically opens two port ranges, one for TCP messaging and one for UDP.
219+
220+
```ts
221+
declare const build: gamelift.Build;
222+
223+
const fleet = new gamelift.BuildFleet(this, 'Game server fleet', {
224+
fleetName: 'test-fleet',
225+
content: build,
226+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C4, ec2.InstanceSize.LARGE),
227+
runtimeConfiguration: {
228+
serverProcesses: [{
229+
launchPath: '/local/game/GameLiftExampleServer.x86_64',
230+
}]
231+
},
232+
ingressRules: [{
233+
source: gamelift.Peer.anyIpv4(),
234+
port: gamelift.Port.tcpRange(100, 200),
235+
}]
236+
});
237+
// Allowing a specific CIDR for port 1111 on UDP Protocol
238+
fleet.addIngressRule(gamelift.Peer.ipv4('1.2.3.4/32'), gamelift.Port.udp(1111));
239+
```
240+
241+
### Managing locations
242+
243+
A single Amazon GameLift fleet has a home Region by default (the Region you
244+
deploy it to), but it can deploy resources to any number of GameLift supported
245+
Regions. Select Regions based on where your players are located and your
246+
latency needs.
247+
248+
By default, home region is used as default location but we can add new locations if needed and define desired capacity
249+
250+
```ts
251+
declare const build: gamelift.Build;
252+
253+
// Locations can be added directly through constructor
254+
const fleet = new gamelift.BuildFleet(this, 'Game server fleet', {
255+
fleetName: 'test-fleet',
256+
content: build,
257+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C4, ec2.InstanceSize.LARGE),
258+
runtimeConfiguration: {
259+
serverProcesses: [{
260+
launchPath: '/local/game/GameLiftExampleServer.x86_64',
261+
}]
262+
},
263+
locations: [ {
264+
region: 'eu-west-1',
265+
capacity: {
266+
desiredCapacity: 5,
267+
minSize: 2,
268+
maxSize: 10
269+
}
270+
}, {
271+
region: 'us-east-1',
272+
capacity: {
273+
desiredCapacity: 5,
274+
minSize: 2,
275+
maxSize: 10
276+
}
277+
}]
278+
});
279+
280+
// Or through dedicated methods
281+
fleet.addLocation('ap-southeast-1', 5, 2, 10);
282+
```
283+
284+
### Specifying an IAM role for a Fleet
285+
286+
Some GameLift features require you to extend limited access to your AWS
287+
resources. This is done by creating an AWS IAM role. The GameLift Fleet class
288+
automatically created an IAM role with all the minimum necessary permissions
289+
for GameLift to access your ressources. If you wish, you may
290+
specify your own IAM role.
291+
292+
```ts
293+
declare const build: gamelift.Build;
294+
const role = new iam.Role(this, 'Role', {
295+
assumedBy: new iam.CompositePrincipal(new iam.ServicePrincipal('gamelift.amazonaws.com'))
296+
});
297+
role.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('CloudWatchAgentServerPolicy'));
298+
299+
const fleet = new gamelift.BuildFleet(this, 'Game server fleet', {
300+
fleetName: 'test-fleet',
301+
content: build,
302+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C5, ec2.InstanceSize.LARGE),
303+
runtimeConfiguration: {
304+
serverProcesses: [{
305+
launchPath: '/local/game/GameLiftExampleServer.x86_64',
306+
}]
307+
},
308+
role: role
309+
});
310+
311+
// Actions can also be grantted through dedicated method
312+
fleet.grant(role, 'gamelift:ListFleets');
313+
```
314+
315+
### Monitoring your Fleet
316+
317+
GameLift is integrated with CloudWatch, so you can monitor the performance of
318+
your game servers via logs and metrics.
319+
320+
#### Metrics
321+
322+
GameLift Fleet sends metrics to CloudWatch so that you can collect and analyze
323+
the activity of your Fleet, including game and player sessions and server
324+
processes.
325+
326+
You can then use CloudWatch alarms to alert you, for example, when matches has
327+
been rejected (potential matches that were rejected by at least one player
328+
since the last report) exceed a certain thresold which could means that you may
329+
have an issue in your matchmaking rules.
330+
331+
CDK provides methods for accessing GameLift Fleet metrics with default configuration,
332+
such as `metricActiveInstances`, or `metricIdleInstances` (see [`IFleet`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-gamelift.IFleet.html)
333+
for a full list). CDK also provides a generic `metric` method that can be used
334+
to produce metric configurations for any metric provided by GameLift Fleet,
335+
Game sessions or server processes; the configurations are pre-populated with
336+
the correct dimensions for the matchmaking configuration.
337+
338+
```ts
339+
declare const fleet: gamelift.BuildFleet;
340+
// Alarm that triggers when the per-second average of not used instances exceed 10%
341+
const instancesUsedRatio = new cloudwatch.MathExpression({
342+
expression: '1 - (activeInstances / idleInstances)',
343+
usingMetrics: {
344+
activeInstances: fleet.metric('ActiveInstances', { statistic: cloudwatch.Statistic.SUM }),
345+
idleInstances: fleet.metricIdleInstances(),
346+
},
347+
});
348+
new cloudwatch.Alarm(this, 'Alarm', {
349+
metric: instancesUsedRatio,
350+
threshold: 0.1,
351+
evaluationPeriods: 3,
352+
});
353+
```
354+
355+
See: [Monitoring Using CloudWatch Metrics](https://docs.aws.amazon.com/gamelift/latest/developerguide/monitoring-cloudwatch.html)
356+
in the *Amazon GameLift Developer Guide*.
357+
113358
## GameLift FleetIQ
114359

115360
The GameLift FleetIQ solution is a game hosting layer that supplements the full
@@ -184,7 +429,7 @@ new gamelift.GameServerGroup(this, 'Game server group', {
184429
See [Manage game server groups](https://docs.aws.amazon.com/gamelift/latest/fleetiqguide/gsg-integrate-gameservergroup.html)
185430
in the *Amazon GameLift FleetIQ Developer Guide*.
186431

187-
### Specifying an IAM role
432+
### Specifying an IAM role for GameLift
188433

189434
The GameLift FleetIQ class automatically creates an IAM role with all the minimum necessary
190435
permissions for GameLift to access your Amazon EC2 Auto Scaling groups. If you wish, you may

0 commit comments

Comments
 (0)