Skip to content

Commit d233caf

Browse files
committed
fix: 테스크 데이터 조합을 최상위에서 하도록 변경
1 parent e2a2921 commit d233caf

File tree

8 files changed

+136
-167
lines changed

8 files changed

+136
-167
lines changed

terraform/dev/ecs/locals.tf

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -9,127 +9,4 @@ locals {
99
}
1010

1111
deployment_controller_type = "ECS"
12-
13-
resolved_task_definitions = {
14-
for name, def in var.ecs_task_definitions :
15-
name => name == "api-dev" ? merge(def, {
16-
task_definition_name = "api-dev"
17-
container_image = "${var.ecr_repo_urls["dev"]}:placeholder"
18-
task_role_arn = def.task_role_arn
19-
execution_role_arn = def.execution_role_arn
20-
environment = {}
21-
secrets = []
22-
}) : name == "mysql-dev" ? merge(def, {
23-
task_definition_name = "mysql-dev"
24-
container_image = "mysql:8"
25-
task_role_arn = def.task_role_arn
26-
execution_role_arn = def.execution_role_arn
27-
environment = {
28-
MYSQL_DATABASE = "eatda"
29-
}
30-
secrets = [
31-
{
32-
name = "MYSQL_USER"
33-
valueFrom = "/dev/MYSQL_USER_NAME"
34-
},
35-
{
36-
name = "MYSQL_ROOT_PASSWORD"
37-
valueFrom = "/dev/MYSQL_ROOT_PASSWORD"
38-
},
39-
{
40-
name = "MYSQL_PASSWORD"
41-
valueFrom = "/dev/MYSQL_PASSWORD"
42-
}
43-
]
44-
}) : merge(def, {
45-
task_definition_name = "dummy"
46-
container_image = "dummy"
47-
task_role_arn = def.task_role_arn
48-
execution_role_arn = def.execution_role_arn
49-
environment = {
50-
MYSQL_DATABASE = "dummy"
51-
}
52-
secrets = [
53-
{
54-
name = "DUMMY"
55-
valueFrom = "/dummy"
56-
},
57-
{
58-
name = "DUMMY"
59-
valueFrom = "/dummy"
60-
},
61-
{
62-
name = "DUMMY"
63-
valueFrom = "/dummy"
64-
}
65-
]
66-
})
67-
}
68-
69-
resolved_ecs_services = {
70-
for name, def in var.ecs_services : name => {
71-
name = name
72-
iam_role_arn = var.ecs_task_definitions[name].task_role_arn
73-
load_balancer = try(def.load_balancer, null)
74-
}
75-
}
76-
77-
container_definitions_map = {
78-
for svc, def in local.resolved_task_definitions : svc => [
79-
{
80-
name = def.task_definition_name
81-
image = def.container_image
82-
cpu = def.cpu
83-
memory = def.memory
84-
essential = true
85-
stopTimeout = lookup(def, "stop_timeout", var.default_stop_timeout)
86-
command = svc == "api-dev" ? [
87-
"java",
88-
"-Dspring.profiles.active=dev",
89-
"-jar",
90-
"/api.jar"
91-
] : null
92-
93-
portMappings = [
94-
for idx, port in def.container_port : {
95-
name = "${svc}-${port}-tcp"
96-
containerPort = port
97-
hostPort = def.host_port[idx]
98-
protocol = lookup(def, "protocol", var.default_protocol)
99-
}
100-
]
101-
102-
environment = [
103-
for k, v in def.environment : {
104-
name = k
105-
value = v
106-
}
107-
]
108-
109-
secrets = [
110-
for s in def.secrets : {
111-
name = s.name
112-
valueFrom = s.valueFrom
113-
}
114-
]
115-
116-
mountPoints = [
117-
for vol in (def.volumes != null ? def.volumes : []) : {
118-
sourceVolume = vol.name
119-
containerPath = lookup(var.volume_mount_paths, vol.name, "/eatda")
120-
readOnly = false
121-
}
122-
]
123-
}
124-
]
125-
}
126-
127-
final_task_definitions = {
128-
for key, task_def in local.resolved_task_definitions : key => merge(
129-
task_def,
130-
{
131-
container_definitions = local.container_definitions_map[key]
132-
}
133-
)
134-
}
13512
}

