-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtf_main.tf
More file actions
266 lines (229 loc) · 7.06 KB
/
tf_main.tf
File metadata and controls
266 lines (229 loc) · 7.06 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.0"
}
}
}
provider "aws" {
region = var.AWS_REGION
# Prevent Terraform from loading shared config files so that env vars are the single source of truth.
shared_config_files = ["${path.module}/.no_aws_config"]
shared_credentials_files = ["${path.module}/.no_aws_credentials"]
}
data "aws_availability_zones" "available" {
state = "available"
}
locals {
name_prefix = var.PROJECT_PREFIX
normalized_name_prefix = replace(lower(var.PROJECT_PREFIX), "_", "-")
ecr_repo_name = "${local.name_prefix}${var.ECR_REPOSITORY_NAME}"
cluster_name = "${local.name_prefix}cluster"
service_name = "${local.name_prefix}service"
task_family = "${local.name_prefix}task"
container_name = "${local.name_prefix}app"
log_group_name = "/aws/ecs/${local.name_prefix}app"
alb_name = trimsuffix(substr("${local.normalized_name_prefix}alb", 0, 32), "-")
target_group_name = trimsuffix(substr("${local.normalized_name_prefix}tg", 0, 32), "-")
execution_role_name = substr("${local.name_prefix}ecs-task-execution-role", 0, 64)
}
resource "aws_ecr_repository" "app" {
name = local.ecr_repo_name
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = var.ECR_IMAGE_SCAN_ON_PUSH
}
}
resource "aws_ecr_lifecycle_policy" "app" {
repository = aws_ecr_repository.app.name
policy = jsonencode({
rules = [
{
rulePriority = 1
description = "Keep only recent images"
selection = {
tagStatus = "any"
countType = "imageCountMoreThan"
countNumber = var.ECR_KEEP_IMAGE_COUNT
}
action = {
type = "expire"
}
}
]
})
}
resource "aws_vpc" "main" {
cidr_block = var.VPC_CIDR
enable_dns_support = true
enable_dns_hostnames = true
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
}
resource "aws_subnet" "public" {
count = length(var.PUBLIC_SUBNET_CIDRS)
vpc_id = aws_vpc.main.id
cidr_block = var.PUBLIC_SUBNET_CIDRS[count.index]
availability_zone = data.aws_availability_zones.available.names[count.index % length(data.aws_availability_zones.available.names)]
map_public_ip_on_launch = true
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
}
resource "aws_route_table_association" "public" {
count = length(var.PUBLIC_SUBNET_CIDRS)
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
resource "aws_security_group" "alb" {
name = "${local.name_prefix}alb-sg"
description = "Allow inbound HTTP traffic from the internet"
vpc_id = aws_vpc.main.id
ingress {
description = "HTTP from internet"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
description = "Allow all outbound traffic"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "ecs_tasks" {
name = "${local.name_prefix}ecs-tasks-sg"
description = "Allow inbound app traffic from the ALB"
vpc_id = aws_vpc.main.id
ingress {
description = "App traffic from ALB"
from_port = var.CONTAINER_PORT
to_port = var.CONTAINER_PORT
protocol = "tcp"
security_groups = [aws_security_group.alb.id]
}
egress {
description = "Allow all outbound traffic"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_cloudwatch_log_group" "ecs" {
name = local.log_group_name
retention_in_days = var.ECS_LOGS_RETENTION_DAYS
}
resource "aws_iam_role" "ecs_task_execution" {
name = local.execution_role_name
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = "sts:AssumeRole"
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
}
]
})
}
resource "aws_iam_role_policy_attachment" "ecs_task_execution_default" {
role = aws_iam_role.ecs_task_execution.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
resource "aws_ecs_cluster" "main" {
name = local.cluster_name
}
resource "aws_ecs_task_definition" "app" {
family = local.task_family
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = tostring(var.ECS_TASK_CPU)
memory = tostring(var.ECS_TASK_MEMORY)
execution_role_arn = aws_iam_role.ecs_task_execution.arn
runtime_platform {
cpu_architecture = lower(var.CAT_ARCHITECTURE) == "arm64" ? "ARM64" : "X86_64"
operating_system_family = "LINUX"
}
container_definitions = jsonencode([
{
name = local.container_name
image = "${aws_ecr_repository.app.repository_url}:${var.DOCKER_IMAGE_TAG}"
essential = true
portMappings = [
{
containerPort = var.CONTAINER_PORT
hostPort = var.CONTAINER_PORT
protocol = "tcp"
}
]
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = aws_cloudwatch_log_group.ecs.name
awslogs-region = var.AWS_REGION
awslogs-stream-prefix = "ecs"
}
}
}
])
depends_on = [aws_iam_role_policy_attachment.ecs_task_execution_default]
}
resource "aws_lb" "main" {
name = local.alb_name
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb.id]
subnets = aws_subnet.public[*].id
}
resource "aws_lb_target_group" "app" {
name = local.target_group_name
port = var.CONTAINER_PORT
protocol = "HTTP"
target_type = "ip"
vpc_id = aws_vpc.main.id
health_check {
enabled = true
path = var.HEALTHCHECK_PATH
matcher = "200"
protocol = "HTTP"
}
}
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.main.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.app.arn
}
}
resource "aws_ecs_service" "app" {
name = local.service_name
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.app.arn
desired_count = var.ECS_DESIRED_COUNT
launch_type = "FARGATE"
network_configuration {
subnets = aws_subnet.public[*].id
security_groups = [aws_security_group.ecs_tasks.id]
assign_public_ip = true
}
load_balancer {
target_group_arn = aws_lb_target_group.app.arn
container_name = local.container_name
container_port = var.CONTAINER_PORT
}
depends_on = [aws_lb_listener.http]
}