-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
126 lines (120 loc) · 4.37 KB
/
main.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
provider "aws" {
region = "us-west-2"
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.58.0"
}
}
required_version = ">= 1.0"
}
# Data
data "aws_region" "current" {}
# Resources
resource "aws_ecs_cluster" "ecs_cluster" {
name = lower("${var.app_name}-cluster")
}
# ECS Services
resource "aws_ecs_service" "service" {
for_each = var.ecs_services
name = "${each.key}-service"
cluster = aws_ecs_cluster.ecs_cluster.id
task_definition = aws_ecs_task_definition.ecs_task_definition[each.key].arn
launch_type = "FARGATE"
desired_count = each.value.desired_count
network_configuration {
subnets = each.value.is_public == true ? var.public_subnet_ids : var.private_subnet_ids
assign_public_ip = each.value.is_public
security_groups = var.security_group_ids
}
load_balancer {
target_group_arn = var.target_group_arns[each.key].arn
container_name = each.key
container_port = each.value.container_port
}
}
# ECS Task Definitions
resource "aws_ecs_task_definition" "ecs_task_definition" {
for_each = var.ecs_services
family = "${lower(var.app_name)}-${each.key}"
execution_role_arn = var.ecs_role_arn
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
memory = each.value.memory
cpu = each.value.cpu
container_definitions = jsonencode([
{
name = each.key
image = each.value.image
cpu = each.value.cpu
memory = each.value.memory
essential = true
environment = [
{ name = "INTERNAL_ALB", value = var.internal_alb_dns },
{ name = "SERVICE_HOST", value = var.internal_alb_dns },
{ name = "SERVER_SERVLET_CONTEXT_PATH", value = each.value.is_public == true ? "/" : "/${each.key}" },
{ name = "SERVICES", value = "backend" },
{ name = "SERVICE", value = each.key },
{ name = "SERVICE_NAME", value = each.key }
]
portMappings = [
{
containerPort = each.value.container_port
}
]
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-group = "${lower(each.key)}-logs"
awslogs-region = data.aws_region.current.name
awslogs-stream-prefix = var.app_name
}
}
}
])
}
# CloudWatch Log Groups
resource "aws_cloudwatch_log_group" "ecs_cw_log_group" {
for_each = toset(keys(var.ecs_services))
name = lower("${each.key}-logs")
}
# ECS Auto Scaling Configuration
resource "aws_appautoscaling_target" "service_autoscaling" {
for_each = var.ecs_services
max_capacity = each.value.auto_scaling.max_capacity
min_capacity = each.value.auto_scaling.min_capacity
resource_id = "service/${aws_ecs_cluster.ecs_cluster.name}/${aws_ecs_service.service[each.key].name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
# Auto Scaling Policies
resource "aws_appautoscaling_policy" "ecs_policy_memory" {
for_each = var.ecs_services
name = "${var.app_name}-memory-autoscaling"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.service_autoscaling[each.key].resource_id
scalable_dimension = aws_appautoscaling_target.service_autoscaling[each.key].scalable_dimension
service_namespace = aws_appautoscaling_target.service_autoscaling[each.key].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
}
target_value = each.value.auto_scaling.memory_threshold
}
}
resource "aws_appautoscaling_policy" "ecs_policy_cpu" {
for_each = var.ecs_services
name = "${var.app_name}-cpu-autoscaling"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.service_autoscaling[each.key].resource_id
scalable_dimension = aws_appautoscaling_target.service_autoscaling[each.key].scalable_dimension
service_namespace = aws_appautoscaling_target.service_autoscaling[each.key].service_namespace
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = each.value.auto_scaling.cpu_threshold
}
}