-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.tf
157 lines (138 loc) · 6.53 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
locals {
service_name = "${var.env}-${replace(lookup(var.release, "component"), ".", "_")}"
}
module "ecs_update_monitor" {
source = "github.com/mergermarket/tf_ecs_update_monitor"
cluster = "${var.ecs_cluster}"
service = "${module.service.name}"
taskdef = "${module.taskdef.arn}"
}
module "service" {
source = "github.com/mergermarket/tf_load_balanced_ecs_service?ref=no-target-group"
name = "${local.service_name}${var.name_suffix}"
cluster = "${var.ecs_cluster}"
task_definition = "${module.taskdef.arn}"
container_name = "${lookup(var.release, "component")}${var.name_suffix}"
container_port = "${var.port}"
desired_count = "${var.desired_count}"
target_group_arn = "${var.target_group_arn}"
deployment_minimum_healthy_percent = "${var.deployment_minimum_healthy_percent}"
deployment_maximum_percent = "${var.deployment_maximum_percent}"
tags = "${var.tags}"
# tags = "${merge(
# map(
# "env", "${var.env}",
# "component", "${lookup(var.release, "component")}",
# "team", "${lookup(var.release, "version")}"
# ),
# var.tags
# )}"
health_check_grace_period_seconds = "${var.health_check_grace_period_seconds}"
health_check_interval = "${var.health_check_interval}"
health_check_timeout = "${var.health_check_timeout}"
health_check_healthy_threshold = "${var.health_check_healthy_threshold}"
health_check_unhealthy_threshold = "${var.health_check_unhealthy_threshold}"
}
module "taskdef" {
source = "github.com/mergermarket/tf_ecs_task_definition_with_task_role"
family = "${local.service_name}${var.name_suffix}"
container_definitions = ["${module.service_container_definition.rendered}"]
policy = "${var.task_role_policy}"
assume_role_policy = "${var.assume_role_policy}"
volume = "${var.taskdef_volume}"
env = "${var.env}"
release = "${var.release}"
network_mode = "${var.network_mode}"
}
module "service_container_definition" {
source = "github.com/mergermarket/tf_ecs_container_definition"
name = "${lookup(var.release, "component")}${var.name_suffix}"
image = "${lookup(var.release, "image_id")}"
cpu = "${var.cpu}"
memory = "${var.memory}"
stop_timeout = "${var.stop_timeout}"
container_port = "${var.port}"
nofile_soft_ulimit = "${var.nofile_soft_ulimit}"
mountpoint = "${var.container_mountpoint}"
port_mappings = "${var.container_port_mappings}"
application_secrets = "${var.application_secrets}"
platform_secrets = "${var.platform_secrets}"
container_env = "${merge(
map(
"LOGSPOUT_CLOUDWATCHLOGS_LOG_GROUP_STDOUT", "${local.service_name}${var.name_suffix}-stdout",
"LOGSPOUT_CLOUDWATCHLOGS_LOG_GROUP_STDERR", "${local.service_name}${var.name_suffix}-stderr",
"STATSD_HOST", "172.17.42.1",
"STATSD_PORT", "8125",
"STATSD_ENABLED", "true",
"ENV_NAME", "${var.env}",
"COMPONENT_NAME", "${lookup(var.release, "component")}",
"VERSION", "${lookup(var.release, "version")}"
),
var.common_application_environment,
var.application_environment,
var.secrets
)}"
labels = "${merge(map(
"component", var.release["component"],
"env", var.env,
"team", var.release["team"],
"version", var.release["version"],
"logentries.token", var.logentries_token
), var.container_labels)}"
}
resource "aws_cloudwatch_log_group" "stdout" {
name = "${local.service_name}${var.name_suffix}-stdout"
retention_in_days = "7"
}
resource "aws_cloudwatch_log_group" "stderr" {
name = "${local.service_name}${var.name_suffix}-stderr"
retention_in_days = "7"
}
resource "aws_cloudwatch_log_subscription_filter" "kinesis_log_stdout_stream" {
count = "${var.platform_config["datadog_log_subscription_arn"] != "" ? 1 : 0}"
name = "kinesis-log-stdout-stream-${local.service_name}"
destination_arn = "${var.platform_config["datadog_log_subscription_arn"]}"
log_group_name = "${local.service_name}${var.name_suffix}-stdout"
filter_pattern = ""
depends_on = ["aws_cloudwatch_log_group.stdout"]
}
resource "aws_cloudwatch_log_subscription_filter" "kinesis_log_stderr_stream" {
count = "${var.platform_config["datadog_log_subscription_arn"] != "" ? 1 : 0}"
name = "kinesis-log-stdout-stream-${local.service_name}"
destination_arn = "${var.platform_config["datadog_log_subscription_arn"]}"
log_group_name = "${local.service_name}${var.name_suffix}-stderr"
filter_pattern = ""
depends_on = ["aws_cloudwatch_log_group.stderr"]
}
resource "aws_appautoscaling_target" "ecs" {
count = "${var.allow_overnight_scaledown}"
min_capacity = "${var.desired_count}"
max_capacity = "${var.desired_count}"
resource_id = "service/${var.ecs_cluster}/${local.service_name}${var.name_suffix}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
resource "aws_appautoscaling_scheduled_action" "scale_down" {
count = "${(var.env != "live" && var.allow_overnight_scaledown) ? 1 : 0}"
name = "scale_down-${local.service_name}${var.name_suffix}"
service_namespace = "${aws_appautoscaling_target.ecs.service_namespace}"
resource_id = "${aws_appautoscaling_target.ecs.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.ecs.scalable_dimension}"
schedule = "cron(*/30 ${var.overnight_scaledown_start_hour}-${(var.overnight_scaledown_end_hour)-1} ? * * *)"
scalable_target_action {
min_capacity = "${var.overnight_scaledown_min_count}"
max_capacity = "${var.overnight_scaledown_min_count}"
}
}
resource "aws_appautoscaling_scheduled_action" "scale_back_up" {
count = "${(var.env != "live" && var.allow_overnight_scaledown) ? 1 : 0}"
name = "scale_up-${local.service_name}${var.name_suffix}"
service_namespace = "${aws_appautoscaling_target.ecs.service_namespace}"
resource_id = "${aws_appautoscaling_target.ecs.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.ecs.scalable_dimension}"
schedule = "cron(10 ${var.overnight_scaledown_end_hour} ? * MON-FRI *)"
scalable_target_action {
min_capacity = "${var.desired_count}"
max_capacity = "${var.desired_count}"
}
}