-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
165 lines (143 loc) · 4.7 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
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
/*
@doc()
# Service without traffic control ecs documentation
Module to provisioning services and rolling update deployments and autoscaling ecs task with cloudwatch alarms
*/
// AWS ECS Service to run the task definition
resource "aws_ecs_service" "main" {
name = var.name
cluster = var.cluster
task_definition = aws_ecs_task_definition.main.arn
scheduling_strategy = "REPLICA"
desired_count = var.service_count
force_new_deployment = true
deployment_minimum_healthy_percent = 0
deployment_maximum_percent = 100
network_configuration {
security_groups = var.security_groups
subnets = var.subnets
assign_public_ip = var.public_ip
}
lifecycle {
ignore_changes = [
desired_count,
]
}
capacity_provider_strategy {
capacity_provider = "FARGATE_SPOT"
weight = 100
base = 1
}
}
// AWS ECS Task definition to run the container passed by name
resource "aws_ecs_task_definition" "main" {
family = "${var.name}-service"
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
execution_role_arn = var.roleExecArn
task_role_arn = var.roleArn
cpu = var.cpu_unit
memory = var.memory
container_definitions = data.template_file.main.rendered
}
data "template_file" "main" {
template = file("${path.module}/task_definition_${var.use_cloudwatch_logs ? "cloudwatch" : "elasticsearch"}.json")
vars = {
ecr_image_url = var.ecr_image_url
name = var.name
name_index_log = lower(var.name)
port = var.port
region = var.region
environment = jsonencode(concat(local.main_environment, var.environment_list))
database_log_level = var.database_log_level
log_level = var.log_level
es_url = var.es_url
prefix_logs = var.prefix_logs
}
}
locals {
main_environment = [
{
name = "DATABASE_LOG_LEVEL",
value = var.database_log_level
},
{
name = "APP",
value = var.name
},
{
name = "LOG_LEVEL",
value = var.log_level
},
{
name = "PORT",
value = var.port
},
{
name = "NEW_RELIC_APP_NAME",
value = var.name
}
]
}
// Auxiliary logs
resource "aws_cloudwatch_log_group" "main" {
count = var.use_cloudwatch_logs ? 0 : 1
name = "${var.name}-firelens-container"
retention_in_days = 14
}
resource "aws_cloudwatch_log_group" "main_app" {
count = var.use_cloudwatch_logs ? 1 : 0
name = var.name
retention_in_days = 14
}
/*===========================================
Autoscaling zone
============================================*/
// AWS Autoscaling target to linked the ecs cluster and service
resource "aws_appautoscaling_target" "main" {
service_namespace = "ecs"
resource_id = "service/${var.cluster}/${aws_ecs_service.main.name}"
scalable_dimension = "ecs:service:DesiredCount"
role_arn = var.auto_scale_role
min_capacity = var.min_scale
max_capacity = var.max_scale
lifecycle {
ignore_changes = [
role_arn,
]
}
}
// AWS Autoscaling policy to scale using cpu allocation
resource "aws_appautoscaling_policy" "cpu" {
name = "ecs_scale_cpu"
resource_id = aws_appautoscaling_target.main.resource_id
scalable_dimension = aws_appautoscaling_target.main.scalable_dimension
service_namespace = aws_appautoscaling_target.main.service_namespace
policy_type = "TargetTrackingScaling"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = 75
scale_in_cooldown = 60
scale_out_cooldown = 60
}
depends_on = [aws_appautoscaling_target.main]
}
// AWS Autoscaling policy to scale using memory allocation
resource "aws_appautoscaling_policy" "memory" {
name = "ecs_scale_memory"
resource_id = aws_appautoscaling_target.main.resource_id
scalable_dimension = aws_appautoscaling_target.main.scalable_dimension
service_namespace = aws_appautoscaling_target.main.service_namespace
policy_type = "TargetTrackingScaling"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
}
target_value = 75
scale_in_cooldown = 60
scale_out_cooldown = 60
}
depends_on = [aws_appautoscaling_target.main]
}