forked from covidgreen/covid-green-interoperability-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecs_interop.tf
176 lines (154 loc) · 5.81 KB
/
ecs_interop.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
# #########################################
# ECS General Resources
# #########################################
data "aws_iam_policy_document" "interop_ecs_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "interop_ecs_task_execution" {
name = "${module.labels.id}-interop-task-exec-role"
assume_role_policy = data.aws_iam_policy_document.interop_ecs_assume_role_policy.json
}
resource "aws_iam_role_policy_attachment" "interop_ecs_task_execution" {
role = aws_iam_role.interop_ecs_task_execution.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
resource "aws_iam_role" "interop_ecs_task_role" {
name = "${module.labels.id}-interop-task-role"
assume_role_policy = data.aws_iam_policy_document.interop_ecs_assume_role_policy.json
}
data "aws_iam_policy_document" "interop_ecs_task_policy" {
statement {
actions = ["ssm:GetParameter", "secretsmanager:GetSecretValue"]
resources = [
aws_ssm_parameter.log_level.arn,
aws_ssm_parameter.interop_port.arn,
aws_ssm_parameter.interop_host.arn,
aws_ssm_parameter.cors_origin.arn,
aws_ssm_parameter.db_host.arn,
aws_ssm_parameter.db_reader_host.arn,
aws_ssm_parameter.db_port.arn,
aws_ssm_parameter.db_database.arn,
aws_ssm_parameter.db_ssl.arn,
aws_ssm_parameter.batch_size.arn,
aws_ssm_parameter.batch_url.arn,
data.aws_secretsmanager_secret_version.rds.arn,
data.aws_secretsmanager_secret_version.jwt.arn
]
}
statement {
actions = ["sqs:*"]
resources = [
aws_sqs_queue.batch.arn
]
}
}
resource "aws_iam_policy" "interop_ecs_task_policy" {
name = "${module.labels.id}-ecs-interop-task-policy"
path = "/"
policy = data.aws_iam_policy_document.interop_ecs_task_policy.json
}
resource "aws_iam_role_policy_attachment" "interop_ecs_task_policy" {
role = aws_iam_role.interop_ecs_task_role.name
policy_arn = aws_iam_policy.interop_ecs_task_policy.arn
}
# #########################################
# Interop Service
# #########################################
data "template_file" "interop_service_container_definitions" {
template = file("templates/interop_service_task_definition.tpl")
vars = {
interop_image_uri = "${aws_ecr_repository.interop.repository_url}:latest"
config_var_prefix = local.config_var_prefix
migrations_image_uri = "${aws_ecr_repository.migrations.repository_url}:latest"
listening_port = var.interop_listening_port
logs_service_name = aws_cloudwatch_log_group.interop.name
log_group_region = var.aws_region
node_env = "production"
aws_region = var.aws_region
}
}
resource "aws_ecs_task_definition" "interop" {
family = "${module.labels.id}-interop"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
cpu = var.interop_services_task_cpu
memory = var.interop_services_task_memory
execution_role_arn = aws_iam_role.interop_ecs_task_execution.arn
task_role_arn = aws_iam_role.interop_ecs_task_role.arn
container_definitions = data.template_file.interop_service_container_definitions.rendered
tags = module.labels.tags
}
resource "aws_ecs_service" "interop" {
name = "${module.labels.id}-interop"
cluster = aws_ecs_cluster.services.id
launch_type = "FARGATE"
task_definition = aws_ecs_task_definition.interop.arn
desired_count = var.interop_service_desired_count
network_configuration {
security_groups = [module.interop_sg.id]
subnets = module.vpc.private_subnets
}
load_balancer {
target_group_arn = aws_lb_target_group.interop.id
container_name = "interop"
container_port = var.interop_listening_port
}
depends_on = [
aws_lb_listener.interop_http
]
lifecycle {
ignore_changes = [
task_definition,
desired_count
]
}
}
module "interop_autoscale" {
source = "./modules/ecs-autoscale-service"
ecs_cluster_resource_name = aws_ecs_cluster.services.name
service_resource_name = aws_ecs_service.interop.name
ecs_autoscale_max_instances = var.interop_ecs_autoscale_max_instances
ecs_autoscale_min_instances = var.interop_ecs_autoscale_min_instances
ecs_as_cpu_high_threshold = var.interop_cpu_high_threshold
ecs_as_cpu_low_threshold = var.interop_cpu_low_threshold
ecs_as_mem_high_threshold = var.interop_mem_high_threshold
ecs_as_mem_low_threshold = var.interop_mem_low_threshold
tags = module.labels.tags
}
# #########################################
# Interop log group
# #########################################
resource "aws_cloudwatch_log_group" "interop" {
name = "${module.labels.id}-interop"
retention_in_days = var.logs_retention_days
tags = module.labels.tags
lifecycle {
create_before_destroy = true
}
}
# #########################################
# Security group - Allow all access from LB
# #########################################
module "interop_sg" {
source = "./modules/security-group"
open_egress = true
name = "${module.labels.id}-interop"
environment = var.environment
vpc_id = module.vpc.vpc_id
tags = module.labels.tags
}
resource "aws_security_group_rule" "interop_ingress_http" {
description = "Allows backend services to accept connections from ALB"
type = "ingress"
from_port = 0
to_port = 65535
protocol = "tcp"
source_security_group_id = module.alb_interop_sg.id
security_group_id = module.interop_sg.id
}