This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.tf
105 lines (87 loc) · 3.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
/* Template file that renders the container definition */
data "template_file" "docker-template" {
template = file("${path.module}/templates/task-definition.tpl")
vars = {
docker_repository = var.docker_repository
docker_image_tag = var.docker_image_tag
docker_image = var.docker_image
service_name = var.service_name
container_memory = var.container_memory
desired_count = var.desired_count
container_cpu = var.container_cpu == "" ? "" : "\"cpu\": ${var.container_cpu},"
environment_vars = var.docker_environment_vars
logging_config = var.docker_logging_config == "" ? "" : ",${var.docker_logging_config}"
mount_points = var.docker_mount_points == "" ? "" : ",${var.docker_mount_points}"
container_portmappings = jsonencode([for m in var.container_ports : { containerPort = tonumber(split("/", m)[0]), protocol = length(split("/", m)) > 1 ? split("/", m)[1] : "tcp" }])
}
}
resource "aws_ecs_task_definition" "task" {
family = "${var.environment}-${var.service_name}"
dynamic "volume" {
for_each = var.volumes
content {
host_path = volume.value["host_path"]
name = volume.value["name"]
}
}
cpu = var.launch_type == "FARGATE" ? var.container_cpu : null
memory = var.launch_type == "FARGATE" ? var.container_memory : null
execution_role_arn = var.launch_type == "FARGATE" ? aws_iam_role.ecs_tasks_execution_role[0].arn : null
requires_compatibilities = [var.launch_type]
container_definitions = data.template_file.docker-template.rendered
task_role_arn = var.task_role_arn
network_mode = var.launch_type == "FARGATE" ? "awsvpc" : var.networkmode
}
locals {
target_group_arn = var.target_group_arn == "" ? element(concat(aws_alb_target_group.target_group.*.arn, [""]), 0) : var.target_group_arn
}
resource "null_resource" "ecs_services_dependencies" {
count = var.enable_target_group_connection || var.enable_load_balanced ? 1 : 0
triggers = {
listeners = join(",", var.ecs_services_dependencies)
}
}
resource "aws_ecs_service" "service_alb" {
count = var.enable_target_group_connection || var.enable_alb || var.enable_load_balanced ? 1 : 0
depends_on = [
null_resource.ecs_services_dependencies,
aws_alb_listener.listener,
aws_alb_target_group.target_group,
aws_lb_listener_rule.default,
]
name = "${var.environment}-${var.service_name}"
cluster = var.ecs_cluster_id
task_definition = aws_ecs_task_definition.task.arn
desired_count = var.desired_count
health_check_grace_period_seconds = var.health_check_grace_period_seconds
load_balancer {
target_group_arn = local.target_group_arn
container_name = var.service_name
container_port = var.alb_container_port
}
iam_role = var.launch_type != "FARGATE" ? var.ecs_service_role : null
launch_type = var.launch_type
dynamic "network_configuration" {
for_each = var.launch_type == "FARGATE" ? list(var.launch_type) : []
content {
security_groups = var.awsvpc_service_security_groups
subnets = var.awsvpc_service_subnetids
}
}
}
resource "aws_ecs_service" "service" {
// Only enable if LB is not required
count = var.enable_target_group_connection || var.enable_alb || var.enable_load_balanced ? 0 : 1
name = "${var.environment}-${var.service_name}"
cluster = var.ecs_cluster_id
task_definition = aws_ecs_task_definition.task.arn
desired_count = var.desired_count
launch_type = var.launch_type
dynamic "network_configuration" {
for_each = var.launch_type == "FARGATE" ? list(var.launch_type) : []
content {
security_groups = var.awsvpc_service_security_groups
subnets = var.awsvpc_service_subnetids
}
}
}