-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.tf
142 lines (118 loc) · 4.85 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
terraform {
required_version = ">= 0.11.6"
}
module "consul_auto_join_instance_role" {
source = "github.com/hashicorp-modules/consul-auto-join-instance-role-aws"
create = "${var.create ? 1 : 0}"
name = "${var.name}"
}
data "aws_ami" "vault" {
count = "${var.create && var.image_id == "" ? 1 : 0}"
most_recent = true
owners = ["${var.ami_owner}"]
name_regex = "vault-image_${lower(var.release_version)}_vault_${lower(var.vault_version)}_consul_${lower(var.consul_version)}_${lower(var.os)}_${var.os_version}.*"
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
data "template_file" "vault_init" {
count = "${var.create ? 1 : 0}"
template = "${file("${path.module}/templates/init-systemd.sh.tpl")}"
vars = {
name = "${var.name}"
user_data = "${var.user_data != "" ? var.user_data : "echo 'No custom user_data'"}"
}
}
module "vault_server_sg" {
source = "github.com/hashicorp-modules/vault-server-ports-aws"
create = "${var.create ? 1 : 0}"
name = "${var.name}-vault-server"
vpc_id = "${var.vpc_id}"
cidr_blocks = ["${var.public ? "0.0.0.0/0" : var.vpc_cidr}"] # If there's a public IP, open Vault ports for public access - DO NOT DO THIS IN PROD
}
module "consul_client_sg" {
source = "github.com/hashicorp-modules/consul-client-ports-aws"
create = "${var.create ? 1 : 0}"
name = "${var.name}-vault-consul-client"
vpc_id = "${var.vpc_id}"
cidr_blocks = ["${var.public ? "0.0.0.0/0" : var.vpc_cidr}"] # If there's a public IP, open Consul ports for public access - DO NOT DO THIS IN PROD
}
resource "aws_security_group_rule" "ssh" {
count = "${var.create ? 1 : 0}"
security_group_id = "${module.vault_server_sg.vault_server_sg_id}"
type = "ingress"
protocol = "tcp"
from_port = 22
to_port = 22
cidr_blocks = ["${var.public ? "0.0.0.0/0" : var.vpc_cidr}"] # If there's a public IP, open port 22 for public access - DO NOT DO THIS IN PROD
}
resource "aws_launch_configuration" "vault" {
count = "${var.create ? 1 : 0}"
name_prefix = "${format("%s-vault-", var.name)}"
associate_public_ip_address = "${var.public}"
ebs_optimized = false
instance_type = "${var.instance_type}"
image_id = "${var.image_id != "" ? var.image_id : element(concat(data.aws_ami.vault.*.id, list("")), 0)}" # TODO: Workaround for issue #11210
iam_instance_profile = "${var.instance_profile != "" ? var.instance_profile : module.consul_auto_join_instance_role.instance_profile_id}"
user_data = "${data.template_file.vault_init.rendered}"
key_name = "${var.ssh_key_name}"
security_groups = [
"${module.vault_server_sg.vault_server_sg_id}",
"${module.consul_client_sg.consul_client_sg_id}",
]
lifecycle {
create_before_destroy = true
}
}
module "vault_lb_aws" {
source = "github.com/hashicorp-modules/vault-lb-aws"
create = "${var.create}"
name = "${var.name}"
vpc_id = "${var.vpc_id}"
cidr_blocks = ["${var.public ? "0.0.0.0/0" : var.vpc_cidr}"] # If there's a public IP, open port 22 for public access - DO NOT DO THIS IN PROD
subnet_ids = ["${var.subnet_ids}"]
is_internal_lb = "${!var.public}"
use_lb_cert = "${var.use_lb_cert}"
lb_cert = "${var.lb_cert}"
lb_private_key = "${var.lb_private_key}"
lb_cert_chain = "${var.lb_cert_chain}"
lb_ssl_policy = "${var.lb_ssl_policy}"
lb_bucket = "${var.lb_bucket}"
lb_bucket_override = "${var.lb_bucket_override}"
lb_bucket_prefix = "${var.lb_bucket_prefix}"
lb_logs_enabled = "${var.lb_logs_enabled}"
tags = "${var.tags}"
}
resource "aws_autoscaling_group" "vault" {
count = "${var.create ? 1 : 0}"
name_prefix = "${aws_launch_configuration.vault.name}"
launch_configuration = "${aws_launch_configuration.vault.id}"
vpc_zone_identifier = ["${var.subnet_ids}"]
max_size = "${var.count != -1 ? var.count : length(var.subnet_ids)}"
min_size = "${var.count != -1 ? var.count : length(var.subnet_ids)}"
desired_capacity = "${var.count != -1 ? var.count : length(var.subnet_ids)}"
default_cooldown = 30
force_delete = true
target_group_arns = ["${compact(concat(
list(
module.vault_lb_aws.vault_tg_http_8200_arn,
module.vault_lb_aws.vault_tg_https_8200_arn,
),
var.target_groups
))}"]
tags = ["${concat(
list(
map("key", "Name", "value", format("%s-vault-node", var.name), "propagate_at_launch", true),
map("key", "Consul-Auto-Join", "value", var.name, "propagate_at_launch", true)
),
var.tags_list
)}"]
lifecycle {
create_before_destroy = true
}
}