-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathecs.tf
217 lines (184 loc) · 4.82 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
module "ecs_cluster" {
source = "terraform-aws-modules/ecs/aws//modules/cluster"
version = "~> 5.6"
cluster_name = var.name
fargate_capacity_providers = {
FARGATE = {}
FARGATE_SPOT = {}
}
tags = var.tags
}
module "ecs_service" {
source = "terraform-aws-modules/ecs/aws//modules/service"
version = "~> 5.6"
name = var.name
cluster_arn = module.ecs_cluster.arn
cpu = 1024
memory = 2048
# supports external task def deployments
# by ignoring changes to task definition and desired count
ignore_task_definition_changes = true
desired_count = 1
# Task Definition
enable_execute_command = false
container_definitions = {
(var.container_name) = {
image = var.image
port_mappings = [
{
protocol = "tcp",
containerPort = var.container_port
}
]
environment = [
{
"name" : "PORT",
"value" : "${var.container_port}"
},
{
"name" : "HEALTHCHECK",
"value" : "${var.health_check}"
},
{
"name" : "POSTGRES_DB"
"value" : local.database_name
},
{
"name" : "POSTGRES_HOST"
"value" : module.aurora_postgres.cluster_endpoint
},
{
"name" : "KNOWLEDGE_BASE_ID",
"value" : aws_bedrockagent_knowledge_base.main.id
},
{
"name" : "DB_SECRET_ARN",
"value" : local.aurora_secret_arn
},
{
"name" : "OTEL_RESOURCE_ATTRIBUTES",
"value" : "service.namespace=${var.name},service.name=orchestrator"
},
]
readonly_root_filesystem = false
dependsOn = [
{
containerName = "otel"
condition = "HEALTHY"
}
]
},
otel = {
image = "public.ecr.aws/aws-observability/aws-otel-collector:v0.41.2"
command = ["--config=/etc/ecs/ecs-default-config.yaml"]
healthCheck = {
command = ["/healthcheck"]
interval = 5
timeout = 6
retries = 5
startPeriod = 1
}
},
}
load_balancer = {
service = {
target_group_arn = module.alb.target_groups["ecs-task"].arn
container_name = var.container_name
container_port = var.container_port
}
}
subnet_ids = module.vpc.private_subnets
security_group_rules = {
ingress_alb_service = {
type = "ingress"
from_port = var.container_port
to_port = var.container_port
protocol = "tcp"
description = "Service port"
source_security_group_id = module.alb.security_group_id
}
egress_all = {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
tasks_iam_role_name = "${var.name}-tasks"
tasks_iam_role_description = "role for ${var.name}"
tasks_iam_role_statements = [
{
actions = ["secretsmanager:GetSecretValue"]
resources = [local.aurora_secret_arn]
},
{
actions = ["bedrock:Retrieve"]
resources = ["arn:aws:bedrock:${local.region}:${local.account_id}:knowledge-base/*"]
},
{
actions = ["bedrock:InvokeModel"]
resources = ["arn:aws:bedrock:${local.region}::foundation-model/*"]
},
{
actions = [
"xray:PutTraceSegments",
"xray:PutTelemetryRecords"
]
resources = ["*"]
}
]
tags = var.tags
}
module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 9.0"
name = var.name
enable_deletion_protection = false
vpc_id = module.vpc.vpc_id
subnets = module.vpc.public_subnets
security_group_ingress_rules = {
all_http = {
from_port = 80
to_port = 80
ip_protocol = "tcp"
description = "HTTP web traffic"
cidr_ipv4 = "0.0.0.0/0"
}
}
security_group_egress_rules = { for cidr_block in module.vpc.private_subnets_cidr_blocks :
(cidr_block) => {
ip_protocol = "-1"
cidr_ipv4 = cidr_block
}
}
listeners = {
http = {
port = "80"
protocol = "HTTP"
forward = {
target_group_key = "ecs-task"
}
}
}
target_groups = {
ecs-task = {
backend_protocol = "HTTP"
backend_port = var.container_port
target_type = "ip"
health_check = {
enabled = true
healthy_threshold = 5
interval = 10
matcher = "200-299"
path = var.health_check
port = "traffic-port"
protocol = "HTTP"
timeout = 5
unhealthy_threshold = 2
}
create_attachment = false
}
}
tags = var.tags
}