This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
123 lines (103 loc) · 3.22 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
data "aws_region" "current" {
}
data "aws_ecs_cluster" "current" {
cluster_name = var.ecs_cluster
}
data "template_file" "consul" {
template = file("${path.module}/templates/consul.json")
vars = {
env = var.ecs_cluster
definitions = join(" ", var.definitions)
image = var.consul_image
registrator_image = var.registrator_image
sidecar_image = var.sidecar_image
consul_memory_reservation = var.consul_memory_reservation
registrator_memory_reservation = var.registrator_memory_reservation
sidecar_memory_reservation = var.sidecar_memory_reservation
awslogs_group = "consul-agent-${var.ecs_cluster}"
awslogs_stream_prefix = "consul-agent-${var.ecs_cluster}"
awslogs_region = data.aws_region.current.name
}
}
# IAM Resources for Consul and Registrator Agents
data "aws_iam_policy_document" "consul_task_policy" {
statement {
actions = [
"ec2:Describe*",
"autoscaling:Describe*",
"cloudwatch:PutMetricData",
"ecs:DescribeClusters",
"ecs:UpdateContainerInstancesState",
"ecs:DescribeContainerInstances",
]
resources = ["*"]
}
}
data "aws_iam_policy_document" "assume_role_consul_task" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
# End Data block
resource "aws_iam_role" "consul_task" {
name_prefix = replace(
format(
"%.32s",
replace("tf-agentTaskRole-${var.ecs_cluster}-", "_", "-"),
),
"/\\s/",
"-",
)
path = var.iam_path
assume_role_policy = data.aws_iam_policy_document.assume_role_consul_task.json
}
resource "aws_iam_role_policy" "consul_ecs_task" {
name_prefix = replace(
format(
"%.102s",
replace("tf-agentTaskPol-${var.ecs_cluster}-", "_", "-"),
),
"/\\s/",
"-",
)
role = aws_iam_role.consul_task.id
policy = data.aws_iam_policy_document.consul_task_policy.json
}
resource "aws_ecs_task_definition" "consul" {
family = "consul-agent-${var.ecs_cluster}"
container_definitions = data.template_file.consul.rendered
network_mode = "host"
task_role_arn = aws_iam_role.consul_task.arn
volume {
name = "consul-config-dir"
host_path = "/etc/consul"
}
volume {
name = "docker-sock"
host_path = "/var/run/docker.sock"
}
volume {
name = "consul-check-definitions"
host_path = "/consul_check_definitions"
}
}
resource "aws_cloudwatch_log_group" "consul" {
name = aws_ecs_task_definition.consul.family
tags = {
Application = aws_ecs_task_definition.consul.family
}
}
resource "aws_ecs_service" "consul" {
name = "consul-agent-${var.ecs_cluster}"
cluster = data.aws_ecs_cluster.current.arn
task_definition = aws_ecs_task_definition.consul.arn
desired_count = data.aws_ecs_cluster.current.registered_container_instances_count
deployment_minimum_healthy_percent = "60"
placement_constraints {
type = "distinctInstance"
}
}