-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
156 lines (133 loc) · 4.81 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
resource "aws_ecs_service" "this" {
name = var.service_name
cluster = local.cluster_arn
task_definition = aws_ecs_task_definition.this.arn
desired_count = var.service_count
launch_type = var.use_fargate ? "FARGATE" : "EC2"
platform_version = var.use_fargate ? var.fargate_version : null
enable_execute_command = var.enable_ecs_exec
health_check_grace_period_seconds = var.healthcheck_grace_period
dynamic "load_balancer" {
for_each = var.target_group_arn != null ? [true] : []
content {
container_name = var.service_name
container_port = var.container_port
target_group_arn = var.target_group_arn
}
}
dynamic "network_configuration" {
for_each = var.network_mode == "awsvpc" && var.subnet_ids != null ? [true] : []
content {
assign_public_ip = var.use_fargate ? var.assign_public_ip : false
security_groups = var.security_groups
subnets = var.subnet_ids
}
}
dynamic "service_registries" {
for_each = var.private_dns_namespace != null ? [true] : []
content {
registry_arn = aws_service_discovery_service.this[0].arn
container_port = var.container_port
}
}
deployment_maximum_percent = var.deployment_maximum_percent
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
wait_for_steady_state = var.wait_for_steady_state
deployment_circuit_breaker {
enable = var.enable_rollback
rollback = var.enable_rollback
}
timeouts {
create = var.timeout
update = var.timeout
delete = var.timeout
}
lifecycle {
ignore_changes = [desired_count]
}
depends_on = [
aws_iam_role_policy_attachment.execution_role_policy,
aws_iam_role_policy_attachment.task_role_policy
]
tags = local.tags
}
resource "aws_ecs_task_definition" "this" {
family = var.service_name
network_mode = var.network_mode
execution_role_arn = aws_iam_role.execution_role.arn
task_role_arn = aws_iam_role.task_role.arn
skip_destroy = var.skip_destroy
requires_compatibilities = var.use_fargate ? ["FARGATE"] : ["EC2"]
cpu = var.use_fargate ? var.cpu : null
memory = var.use_fargate ? var.memory : null
container_definitions = jsonencode([
{
name = var.service_name
image = "${var.docker_image}:${var.docker_tag}"
essential = true
environment = local.merged_environment
secrets = local.merged_secrets
cpu = var.use_fargate ? null : var.cpu
memory = var.use_fargate ? null : var.memory
entrypoint = var.entrypoint
portMappings = [{
protocol = "tcp"
containerPort = var.container_port
}]
logConfiguration = {
logDriver = "awslogs"
options = {
awslogs-region = local.region
awslogs-stream-prefix = "ecs"
awslogs-group = local.log_group
}
}
}
])
# TODO: allow users to specify platform
dynamic "runtime_platform" {
for_each = var.use_fargate ? [true] : [false]
content {
operating_system_family = "LINUX"
cpu_architecture = "X86_64"
}
}
tags = local.tags
}
resource "aws_appautoscaling_target" "this" {
count = var.use_autoscaling ? 1 : 0
max_capacity = var.max_capacity
min_capacity = var.min_capacity
resource_id = "service/${var.cluster_name}/${aws_ecs_service.this.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
resource "aws_appautoscaling_policy" "this" {
for_each = var.use_autoscaling ? var.autoscaling_metrics : {}
name = "${aws_ecs_service.this.name}-scaling-policy-${each.key}"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.this[0].resource_id
scalable_dimension = aws_appautoscaling_target.this[0].scalable_dimension
service_namespace = aws_appautoscaling_target.this[0].service_namespace
target_tracking_scaling_policy_configuration {
target_value = each.value.target_value
scale_in_cooldown = each.value.scale_in_cooldown
scale_out_cooldown = each.value.scale_out_cooldown
predefined_metric_specification {
predefined_metric_type = each.value.metric_type
}
}
depends_on = [aws_appautoscaling_target.this]
}
resource "aws_service_discovery_service" "this" {
count = var.private_dns_namespace != null ? 1 : 0
name = var.service_name
dns_config {
namespace_id = data.aws_service_discovery_dns_namespace.this[0].id
routing_policy = "MULTIVALUE"
dns_records {
ttl = 10
type = "A"
}
}
}