-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
127 lines (117 loc) · 5.07 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
locals {
service_name = lookup(var.fargate_services, "service_name", "app")
}
resource "aws_ecs_service" "main" {
name = lookup(var.fargate_services, "service_name", "app")
cluster = var.cluster_id
task_definition = lookup(var.fargate_services, "task_definition_arn")
desired_count = lookup(var.fargate_services, "service_desired_count", 1)
launch_type = "FARGATE"
force_new_deployment = var.force_new_deployment
enable_execute_command = true
network_configuration {
security_groups = var.security_group_ids
assign_public_ip = var.assign_public_ip
subnets = var.subnet_ids
}
dynamic "load_balancer" {
for_each = var.fargate_services["lb_config"] == null ? [] : [for s in var.fargate_services["lb_config"] : {
target_group_arn = s.target_group_arn != null ? s.target_group_arn : null
container_name = s.container_name != null ? s.container_name : null
container_port = s.container_port != null ? s.container_port : null
}]
content {
target_group_arn = load_balancer.value.target_group_arn
container_name = load_balancer.value.container_name
container_port = load_balancer.value.container_port
}
}
}
#------------------------------------------------------------------------------
# AWS Auto Scaling - CloudWatch Alarm CPU High
#------------------------------------------------------------------------------
resource "aws_cloudwatch_metric_alarm" "ecs_cpu_high" {
count = var.enable_autoscaling ? 1 : 0
alarm_name = "${local.service_name}-cpu-hihg"
alarm_description = "ECS Fargate Service CPU utilization"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Maximum"
threshold = var.max_cpu_threshold
dimensions = {
ClusterName = var.cluster_name
ServiceName = local.service_name
}
alarm_actions = [aws_appautoscaling_policy.scale_up_policy.arn]
}
#------------------------------------------------------------------------------
# AWS Auto Scaling - CloudWatch Alarm CPU Low
#------------------------------------------------------------------------------
resource "aws_cloudwatch_metric_alarm" "ecs_cpu_low" {
count = var.enable_autoscaling ? 1 : 0
alarm_name = "${local.service_name}-cpu-low"
alarm_description = "ECS Fargate Service CPU utilization"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = var.min_cpu_threshold
dimensions = {
ClusterName = var.cluster_name
ServiceName = local.service_name
}
alarm_actions = [aws_appautoscaling_policy.scale_down_policy.arn]
}
#------------------------------------------------------------------------------
# AWS Auto Scaling - Scaling Up Policy
#------------------------------------------------------------------------------
resource "aws_appautoscaling_policy" "scale_up_policy" {
name = "${local.service_name}-scale-up-policy"
depends_on = [aws_appautoscaling_target.scale_target]
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${local.service_name}"
scalable_dimension = "ecs:service:DesiredCount"
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Maximum"
step_adjustment {
metric_interval_lower_bound = 0
scaling_adjustment = 1
}
}
}
#------------------------------------------------------------------------------
# AWS Auto Scaling - Scaling Down Policy
#------------------------------------------------------------------------------
resource "aws_appautoscaling_policy" "scale_down_policy" {
name = "${local.service_name}-scale-down-policy"
depends_on = [aws_appautoscaling_target.scale_target]
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${local.service_name}"
scalable_dimension = "ecs:service:DesiredCount"
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Maximum"
step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = -1
}
}
}
#------------------------------------------------------------------------------
# AWS Auto Scaling - Scaling Target
#------------------------------------------------------------------------------
resource "aws_appautoscaling_target" "scale_target" {
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${local.service_name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = var.environment == "prd" ? var.scale_target_min_capacity : 1
max_capacity = var.environment == "prd" ? var.scale_target_max_capacity : 2
}