Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gamelift): add BuildFleet L2 Construct for GameLift #22735

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding BuildFleet tests and README fixes
  • Loading branch information
stevehouel committed Nov 1, 2022
commit c7b020d17c4221746de0ed146af20bd5dd84fb8c
184 changes: 87 additions & 97 deletions packages/@aws-cdk/aws-gamelift/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ services.
```ts
declare const bucket: s3.Bucket;
new gamelift.Build(this, 'Build', {
content: gamelift.Content.fromBucket(bucket, "sample-asset-key")
content: gamelift.Content.fromBucket(bucket, "sample-asset-key"),
});
```

Expand All @@ -104,28 +104,12 @@ server script onto each instance in the fleet, placing the script files in `/loc
```ts
declare const bucket: s3.Bucket;
new gamelift.Script(this, 'Script', {
content: gamelift.Content.fromBucket(bucket, "sample-asset-key")
content: gamelift.Content.fromBucket(bucket, "sample-asset-key"),
});
```

### Defining a GameLift Fleet

#### Creating a realtime game server fleet

This lightweight server solution provides ready-to-go game servers that you can
configure to fit your game. To set up and optionnally customize a realtime
server fleet, you need to provide a script (in the form of some JavaScript
code).

```ts
import * as s3 from 'aws-cdk-lib/aws-s3-assets';
import * as gamelift from 'aws-cdk-lib/aws-gamelift';

new gamelift.ScriptFleet(this, 'Realtime server fleet', {
content: gamelift.Script.fromAsset(path.join(__dirname, 'file-asset.js')
});
```

#### Creating a custom game server fleet

Your uploaded game servers are hosted on GameLift virtual computing resources,
Expand All @@ -134,11 +118,14 @@ instances and deploying them to run your game servers. You can design a fleet
to fit your game's needs.

```ts
import * as s3 from 'aws-cdk-lib/aws-s3-assets';
import * as gamelift from 'aws-cdk-lib/aws-gamelift';

new gamelift.BuildFleet(this, 'Game server fleet', {
content: gamelift.Build.fromAsset(path.join(__dirname, 'CustomerGameServer/')
content: gamelift.Build.fromAsset(this, 'Build', path.join(__dirname, 'CustomerGameServer')),
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C4, ec2.InstanceSize.LARGE),
runtimeConfiguration: {
serverProcesses: [{
launchPath: 'test-launch-path',
}],
},
});
```

Expand All @@ -159,27 +146,19 @@ server process configuration contains the following information:

A GameLift instance is limited to 50 processes running concurrently.

```ts fixture=with-build
import * as gamelift from '@aws-cdk-lib/aws-gamelift';

```ts
declare const build: gamelift.Build;
// Server processes can be delcared in a declarative way through the constructor
const fleet = new gamelift.BuildFleet(this, 'Game server fleet', {
content: build,
gameSessionActivationTimeoutSeconds: 123,
maxConcurrentGameSessionActivations: 123,
serverProcesses: [{
launchPath: '/local/game/GameLiftExampleServer.x86_64',
parameters: '-logFile /local/game/logs/myserver1935.log -port 1935',
concurrentExecutions: 100,
}]
});

// Or through dedicated runtimeConfiguration methods
fleet.addServerProcess({
launchPath: '/local/game/GameLiftExampleServer.x86_64',
parameters: '-logFile /local/game/logs/myserver1935.log -port 1935',
concurrentExecutions: 100,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C4, ec2.InstanceSize.LARGE),
runtimeConfiguration: {
serverProcesses: [{
launchPath: '/local/game/GameLiftExampleServer.x86_64',
parameters: '-logFile /local/game/logs/myserver1935.log -port 1935',
concurrentExecutions: 100,
}]
}
});
```

Expand All @@ -195,14 +174,16 @@ and how to run game server processes on them (using a runtime configuration). Al
configuration. You can edit a fleet's runtime configuration and other fleet
properties, but the type of resources cannot be changed.

```ts fixture=with-build
import * as gamelift from '@aws-cdk-lib/aws-gamelift';
import * as ec2 from '@aws-cdk-lib/aws-ec2';

```ts
declare const build: gamelift.Build;
new gamelift.BuildFleet(this, 'Game server fleet', {
content: build,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C5, ec2.InstanceSize.LARGE)
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C5, ec2.InstanceSize.LARGE),
runtimeConfiguration: {
serverProcesses: [{
launchPath: '/local/game/GameLiftExampleServer.x86_64',
}]
}
});
```

Expand All @@ -213,12 +194,16 @@ Instances, On-Demand Instances, or a combination.

By default, fleet are using on demand capacity.

```ts fixture=with-build
import * as gamelift from '@aws-cdk-lib/aws-gamelift';

```ts
declare const build: gamelift.Build;
new gamelift.BuildFleet(this, 'Game server fleet', {
content: build,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C4, ec2.InstanceSize.LARGE),
runtimeConfiguration: {
serverProcesses: [{
launchPath: '/local/game/GameLiftExampleServer.x86_64',
}]
},
useSpot: true
});
```
Expand All @@ -233,22 +218,27 @@ must fall into the fleet's allowed ranges. Fleets with custom game builds must
have permissions explicitly set. For Realtime Servers fleets, GameLift
automatically opens two port ranges, one for TCP messaging and one for UDP.

```ts fixture=with-build
import * as gamelift from '@aws-cdk-lib/aws-gamelift';
import * as ec2 from '@aws-cdk-lib/aws-ec2';

```ts
declare const build: gamelift.Build;

