-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
147 lines (123 loc) · 6.43 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
module "service" {
source = "./modules/ecs-service"
env = var.env
name = var.name
app_type = var.app_type
ecs_cluster_name = local.ecs_cluster_name
ecs_cluster_arn = local.ecs_cluster_arn
ecs_service_name = local.ecs_service_name
ecs_platform_version = var.ecs_launch_type == "FARGATE" ? var.ecs_platform_version : null
ecs_launch_type = var.ecs_launch_type
ecs_task_health_check_command = var.ecs_task_health_check_command
ec2_service_group = var.ec2_service_group
docker_container_port = var.docker_container_port
ecs_network_mode = var.ecs_network_mode
ecs_volumes_from = var.ecs_volumes_from
cpu = var.cpu
memory = var.memory
memory_reservation = var.memory_reservation
volumes = local.volumes
assign_public_ip = var.assign_public_ip
security_groups = var.security_groups
operating_system_family = var.operating_system_family
cpu_architecture = var.cpu_architecture
web_proxy_enabled = var.web_proxy_enabled
ecs_exec_enabled = var.ecs_exec_enabled
subnets = var.public_ecs_service ? var.public_subnets : var.private_subnets
# length(var.cloudwatch_schedule_expressions) > 1 means that it is cron task and desired_count should be 0
cloudwatch_schedule_expressions = var.cloudwatch_schedule_expressions
service_desired_count = length(var.cloudwatch_schedule_expressions) > 1 ? 0 : var.desired_capacity
max_size = var.max_size
min_size = var.min_size
autoscale_enabled = var.autoscale_enabled
autoscale_scheduled_up = var.autoscale_scheduled_up
autoscale_scheduled_down = var.autoscale_scheduled_down
autoscale_target_value_cpu = var.autoscale_target_value_cpu
autoscale_target_value_memory = var.autoscale_target_value_memory
autoscale_scheduled_timezone = var.autoscale_scheduled_timezone
autoscaling_min_size = var.autoscaling_min_size
autoscaling_max_size = var.autoscaling_max_size
docker_container_entrypoint = var.docker_container_entrypoint
docker_container_command = var.docker_container_command
# If docker_image_name is set then use it, otherwise check if we are managing ECR repo on this module and use it's repository_url. Otherwise use docker_registry/name
docker_image_name = var.docker_image_name != "" ? var.docker_image_name : var.ecr_repo_create ? module.ecr.repository_url : "${var.docker_registry}/${var.name}"
docker_image_tag = var.docker_image_tag
iam_role_policy_statement = var.iam_role_policy_statement
additional_container_definition_parameters = var.additional_container_definition_parameters
app_secrets = var.app_secrets
global_secrets = var.global_secrets
ecs_service_deployed = (var.cloudwatch_schedule_expressions == [] || !var.ecs_service_deployed) ? false : true
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
aws_service_discovery_private_dns_namespace = var.aws_service_discovery_private_dns_namespace
firelens_ecs_log_enabled = var.firelens_ecs_log_enabled
tmpfs_enabled = var.tmpfs_enabled
tmpfs_size = var.tmpfs_size
tmpfs_container_path = var.tmpfs_container_path
tmpfs_mount_options = var.tmpfs_mount_options
shared_memory_size = var.shared_memory_size
# TODO: This should be expanded to read some standard labels from datadog module to configure JMX, http and other checks. per https://docs.datadoghq.com/agent/docker/integrations/?tab=docker#configuration
docker_labels = var.docker_labels
resource_requirements = var.gpu > 0 ? [
{
type = "GPU"
value = tostring(var.gpu)
}
] : []
sidecar_container_definitions = concat(
var.sidecar_container_definitions,
var.web_proxy_enabled ? [
module.nginx.container_definition
] : [],
var.datadog_enabled ? [
module.datadog.container_definition
] : [],
var.firelens_ecs_log_enabled ? local.fluentbit_container_definition : []
)
docker_container_links = concat(
var.datadog_enabled && var.ecs_network_mode == "bridge" ? [
"datadog-agent:datadog-agent"
] : [])
docker_container_depends_on = concat(
# TODO: This needs to be pulled from datadog agent module output
var.datadog_enabled ? [
{
containerName = "datadog-agent",
condition = "START"
},
] : []
)
# TODO: instead of hardcoding the index, better use dynamic lookup by a canonical name
target_group_arn = var.app_type == "web" && length(module.alb[*].target_group_arns) >= 1 ? module.alb[0].target_group_arns[0] : null
port_mappings = jsondecode(var.app_type == "web" ? jsonencode([
{
container_name = var.web_proxy_enabled ? "nginx" : var.name
container_port = var.web_proxy_enabled ? var.web_proxy_docker_container_port : var.docker_container_port
host_port = var.ecs_network_mode == "awsvpc" ? (var.web_proxy_enabled ? var.web_proxy_docker_container_port : var.docker_container_port) : var.docker_host_port
target_group_arn = length(module.alb[*].target_group_arns) >= 1 ? module.alb[0].target_group_arns[0] : ""
}
]) : (var.app_type == "tcp-app" ? jsonencode(local.ecs_service_tcp_port_mappings) : jsonencode(var.port_mappings)))
environment = merge(var.environment, local.datadog_env_vars, local.ecs_exec_env_vars, {
APP_NAME = var.name
ENV = var.env
PROXY_ENABLED = var.web_proxy_enabled ? "true" : "false"
}
)
}
resource "aws_route53_record" "alb" {
count = var.app_type == "web" || var.app_type == "tcp-app" ? length(local.domain_names) : 0
zone_id = var.zone_id
name = local.domain_names[count.index]
type = "A"
alias {
name = module.alb[0].lb_dns_name
zone_id = module.alb[0].lb_zone_id
evaluate_target_health = true
}
}
resource "aws_route53_record" "ec2" {
count = (var.ecs_launch_type == "EC2" && var.ec2_eip_enabled && var.ec2_eip_dns_enabled) ? length(local.domain_names) : 0
zone_id = var.zone_id
name = local.domain_names[count.index]
type = "A"
records = var.ec2_eip_enabled ? aws_eip.autoscaling.*.public_ip : []
}