-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
289 lines (249 loc) · 7.13 KB
/
main.tf
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
locals {
ecs_cluster_name = "Valheim"
}
#######################################
# VPC
#######################################
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "Valheim-VPC"
cidr = var.vpc_cidr
azs = [var.availability_zone]
public_subnets = [var.subnet_cidr]
enable_dns_hostnames = true
enable_nat_gateway = false
}
#######################################
# EFS
#######################################
resource "aws_efs_file_system" "valheim" {
creation_token = "valheim"
performance_mode = "generalPurpose"
throughput_mode = "bursting"
encrypted = "true"
lifecycle {
prevent_destroy = true
}
}
resource "aws_efs_mount_target" "valheim" {
file_system_id = aws_efs_file_system.valheim.id
subnet_id = module.vpc.public_subnets[0]
security_groups = [aws_security_group.efs.id]
}
resource "aws_security_group" "efs" {
name = "EFS"
vpc_id = module.vpc.vpc_id
ingress {
description = "EFS"
from_port = 2049
to_port = 2049
protocol = "tcp"
cidr_blocks = [module.vpc.vpc_cidr_block]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
#######################################
# EC2
#######################################
data "aws_ssm_parameter" "ecs_ami" {
name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
resource "aws_instance" "valheim_server" {
ami = data.aws_ssm_parameter.ecs_ami.value
instance_type = var.instance_type
key_name = var.key_name
subnet_id = module.vpc.public_subnets[0]
vpc_security_group_ids = [aws_security_group.valheim_server.id]
iam_instance_profile = aws_iam_instance_profile.ecs.name
user_data = <<EOF
#!/bin/bash
echo "ECS_CLUSTER=${local.ecs_cluster_name}" >> /etc/ecs/ecs.config
EOF
tags = {
Name = "Valheim Server"
tostop = var.enable_scheduled_shutdown || var.enable_scheduled_startup ? "true" : "false"
}
}
resource "aws_eip" "ip" {
instance = aws_instance.valheim_server.id
vpc = true
}
resource "aws_security_group" "valheim_server" {
name = "Valheim Server"
description = "Allow SSH & Valheim server traffic"
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Valheim"
from_port = 2456
to_port = 2458
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
#######################################
# IAM Role for ECS Instances
#######################################
resource "aws_iam_instance_profile" "ecs" {
name = "ecsInstanceRoleProfile"
role = aws_iam_role.ecs_instance_role.name
}
resource "aws_iam_role" "ecs_instance_role" {
name = "ecsInstanceRole"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "ecs_policy" {
role = aws_iam_role.ecs_instance_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}
#######################################
# Scheduled startup/shutdown
#######################################
module "stop_ec2_instance" {
count = var.enable_scheduled_shutdown ? 1 : 0
source = "diodonfrost/lambda-scheduler-stop-start/aws"
name = "ec2_stop"
cloudwatch_schedule_expression = "cron(${var.shutdown_schedule_expression})"
schedule_action = "stop"
ec2_schedule = "true"
scheduler_tag = {
key = "tostop"
value = "true"
}
}
module "start_ec2_instance" {
count = var.enable_scheduled_startup ? 1 : 0
source = "diodonfrost/lambda-scheduler-stop-start/aws"
name = "ec2_start"
cloudwatch_schedule_expression = "cron(${var.startup_schedule_expression})"
schedule_action = "start"
ec2_schedule = "true"
scheduler_tag = {
key = "tostop"
value = "true"
}
}
#######################################
# ECS
#######################################
resource "aws_ecs_cluster" "valheim_cluster" {
name = local.ecs_cluster_name
}
resource "aws_ecs_service" "valheim_service" {
name = "valheim"
cluster = aws_ecs_cluster.valheim_cluster.id
task_definition = aws_ecs_task_definition.valheim_task.arn
desired_count = 1
}
module "valheim_container_definitions" {
source = "cloudposse/ecs-container-definition/aws"
version = "0.53.0"
container_name = "valheim_server"
container_image = "mbround18/valheim:${var.container_image_tag}"
essential = true
container_cpu = var.task_cpu
container_memory = var.task_memory
map_environment = {
NAME : var.server_name
WORLD : var.server_world
PASSWORD : var.server_password
PUBLIC : var.server_public
TZ : var.server_tz
WEBHOOK_URL : var.server_webhook_url
AUTO_UPDATE : var.server_auto_update
AUTO_UPDATE_SCHEDULE : var.server_auto_update_schedule
AUTO_BACKUP : var.server_auto_backup
AUTO_BACKUP_SCHEDULE : var.server_auto_backup_schedule
AUTO_BACKUP_REMOVE_OLD : var.server_auto_backup_remove_old
AUTO_BACKUP_DAYS_TO_LIVE : var.server_auto_backup_days_to_live
AUTO_BACKUP_ON_UPDATE : var.server_auto_backup_on_update
UPDATE_ON_STARTUP : var.server_update_on_startup
AUTO_BACKUP_ON_SHUTDOWN : var.server_auto_backup_on_shutdown
}
port_mappings = [
{
containerPort = 2456
hostPort = 2456
protocol = "udp"
},
{
containerPort = 2457
hostPort = 2457
protocol = "udp"
},
{
containerPort = 2458
hostPort = 2458
protocol = "udp"
}
]
mount_points = [
{
sourceVolume : "saves",
containerPath : "/home/steam/.config/unity3d/IronGate/Valheim"
},
{
sourceVolume : "server"
containerPath : "/home/steam/valheim"
},
{
sourceVolume : "backups"
containerPath : "/home/steam/backups"
}
]
}
resource "aws_ecs_task_definition" "valheim_task" {
family = "valheim_server"
container_definitions = "[${module.valheim_container_definitions.json_map_encoded}]"
volume {
name = "saves"
efs_volume_configuration {
file_system_id = aws_efs_file_system.valheim.id
root_directory = "/saves"
}
}
volume {
name = "server"
efs_volume_configuration {
file_system_id = aws_efs_file_system.valheim.id
root_directory = "/server"
}
}
volume {
name = "backups"
efs_volume_configuration {
file_system_id = aws_efs_file_system.valheim.id
root_directory = "/backups"
}
}
}