-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs_task.tf
189 lines (171 loc) · 5.02 KB
/
ecs_task.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
resource "aws_ecs_task_definition" "rust_service" {
family = var.ecs_container_name
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
cpu = "512" # 0.5 vCPU
memory = "1024" # 1024 MB
container_definitions = jsonencode([
{
name = var.ecs_db_migaration
cpu = 256
memory = 512
essential = false
image = "${aws_ecr_repository.db_repository.repository_url}:latest"
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = aws_cloudwatch_log_group.app_group.name
awslogs-region = var.aws_region
awslogs-stream-prefix = aws_cloudwatch_log_stream.db_migration_stream.name
}
}
environment = [
{
name = "DATABASE_URL"
value = "postgres://${local.secret_RDS_USERNAME}:${local.secret_RDS_PASSWORD}@${aws_db_instance.postgres_db.endpoint}/${local.secret_RDS_DB_NAME}"
},
]
},
{
name = var.ecs_container_name
cpu = 256
memory = 512
essential = true
image = "${aws_ecr_repository.my_repository.repository_url}:latest"
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = aws_cloudwatch_log_group.app_group.name
awslogs-region = var.aws_region
awslogs-stream-prefix = aws_cloudwatch_log_stream.app_service_stream.name
}
},
portMappings = [
{
containerPort = 3001
hostPort = 3001
protocol = "tcp"
}
],
depends_on = [
{
containerName = var.ecs_db_migaration
condition = "SUCCESS"
}
],
environment = [
{
name = "DATABASE_URL"
value = "postgres://${local.secret_RDS_USERNAME}:${local.secret_RDS_PASSWORD}@${aws_db_instance.postgres_db.endpoint}/${local.secret_RDS_DB_NAME}"
},
{
name = "JWT_ACCESS_SECRET"
value = local.secret_JWT_ACCESS_SECRET
},
{
name = "JWT_REFRESH_SECRET"
value = local.secret_JWT_REFRESH_SECRET
},
{
name = "OPEN_AI_API_KEY"
value = local.secret_OPEN_AI_API_KEY
},
{
name = "CLOUDFLARE_AI_API_KEY"
value = local.secret_CLOUDFLARE_AI_API_KEY
},
{
name = "CLOUDFLARE_ACCOUNT"
value = local.secret_CLOUDFLARE_ACCOUNT
},
{
name = "GOOGLE_VISION_API_KEY"
value = local.secret_GOOGLE_VISION_API_KEY
},
{
name = "GOOGLE_PALM2_API_KEY"
value = local.secret_GOOGLE_PALM2_API_KEY
},
]
}
])
}
resource "aws_ecs_service" "app_service" {
depends_on = [aws_lb.app_lb]
name = var.ecs_service_name
cluster = aws_ecs_cluster.app_cluster.id
task_definition = aws_ecs_task_definition.rust_service.arn
desired_count = 1
launch_type = "FARGATE"
deployment_maximum_percent = 100
deployment_minimum_healthy_percent = 0
load_balancer {
target_group_arn = aws_lb_target_group.app_target_group.arn
container_name = var.ecs_container_name
container_port = 3001
}
network_configuration {
subnets = [
aws_subnet.public_subnet_a.id,
aws_subnet.public_subnet_b.id
]
security_groups = [
aws_security_group.default_sg.id,
]
assign_public_ip = true
}
}
resource "aws_lb_target_group" "app_target_group" {
name = "app-tg"
port = 3001
protocol = "HTTP"
target_type = "ip"
vpc_id = aws_vpc.shared_vpc.id
health_check {
path = "/api/ping"
protocol = "HTTP"
port = "traffic-port"
interval = 120
timeout = 30
healthy_threshold = 3
unhealthy_threshold = 3
matcher = "200-399"
}
}
resource "aws_lb_listener" "app_listener" {
load_balancer_arn = aws_lb.app_lb.arn
port = 80
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
resource "aws_lb_listener" "app_ssl_listener" {
load_balancer_arn = aws_lb.app_lb.arn
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = data.aws_acm_certificate.getthedeck.arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.app_target_group.arn
}
}
resource "aws_lb_listener_rule" "getthedeck_rule" {
listener_arn = aws_lb_listener.app_ssl_listener.arn
action {
type = "forward"
target_group_arn = aws_lb_target_group.app_target_group.arn
}
condition {
host_header {
values = [var.getthedeck_api_domain]
}
}
}