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 12
/
main.tf
229 lines (198 loc) · 7 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#docker run --rm -it -e VAULT_ADDR='http://127.0.0.1:8200' --privileged --network=host vault unseal $KEY
data "aws_availability_zones" "available" {
}
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 "aws_region" "current" {
}
data "template_file" "vault" {
template = file("${path.module}/files/vault.json")
vars = {
datacenter = local.vpc_name
env = var.env
image = var.vault_image
unseal_key0 = var.unseal_keys[0]
unseal_key1 = var.unseal_keys[1]
unseal_key2 = var.unseal_keys[2]
awslogs_group = "vault-${var.env}"
awslogs_stream_prefix = "vault-${var.env}"
awslogs_region = data.aws_region.current.name
vault_ui = var.enable_vault_ui ? "true" : "false"
}
}
data "template_file" "vault_init" {
template = file("${path.module}/files/vault_init.json")
vars = {
datacenter = local.vpc_name
env = var.env
image = var.vault_image
awslogs_group = "vault-${var.env}"
awslogs_stream_prefix = "vault-${var.env}"
awslogs_region = data.aws_region.current.name
vault_ui = var.enable_vault_ui ? "true" : "false"
}
}
# End Data block
# local variables
locals {
initialize = var.initialize ? true : false
cluster_count = length(var.ecs_cluster_ids)
vault_standalone_count = local.initialize ? 0 : local.cluster_count == 1 ? 1 : 0
vault_clustered_count = local.initialize ? 0 : local.cluster_count > 1 ? 1 : 0
vault_init_count = local.initialize ? 1 : 0
vpc_name = data.aws_vpc.vpc.tags["Name"]
sg_name = "tf-${local.vpc_name}-vault-uiSecurityGroup"
sg_tags = merge(
var.tags,
{
"Name" = local.sg_name
"Environment" = var.env
},
)
log_tags = merge(
var.tags,
{
"VPC" = local.vpc_name
"Application" = aws_ecs_task_definition.vault.family
},
)
}
resource "aws_ecs_task_definition" "vault" {
family = "vault-${var.env}"
container_definitions = data.template_file.vault.rendered
network_mode = "host"
task_role_arn = aws_iam_role.vault_task.arn
}
resource "aws_cloudwatch_log_group" "vault" {
name = aws_ecs_task_definition.vault.family
retention_in_days = var.cloudwatch_log_retention
tags = local.log_tags
}
resource "aws_ecs_task_definition" "vault_init" {
count = local.vault_init_count
family = "vault-init-${var.env}"
container_definitions = data.template_file.vault_init.rendered
network_mode = "host"
task_role_arn = aws_iam_role.vault_task.arn
}
resource "aws_cloudwatch_log_group" "vault_init" {
count = local.vault_init_count
name = aws_ecs_task_definition.vault_init[0].family
retention_in_days = "1"
tags = local.log_tags
}
# ECS Service
resource "aws_ecs_service" "vault" {
/* count = "${local.cluster_count == 1 ? 1 : 0}" */
count = local.vault_standalone_count
name = "vault-${var.env}"
cluster = var.ecs_cluster_ids[0]
task_definition = aws_ecs_task_definition.vault.arn
desired_count = var.desired_count
deployment_minimum_healthy_percent = var.service_minimum_healthy_percent
iam_role = aws_iam_role.ecsServiceRole.arn
placement_constraints {
type = "distinctInstance"
}
load_balancer {
target_group_arn = aws_alb_target_group.vault_ui.arn
container_name = "vault-${var.env}"
container_port = 8200
}
depends_on = [
aws_alb_target_group.vault_ui,
aws_alb_listener.vault_https,
aws_alb.vault,
aws_iam_role.ecsServiceRole,
]
}
resource "aws_ecs_service" "vault_primary" {
/* count = "${local.cluster_count > 1 ? 1 : 0}" */
count = local.vault_clustered_count
name = "vault-${var.env}-primary"
cluster = var.ecs_cluster_ids[0]
task_definition = aws_ecs_task_definition.vault.arn
desired_count = var.desired_count
deployment_minimum_healthy_percent = var.service_minimum_healthy_percent
iam_role = aws_iam_role.ecsServiceRole.arn
placement_constraints {
type = "distinctInstance"
}
load_balancer {
target_group_arn = aws_alb_target_group.vault_ui.arn
container_name = "vault-${var.env}"
container_port = 8200
}
depends_on = [
aws_alb_target_group.vault_ui,
aws_alb_listener.vault_https,
aws_alb.vault,
aws_iam_role.ecsServiceRole,
]
}
resource "aws_ecs_service" "vault_secondary" {
/* count = "${local.cluster_count > 1 ? 1 : 0}" */
count = local.vault_clustered_count
name = "vault-${var.env}-secondary"
cluster = var.ecs_cluster_ids[1]
task_definition = aws_ecs_task_definition.vault.arn
desired_count = var.desired_count
deployment_minimum_healthy_percent = var.service_minimum_healthy_percent
iam_role = aws_iam_role.ecsServiceRole.arn
placement_constraints {
type = "distinctInstance"
}
load_balancer {
target_group_arn = aws_alb_target_group.vault_ui.arn
container_name = "vault-${var.env}"
container_port = 8200
}
depends_on = [
aws_alb_target_group.vault_ui,
aws_alb_listener.vault_https,
aws_alb.vault,
aws_iam_role.ecsServiceRole,
]
}
resource "aws_ecs_service" "vault_init" {
count = local.vault_init_count
name = "vault-init-${var.env}"
cluster = var.ecs_cluster_ids[0]
task_definition = aws_ecs_task_definition.vault_init[0].arn
desired_count = "1"
deployment_minimum_healthy_percent = "0"
placement_constraints {
type = "distinctInstance"
}
}
# End Service
# Security Groups
resource "aws_security_group" "lb-vault-sg" {
name = "tf-${local.vpc_name}-vault-uiSecurityGroup"
description = "Allow Web Traffic into the ${local.vpc_name} VPC"
vpc_id = data.aws_vpc.vpc.id
tags = local.sg_tags
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"]
}
}