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 8
/
main.tf
196 lines (165 loc) · 6.04 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
data "aws_vpc" "vpc" {
id = var.vpc_id
}
data "aws_route53_zone" "zone" {
count = local.enable_custom_domain ? 1 : 0
name = var.dns_zone
}
data "aws_acm_certificate" "cert" {
count = local.enable_custom_domain ? 1 : 0
domain = replace(var.dns_zone, "/.$/", "") # dirty hack to strip off trailing dot
statuses = ["ISSUED"]
most_recent = true
}
data "template_file" "consul" {
template = file("${path.module}/files/consul.json")
vars = {
datacenter = coalesce(var.datacenter_name, data.aws_vpc.vpc.tags["Name"])
definitions = join(" ", var.definitions)
env = var.env
enable_script_checks = var.enable_script_checks
enable_script_checks = var.enable_script_checks ? "true" : "false"
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
join_ec2_tag_key = var.join_ec2_tag_key
join_ec2_tag = var.join_ec2_tag
awslogs_group = "consul-${var.env}"
awslogs_stream_prefix = "consul-${var.env}"
awslogs_region = var.region
sha_htpasswd_hash = var.sha_htpasswd_hash
oauth2_proxy_htpasswd_file = var.oauth2_proxy_htpasswd_file
oauth2_proxy_provider = var.oauth2_proxy_provider
oauth2_proxy_github_org = var.oauth2_proxy_github_org
oauth2_proxy_github_team = join(",", var.oauth2_proxy_github_team)
oauth2_proxy_client_id = var.oauth2_proxy_client_id
oauth2_proxy_client_secret = var.oauth2_proxy_client_secret
raft_multiplier = var.raft_multiplier
leave_on_terminate = var.leave_on_terminate ? "true" : "false"
s3_backup_bucket = var.s3_backup_bucket
}
}
# End Data block
resource "aws_ecs_task_definition" "consul" {
family = "consul-${var.env}"
container_definitions = data.template_file.consul.rendered
network_mode = "host"
task_role_arn = aws_iam_role.consul_task.arn
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
retention_in_days = var.cloudwatch_log_retention
tags = {
VPC = data.aws_vpc.vpc.tags["Name"]
Application = aws_ecs_task_definition.consul.family
}
}
# start service
resource "aws_ecs_service" "consul" {
count = length(var.ecs_cluster_ids) == 1 ? 1 : 0
name = "consul-${var.env}"
cluster = var.ecs_cluster_ids[0]
task_definition = aws_ecs_task_definition.consul.arn
desired_count = var.cluster_size
deployment_minimum_healthy_percent = var.service_minimum_healthy_percent
placement_constraints {
type = "distinctInstance"
}
load_balancer {
target_group_arn = aws_alb_target_group.consul_ui.arn
container_name = "consul-ui-${var.env}"
container_port = 4180
}
iam_role = aws_iam_role.ecsServiceRole.arn
depends_on = [
aws_alb_target_group.consul_ui,
aws_alb_listener.consul_https,
aws_alb.consul,
aws_iam_role.ecsServiceRole,
]
}
resource "aws_ecs_service" "consul_primary" {
count = length(var.ecs_cluster_ids) > 1 ? 1 : 0
name = "consul-${var.env}-primary"
cluster = var.ecs_cluster_ids[0]
task_definition = aws_ecs_task_definition.consul.arn
desired_count = var.cluster_size
deployment_minimum_healthy_percent = var.service_minimum_healthy_percent
placement_constraints {
type = "distinctInstance"
}
load_balancer {
target_group_arn = aws_alb_target_group.consul_ui.arn
container_name = "consul-ui-${var.env}"
container_port = 4180
}
iam_role = aws_iam_role.ecsServiceRole.arn
depends_on = [
aws_alb_target_group.consul_ui,
aws_alb_listener.consul_https,
aws_alb.consul,
aws_iam_role.ecsServiceRole,
]
}
resource "aws_ecs_service" "consul_secondary" {
count = length(var.ecs_cluster_ids) > 1 ? 1 : 0
name = "consul-${var.env}-secondary"
cluster = var.ecs_cluster_ids[1]
task_definition = aws_ecs_task_definition.consul.arn
desired_count = var.cluster_size
deployment_minimum_healthy_percent = var.service_minimum_healthy_percent
placement_constraints {
type = "distinctInstance"
}
load_balancer {
target_group_arn = aws_alb_target_group.consul_ui.arn
container_name = "consul-ui-${var.env}"
container_port = 4180
}
iam_role = aws_iam_role.ecsServiceRole.arn
depends_on = [
aws_alb_target_group.consul_ui,
aws_alb_listener.consul_https,
aws_alb.consul,
aws_iam_role.ecsServiceRole,
]
}
# end service
# Security Groups
resource "aws_security_group" "alb-web-sg" {
name = "tf-${data.aws_vpc.vpc.tags["Name"]}-consul-uiSecurityGroup"
description = "Allow Web Traffic into the ${data.aws_vpc.vpc.tags["Name"]} VPC"
vpc_id = data.aws_vpc.vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "tf-${data.aws_vpc.vpc.tags["Name"]}-consul-uiSecurityGroup"
}
}