-
Notifications
You must be signed in to change notification settings - Fork 24
/
valheim-server-aws-cdk-stack.ts
163 lines (139 loc) · 5.41 KB
/
valheim-server-aws-cdk-stack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Annotations, CfnOutput, Stack, StackProps } from "aws-cdk-lib";
import { Port, SubnetType, Vpc } from "aws-cdk-lib/aws-ec2";
import { Cluster, Compatibility, ContainerImage, FargatePlatformVersion, FargateService, LogDrivers, MountPoint, NetworkMode, Protocol, Secret, TaskDefinition, Volume } from "aws-cdk-lib/aws-ecs";
import { FileSystem } from "aws-cdk-lib/aws-efs";
import { Bucket } from "aws-cdk-lib/aws-s3";
import { Secret as SecretsManagerSecret } from "aws-cdk-lib/aws-secretsmanager";
import { Construct } from "constructs";
interface ValheimServerAwsCdkStackProps extends StackProps {
/**
* Optional parameter if you want to have the server start with an existing world file.
*/
worldBootstrapLocation?: string;
/**
* The S3 bucket the world file exists in.
* REQURED if worldBootstrapLocation is set.
*/
worldResourcesBucket?: Bucket;
}
const ACTUAL_VALHEIM_WORLD_LOCATION = "/config/";
export class ValheimServerAwsCdkStack extends Stack {
readonly valheimService: FargateService;
readonly fargateCluster: Cluster;
constructor(scope: Construct, id: string, props?: ValheimServerAwsCdkStackProps) {
super(scope, id, props);
if (props && props.worldBootstrapLocation && !props.worldResourcesBucket) {
Annotations.of(this).addError("worldResourcesBucket must be set if worldBootstrapLocation is set!");
}
// MUST BE DEFINED BEFORE RUNNING CDK DEPLOY! Key Value should be: VALHEIM_SERVER_PASS
const valheimServerPass = SecretsManagerSecret.fromSecretNameV2(
this,
"predefinedValheimServerPass",
"valheimServerPass"
);
const vpc = new Vpc(this, "valheimVpc", {
cidr: "10.0.0.0/24",
subnetConfiguration: [
{
cidrMask: 24,
name: "valheimPublicSubnet",
subnetType: SubnetType.PUBLIC,
},
],
maxAzs: 1,
enableDnsSupport: true,
enableDnsHostnames: true,
});
this.fargateCluster = new Cluster(this, "fargateCluster", {
vpc: vpc,
});
const serverFileSystem = new FileSystem(this, "valheimServerStorage", {
vpc: vpc,
encrypted: true,
});
const serverVolumeConfig: Volume = {
name: "valheimServerVolume",
efsVolumeConfiguration: {
fileSystemId: serverFileSystem.fileSystemId,
},
};
const mountPoint: MountPoint = {
containerPath: "/config",
sourceVolume: serverVolumeConfig.name,
readOnly: false,
};
const valheimTaskDefinition = new TaskDefinition(
this,
"valheimTaskDefinition",
{
compatibility: Compatibility.FARGATE,
cpu: "2048",
memoryMiB: "4096",
volumes: [serverVolumeConfig],
networkMode: NetworkMode.AWS_VPC,
}
);
if (props && props.worldResourcesBucket) {
props.worldResourcesBucket.grantRead(valheimTaskDefinition.taskRole);
}
// Valheim server environment variables
// https://github.com/lloesche/valheim-server-docker#environment-variables
const environment: Record<string, string> = Object.entries(process.env)
.filter(([key]) => key.startsWith("VALHEIM_DOCKER_"))
.reduce((a, [k, v]) => ({ ...a, [k.replace("VALHEIM_DOCKER_", "")]: v}), {});
if (props && props.worldResourcesBucket) {
environment["PRE_SUPERVISOR_HOOK"] = "apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install awscli";
// TODO: Make this smarter. Eg, check BOOTSTRAP_WITH_WORLD_NAME and see if *that* world file already exsts. Or give an option to not overwrite with the data from S3.
environment["PRE_START_HOOK"] =
`if [[ ! -d /config/worlds_local/ ]]; then aws s3 cp --recursive s3://${props.worldResourcesBucket.bucketName}/ ${ACTUAL_VALHEIM_WORLD_LOCATION}; else echo "Skipping copy from S3 because /config/worlds_local/ already exists"; fi`;
Annotations.of(this).addInfo("World bootstrapping is configured, if the EFS file system already has a /config/worlds_local/ folder, then we will NOT bootstrap. This is to prevent overrwriting with the original bootstrap if the container restarts.");
}
const container = valheimTaskDefinition.addContainer("valheimContainer", {
image: ContainerImage.fromRegistry("lloesche/valheim-server"),
logging: LogDrivers.awsLogs({ streamPrefix: "ValheimServer" }),
environment,
secrets: {
SERVER_PASS: Secret.fromSecretsManager(
valheimServerPass,
"VALHEIM_SERVER_PASS"
),
},
});
container.addPortMappings(
{
containerPort: 2456,
hostPort: 2456,
protocol: Protocol.UDP,
},
{
containerPort: 2457,
hostPort: 2457,
protocol: Protocol.UDP,
},
{
containerPort: 2458,
hostPort: 2458,
protocol: Protocol.UDP,
}
);
container.addMountPoints(mountPoint);
this.valheimService = new FargateService(this, "valheimService", {
cluster: this.fargateCluster,
taskDefinition: valheimTaskDefinition,
desiredCount: 1,
assignPublicIp: true,
platformVersion: FargatePlatformVersion.VERSION1_4,
minHealthyPercent: 0,
enableExecuteCommand: true,
});
serverFileSystem.connections.allowDefaultPortFrom(this.valheimService);
this.valheimService.connections.allowFromAnyIpv4(
new Port({
protocol: Protocol.UDP,
stringRepresentation: "valheimPorts",
fromPort: 2456,
toPort: 2458,
})
);
}
}