terraform/dev/ecs/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ module "service" {
1919

2020
module "task" {
2121
source = "./task"
22-
ecs_task_definitions = local.final_task_definitions
22+
ecs_task_definitions = var.ecs_task_definitions
2323
tags = var.tags
2424
}

terraform/dev/ecs/task/variables.tf

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
11
variable "ecs_task_definitions" {
2-
description = "Resolved task definition map (with image, roles, etc)"
2+
description = "A strictly-typed, unified map for all task definition properties."
3+
34
type = map(object({
45
cpu = number
56
memory = number
67
network_mode = string
7-
container_image = string
88
task_role_arn = string
99
execution_role_arn = string
1010
requires_compatibilities = list(string)
11-
container_port = list(number)
12-
host_port = list(number)
1311
volumes = optional(list(object({
1412
name = string
1513
host_path = string
1614
})), [])
17-
environment = optional(map(string), {})
18-
container_definitions = any
15+
16+
container_definitions = list(object({
17+
name = string
18+
image = string
19+
cpu = number
20+
memory = number
21+
essential = bool
22+
stopTimeout = number
23+
24+
command = optional(list(string))
25+
26+
portMappings = list(object({
27+
name = string
28+
containerPort = number
29+
hostPort = number
30+
protocol = string
31+
}))
32+
33+
environment = optional(list(object({
34+
name = string
35+
value = string
36+
})), [])
37+
38+
secrets = optional(list(object({
39+
name = string
40+
valueFrom = string
41+
})), [])
42+
43+
mountPoints = optional(list(object({
44+
sourceVolume = string
45+
containerPath = string
46+
readOnly = bool
47+
})), [])
48+
}))
1949
}))
2050
}
2151

terraform/dev/ecs/variables.tf

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,63 @@ variable "ecr_repo_urls" {
66
type = map(string)
77
}
88

9+
variable "ecs_services" {
10+
type = map(object({
11+
load_balancer = optional(object({
12+
target_group_key = string
13+
container_name = string
14+
container_port = number
15+
}))
16+
}))
17+
}
18+
919
variable "ecs_task_definitions" {
20+
description = "A strictly-typed, unified map for all task definition properties."
21+
1022
type = map(object({
1123
cpu = number
1224
memory = number
1325
network_mode = string
14-
container_port = list(number)
15-
host_port = list(number)
16-
environment = map(string)
17-
container_image = string
18-
execution_role_arn = string
1926
task_role_arn = string
27+
execution_role_arn = string
2028
requires_compatibilities = list(string)
21-
volumes = list(object({
29+
volumes = optional(list(object({
2230
name = string
2331
host_path = string
24-
}))
25-
}))
26-
}
32+
})), [])
2733

28-
variable "ecs_services" {
29-
type = map(object({
30-
load_balancer = optional(object({
31-
target_group_key = string
32-
container_name = string
33-
container_port = number
34+
container_definitions = list(object({
35+
name = string
36+
image = string
37+
cpu = number
38+
memory = number
39+
essential = bool
40+
stopTimeout = number
41+
42+
command = optional(list(string))
43+
44+
portMappings = list(object({
45+
name = string
46+
containerPort = number
47+
hostPort = number
48+
protocol = string
49+
}))
50+
51+
environment = optional(list(object({
52+
name = string
53+
value = string
54+
})), [])
55+
56+
secrets = optional(list(object({
57+
name = string
58+
valueFrom = string
59+
})), [])
60+
61+
mountPoints = optional(list(object({
62+
sourceVolume = string
63+
containerPath = string
64+
readOnly = bool
65+
})), [])
3466
}))
3567
}))
3668
}

terraform/dev/locals.tf

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,53 +23,77 @@ locals {
2323
environment = "dev"
2424
name_prefix = "eatda"
2525

26-
ec2_sg_id = data.terraform_remote_state.common.outputs.security_group_ids["ec2"]
27-
instance_definitions = data.terraform_remote_state.common.outputs.instance_profile_name["ec2-to-ecs"]
28-
instance_subnet_map = data.terraform_remote_state.common.outputs.public_subnet_ids
29-
ecr_repo_urls = data.terraform_remote_state.bootstrap.outputs.ecr_repo_urls
30-
ecs_services = var.ecs_services
31-
ecs_task_definitions_base = var.ecs_task_definitions_base
32-
alb_target_group_arns = data.terraform_remote_state.common.outputs.target_group_arns
26+
ec2_sg_id = data.terraform_remote_state.common.outputs.security_group_ids["ec2"]
27+
instance_subnet_map = data.terraform_remote_state.common.outputs.public_subnet_ids # 에러가 났던 부분
28+
ecr_repo_urls = data.terraform_remote_state.bootstrap.outputs.ecr_repo_urls
29+
ecs_services = var.ecs_services
30+
alb_target_group_arns = data.terraform_remote_state.common.outputs.target_group_arns
3331

3432
common_tags = {
3533
Project = local.project_name
3634
ManagedBy = "terraform"
3735
}
38-
}
3936

40-
locals {
4137
dev_instance_definitions = {
4238
ami = "ami-012ea6058806ff688"
4339
instance_type = "t2.micro"
4440
role = "dev"
45-
iam_instance_profile = "ec2-to-ecs"
41+
iam_instance_profile = data.terraform_remote_state.common.outputs.instance_profile_name["ec2-to-ecs"]
4642
key_name = "eatda-ec2-dev-key"
4743
user_data = <<-EOF
4844
#!/bin/bash
4945
echo ECS_CLUSTER=dev-cluster >> /etc/ecs/ecs.config
50-
5146
fallocate -l 2G /swapfile
5247
chmod 600 /swapfile
5348
mkswap /swapfile
5449
swapon /swapfile
55-
5650
echo '/swapfile none swap sw 0 0' >> /etc/fstab
5751
EOF
5852
}
59-
}
6053

61-
locals {
62-
ecs_task_definitions = {
54+
task_definitions_with_roles = {
6355
for k, v in var.ecs_task_definitions_base :
6456
k => merge(
6557
v,
6658
{
6759
execution_role_arn = data.terraform_remote_state.common.outputs.role_arn["ecsTaskExecutionRole"],
6860
task_role_arn = data.terraform_remote_state.common.outputs.role_arn["ecsAppTaskRole"]
69-
},
70-
k == "api-dev" ? {
71-
container_image = "${data.terraform_remote_state.bootstrap.outputs.ecr_repo_urls["dev"]}:placeholder"
72-
} : {},
61+
}
62+
)
63+
}
64+
65+
container_definitions_map = {
66+
for svc, def in local.task_definitions_with_roles : svc => [
67+
{
68+
name = svc
69+
image = svc == "api-dev" ? "${local.ecr_repo_urls["dev"]}:placeholder" : def.container_image
70+
cpu = def.cpu
71+
memory = def.memory
72+
essential = true
73+
stopTimeout = lookup(def, "stop_timeout", 30)
74+
command = svc == "api-dev" ? ["java", "-Dspring.profiles.active=dev", "-jar", "/api.jar"] : null
75+
portMappings = [
76+
for idx, port in def.container_port :
77+
{ name = "${svc}-${port}-tcp", containerPort = port, hostPort = def.host_port[idx], protocol = "tcp" }
78+
]
79+
environment = [for k, v in lookup(def, "environment", {}) : { name = k, value = v }]
80+
secrets = [for s in lookup(def, "secrets", []) : { name = s.name, valueFrom = s.valueFrom }]
81+
mountPoints = [
82+
for vol in lookup(def, "volumes", []) :{
83+
sourceVolume = vol.name, containerPath = lookup(var.volume_mount_paths, vol.name, "/eatda"),
84+
readOnly = false
85+
}
86+
]
87+
}
88+
]
89+
}
90+
91+
final_ecs_definitions_for_module = {
92+
for key, task_def in local.task_definitions_with_roles : key => merge(
93+
task_def,
94+
{
95+
container_definitions = local.container_definitions_map[key]
96+
}
7397
)
7498
}
7599
}

terraform/dev/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module "ecs" {
1212
alb_target_group_arns = local.alb_target_group_arns
1313
ecr_repo_urls = local.ecr_repo_urls
1414
ecs_services = var.ecs_services
15-
ecs_task_definitions = local.ecs_task_definitions
15+
ecs_task_definitions = local.final_ecs_definitions_for_module
1616
environment = local.environment
1717
tags = local.common_tags
1818
}

terraform/dev/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
output "ecs_task_definitions_check" {
2-
value = local.ecs_task_definitions
2+
value = local.final_ecs_definitions_for_module
33
}
44

55
output "ecr_repository_name" {

terraform/dev/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ variable "ecs_task_definitions_base" {
2525
host_path = string
2626
}))
2727
}))
28-
}
28+
}
29+
30+
variable "volume_mount_paths" {
31+
description = "A map of volume names to their mount paths inside the container."
32+
type = map(string)
33+
default = {}
34+
}

0 commit comments

Comments
 (0)