-
Notifications
You must be signed in to change notification settings - Fork 11
/
ecs.tf
238 lines (215 loc) · 7.4 KB
/
ecs.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
locals {
ecs_secrets_foundry_admin_key = length(aws_ssm_parameter.foundry_admin_key) > 0 ? {
name = "FOUNDRY_ADMIN_KEY"
valueFrom = element(aws_ssm_parameter.foundry_admin_key.*.arn, 0)
} : null
ecs_container_definition_foundry_server = [{
image = var.foundryvtt_docker_image
name = "foundry-server-${terraform.workspace}"
environment = [
{
name = "FOUNDRY_AWS_CONFIG"
value = "true"
}
]
logConfiguration = {
logDriver = "awslogs",
options = {
awslogs-create-group = "true"
awslogs-group = "foundry-server-${terraform.workspace}"
awslogs-region = local.region
awslogs-stream-prefix = "ecs-container"
}
}
mountPoints = [{
containerPath = "/data/Data"
sourceVolume = "foundry-data"
}]
portMappings = [{
hostPort = local.foundry_port
protocol = "tcp"
containerPort = local.foundry_port
}]
secrets = [
{
name = "FOUNDRY_USERNAME"
valueFrom = aws_ssm_parameter.foundry_username.arn
},
{
name = "FOUNDRY_PASSWORD"
valueFrom = aws_ssm_parameter.foundry_password.arn
},
local.ecs_secrets_foundry_admin_key
]
}]
ecs_container_availability_zones_stringified = format("[%s]", join(", ", local.server_availability_zones))
ecs_container_foundry_user_and_group_id = 421
}
resource "aws_security_group" "foundry_server" {
name_prefix = "foundry-server-sg-${terraform.workspace}"
revoke_rules_on_delete = true
tags = local.tags_rendered
vpc_id = aws_vpc.foundry.id
}
resource "aws_security_group_rule" "allow_foundry_port_ingress" {
from_port = local.foundry_port
protocol = "tcp"
security_group_id = aws_security_group.foundry_server.id
source_security_group_id = aws_security_group.foundry_load_balancer.id
to_port = local.foundry_port
type = "ingress"
}
resource "aws_security_group_rule" "allow_outbound" {
cidr_blocks = ["0.0.0.0/0"]
from_port = 0
protocol = "tcp"
security_group_id = aws_security_group.foundry_server.id
to_port = 65535
type = "egress"
}
resource "aws_ecs_cluster" "foundry_server" {
name = "foundry-server-${terraform.workspace}"
capacity_providers = ["FARGATE"]
tags = local.tags_rendered
setting {
name = "containerInsights"
value = "enabled"
}
}
resource "aws_ecs_service" "foundry_server" {
cluster = aws_ecs_cluster.foundry_server.id
desired_count = 1
health_check_grace_period_seconds = 120
launch_type = "FARGATE"
name = "foundry-server-${terraform.workspace}"
platform_version = "1.4.0"
task_definition = aws_ecs_task_definition.foundry_server.arn
lifecycle {
create_before_destroy = true
ignore_changes = [
desired_count
]
}
load_balancer {
target_group_arn = aws_lb_target_group.lb_foundry_server_http.arn
container_name = "foundry-server-${terraform.workspace}"
container_port = local.foundry_port
}
network_configuration {
assign_public_ip = false
security_groups = [aws_security_group.foundry_server.id]
subnets = local.subnet_private_ids
}
}
resource "aws_ecs_task_definition" "foundry_server" {
cpu = 1024
container_definitions = jsonencode(local.ecs_container_definition_foundry_server)
execution_role_arn = aws_iam_role.foundry_server.arn
family = "foundry-server-${terraform.workspace}"
memory = 2048
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
tags = local.tags_rendered
task_role_arn = aws_iam_role.foundry_server.arn
volume {
name = "foundry-data"
efs_volume_configuration {
file_system_id = aws_efs_file_system.foundry_server_data.id
transit_encryption = "ENABLED"
authorization_config {
access_point_id = aws_efs_access_point.foundry_server_data.id
iam = "ENABLED"
}
}
}
}
resource "aws_efs_file_system" "foundry_server_data" {
creation_token = "foundry-server-data-${terraform.workspace}"
encrypted = true
tags = local.tags_rendered
lifecycle_policy {
transition_to_ia = "AFTER_${var.artifacts_data_expiration_days}_DAYS"
}
}
data "aws_iam_policy_document" "foundry_data_efs" {
statement {
sid = "FoundryServerMountAccess"
actions = ["elasticfilesystem:ClientMount"]
resources = [aws_efs_file_system.foundry_server_data.arn]
principals {
type = "AWS"
identifiers = [aws_ecs_task_definition.foundry_server.task_role_arn]
}
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["true"]
}
}
statement {
sid = "FoundryServerWriteAccess"
actions = ["elasticfilesystem:ClientWrite"]
resources = [aws_efs_file_system.foundry_server_data.arn]
principals {
type = "AWS"
identifiers = [aws_ecs_task_definition.foundry_server.task_role_arn]
}
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["true"]
}
condition {
test = "StringEquals"
variable = "elasticfilesystem:AccessPointArn"
values = [aws_efs_access_point.foundry_server_data.arn]
}
}
}
resource "aws_efs_file_system_policy" "foundry_server_data" {
file_system_id = aws_efs_file_system.foundry_server_data.id
policy = data.aws_iam_policy_document.foundry_data_efs.json
}
resource "aws_efs_access_point" "foundry_server_data" {
file_system_id = aws_efs_file_system.foundry_server_data.id
root_directory {
path = "/data"
creation_info {
owner_gid = local.ecs_container_foundry_user_and_group_id
owner_uid = local.ecs_container_foundry_user_and_group_id
permissions = "770"
}
}
posix_user {
gid = local.ecs_container_foundry_user_and_group_id
uid = local.ecs_container_foundry_user_and_group_id
}
}
resource "aws_security_group" "foundry_data_mount" {
name_prefix = "foundry-data-mount-sg-${terraform.workspace}"
revoke_rules_on_delete = true
tags = local.tags_rendered
vpc_id = aws_vpc.foundry.id
}
resource "aws_security_group_rule" "foundry_data_mount_allow_nfs" {
from_port = 2049
protocol = "tcp"
security_group_id = aws_security_group.foundry_data_mount.id
source_security_group_id = aws_security_group.foundry_server.id
to_port = 2049
type = "ingress"
}
resource "aws_security_group_rule" "foundry_data_mount_allow_outbound" {
cidr_blocks = ["0.0.0.0/0"]
from_port = 0
protocol = "tcp"
security_group_id = aws_security_group.foundry_data_mount.id
to_port = 65535
type = "egress"
}
resource "aws_efs_mount_target" "private_subnets" {
count = length(local.subnet_private_ids)
file_system_id = aws_efs_file_system.foundry_server_data.id
security_groups = [aws_security_group.foundry_data_mount.id]
subnet_id = element(local.subnet_private_ids, count.index)
}