-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathservice_dedup_api.tf
160 lines (140 loc) · 4.35 KB
/
service_dedup_api.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
locals {
deduplication_api = {
secrets = {
DEDUPLICATION_API_KEY = "/projects/deduplication-api/deduplication-api-key"
MILVUS_DB_URI = "/projects/deduplication-api/milvus-db-uri"
}
}
}
data "aws_secretsmanager_secret" "deduplication_api" {
for_each = local.deduplication_api.secrets
name = each.value
}
data "aws_secretsmanager_secret_version" "deduplication_api" {
for_each = local.deduplication_api.secrets
secret_id = data.aws_secretsmanager_secret.deduplication_api[each.key].id
}
resource "aws_secretsmanager_secret" "deduplication_api_env" {
name = "deduplication-prod-env"
}
resource "aws_secretsmanager_secret_version" "deduplication_api_env" {
secret_id = aws_secretsmanager_secret.deduplication_api_env.id
secret_string = jsonencode({
DEDUPLICATION_API_KEY : data.aws_secretsmanager_secret_version.deduplication_api["DEDUPLICATION_API_KEY"].secret_string
MILVUS_DB_URI : data.aws_secretsmanager_secret_version.deduplication_api["MILVUS_DB_URI"].secret_string
})
}
resource "aws_ecs_task_definition" "service-dedup-api-TD" {
family = "service-dedup-api-TD"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = 1024
memory = 2048
execution_role_arn = data.aws_iam_role.ecs_task_execution_role.arn
container_definitions = jsonencode([
{
name = "container-name"
image = "service-dedup-api"
cpu = 1024
memory = 2048
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-create-group = "true"
awslogs-group = "/ecs/service-dedup-api"
awslogs-region = var.region
awslogs-stream-prefix = "ecs"
}
}
essential = true
portMappings = [
{
containerPort = 80
hostPort = 80
}
]
}
])
}
resource "aws_lb_target_group" "service-dedup-api-tg" {
name = "service-dedup-api-tg"
port = 80
protocol = "HTTP"
target_type = "ip"
vpc_id = aws_vpc.vpc.id
health_check {
enabled = true
path = "/health-check"
port = 80
protocol = "HTTP"
}
tags = {
Name = "service-dedup-api-tg"
Environment = var.environment
}
}
resource "aws_ecs_service" "service-dedup-api-service" {
name = "service-dedup-api-service"
cluster = aws_ecs_cluster.base-cluster.id
task_definition = aws_ecs_task_definition.service-dedup-api-TD.id
desired_count = 1
depends_on = [
aws_ecs_cluster.base-cluster,
aws_ecs_task_definition.service-dedup-api-TD,
aws_lb_target_group.service-dedup-api-tg,
]
launch_type = "FARGATE"
network_configuration {
subnets = [aws_subnet.private-subnet-a.id, aws_subnet.private-subnet-b.id]
security_groups = [aws_security_group.service-sg.id]
assign_public_ip = true
}
load_balancer {
target_group_arn = aws_lb_target_group.service-dedup-api-tg.arn
container_name = "container-name"
container_port = 80
}
lifecycle {
ignore_changes = [task_definition]
}
}
resource "aws_lb_listener_rule" "service-dedup-api-rule" {
listener_arn = aws_lb_listener.service-dedup-api-alb-listener.arn
priority = 100
action {
type = "forward"
target_group_arn = aws_lb_target_group.service-dedup-api-tg.arn
}
condition {
path_pattern {
values = ["*"]
}
}
}
resource "aws_lb" "service-dedup-api-alb" {
name = "service-dedup-api-alb"
internal = false
load_balancer_type = "application"
security_groups = ["sg-09d6376212dfa6ea1"] // Todo change
subnets = [aws_subnet.public-subnet-a.id, aws_subnet.public-subnet-b.id]
enable_deletion_protection = true
tags = {
Name = "service-dedup-api-alb"
}
}
resource "aws_wafv2_web_acl_association" "service-dedup-api-alb" {
resource_arn = aws_lb.service-dedup-api-alb.arn
web_acl_arn = aws_wafv2_web_acl.generic.arn
}
resource "aws_lb_listener" "service-dedup-api-alb-listener" {
load_balancer_arn = aws_lb.service-dedup-api-alb.arn
port = "80"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.service-dedup-api-tg.arn
}
depends_on = [
aws_lb.service-dedup-api-alb
]
}