const fleet = new gamelift.BuildFleet(this, 'Game server fleet', {
content: build,
inboundPermissions: [

]
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C4, ec2.InstanceSize.LARGE),
runtimeConfiguration: {
serverProcesses: [{
launchPath: '/local/game/GameLiftExampleServer.x86_64',
}]
},
ingressRules: [{
source: gamelift.Peer.anyIpv4(),
port: gamelift.Port.tcpRange(100, 200),
}]
});
// Allowing all IP Addresses from port 1111 to port 1122 on TCP Protocol
fleet.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(1111), ec2.Port.tcp(1122));
fleet.addIngressRule(gamelift.Peer.anyIpv4(), gamelift.Port.tcpRange(100, 200));

// Allowing a specific CIDR for port 1111 on UDP Protocol
fleet.addIngressRule(ec2.Peer.ipv4('1.2.3.4/32'), ec2.Port.udp(1111));
fleet.addIngressRule(gamelift.Peer.ipv4('1.2.3.4/32'), gamelift.Port.udp(1111));
```

### Managing locations
Expand All @@ -258,44 +248,39 @@ deploy it to), but it can deploy resources to any number of GameLift supported
Regions. Select Regions based on where your players are located and your
latency needs.

By default, home region is used as default location.

```ts fixture=with-build
import * as gamelift from '@aws-cdk-lib/aws-gamelift';
By default, home region is used as default location but we can add new locations if needed and define desired capacity

```ts
declare const build: gamelift.Build;
new gamelift.BuildFleet(this, 'Game server fleet', {
content: build
});
```

but we can add new locations if needed and define desired capacity

```ts fixture=with-build
import * as gamelift from '@aws-cdk-lib/aws-gamelift';

declare const build: gamelift.Build;
// Locations can be added directly through constructor
const fleet = new gamelift.BuildFleet(this, 'Game server fleet', {
content: build,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C4, ec2.InstanceSize.LARGE),
runtimeConfiguration: {
serverProcesses: [{
launchPath: '/local/game/GameLiftExampleServer.x86_64',
}]
},
locations: [ {
name: 'eu-west-1',
desiredCapacity: 5,
minCapacity: 2,
maxCapacity: 10
region: 'eu-west-1',
capacity: {
desiredCapacity: 5,
minSize: 2,
maxSize: 10
}
}, {
name: 'us-east-1',
desiredCapacity: 5,
minCapacity: 2,
maxCapacity: 10
}
region: 'us-east-1',
capacity: {
desiredCapacity: 5,
minSize: 2,
maxSize: 10
}
}]
});

// Or through dedicated methods
const fleet = new gamelift.BuildFleet(this, 'Game server fleet', {
content: build
});
fleet.addLocation('eu-west-1', 5, 2, 10);
fleet.addLocation('us-east-1', 5, 2, 10);
fleet.addLocation('ap-southeast-1', 5, 2, 10);
```

### Specifying an IAM role
Expand All @@ -307,20 +292,25 @@ for GameLift to access your ressources. If you wish, you may
specify your own IAM role.

```ts
import * as iam from '@aws-cdk-lib/aws-iam';
import * as ec2 from '@aws-cdk-lib/aws-ec2';
import * as gamelift from '@aws-cdk-lib/aws-gamelift';

declare const build: gamelift.Build;
const role = new iam.Role(this, 'Role', {
assumedBy: new iam.CompositePrincipale(new iam.ServicePrincipal('gamelift.amazonaws.com'))
assumedBy: new iam.CompositePrincipal(new iam.ServicePrincipal('gamelift.amazonaws.com'))
});
role.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('CloudWatchAgentServerPolicy'));
role.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('CloudWatchAgentServerPolicy'));

new gamelift.BuildFleet(this, 'Game server fleet', {
content = build,
const fleet = new gamelift.BuildFleet(this, 'Game server fleet', {
content: build,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.C5, ec2.InstanceSize.LARGE),
runtimeConfiguration: {
serverProcesses: [{
launchPath: '/local/game/GameLiftExampleServer.x86_64',
}]
},
role: role
});

// Actions can also be grantted through dedicated method
fleet.grant(role, 'gamelift:ListFleets');
```

### Monitoring
Expand All @@ -346,17 +336,17 @@ to produce metric configurations for any metric provided by GameLift Fleet,
Game sessions or server processes; the configurations are pre-populated with
the correct dimensions for the matchmaking configuration.

```ts fixture=with-matchmaking-configuration
import * as cloudwatch from '@aws-cdk-lib/aws-cloudwatch';
```ts
declare const fleet: gamelift.BuildFleet;
// Alarm that triggers when the per-second average of not used instances exceed 10%
const instancesUsedRatio = new cloudwatch.MathExpression({
expression: '1 - (activeInstances / idleInstances)',
usingMetrics: {
activeInstances: fleet.metricActiveInstances({ statistic: cloudwatch.Statistic.SUM }),
idleInstances: fleet.metric('IdleInstances'),
activeInstances: fleet.metric('ActiveInstances', { statistic: cloudwatch.Statistic.SUM }),
idleInstances: fleet.metricIdleInstances(),
},
});
new Alarm(this, 'Alarm', {
new cloudwatch.Alarm(this, 'Alarm', {
metric: instancesUsedRatio,
threshold: 0.1,
evaluationPeriods: 3,
Expand Down
Loading