forked from tuier/tfm-vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbastion.tf
119 lines (95 loc) · 3.31 KB
/
bastion.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
# bastion configuration
resource "aws_eip" "bastion" {
count = "${var.azs_count}"
# count = "${length(aws_network_interface.bastion.*.id)}"
network_interface = "${element(aws_network_interface.bastion.*.id,count.index)}"
vpc = true
lifecycle {
create_before_destroy = true
}
}
resource "aws_network_interface" "bastion" {
count = "${var.azs_count}"
# count = "${length(aws_subnet.public.*.id)}"
subnet_id = "${element(aws_subnet.public.*.id,count.index)}"
description = "${var.cluster_name}_bastion endpoint for ${element(aws_subnet.public.*.availability_zone,count.index)}"
security_groups = ["${aws_security_group.allow_bastion_ingress.id}"]
tags {
Name = "${var.cluster_name}_eni-bastion-${element(aws_subnet.public.*.id,count.index)}"
cluster = "${var.cluster_name}"
product = "${var.tag_product}"
purpose = "${var.tag_purpose}"
builder = "terraform"
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "bastion" {
name = "${var.cluster_name}_bastion"
max_size = 1
min_size = 1
desired_capacity = 1
health_check_grace_period = 120
health_check_type = "EC2"
force_delete = true
launch_configuration = "${aws_launch_configuration.bastion.name}"
vpc_zone_identifier = ["${aws_subnet.public.*.id}"]
tag {
key = "Name"
value = "${var.cluster_name}_bastion"
propagate_at_launch = true
}
tag {
key = "builder"
value = "terraform"
propagate_at_launch = true
}
tag {
key = "cluster"
value = "${var.cluster_name}"
propagate_at_launch = true
}
tag {
key = "product"
value = "${var.tag_product}"
propagate_at_launch = true
}
tag {
key = "purpose"
value = "${var.tag_purpose}"
propagate_at_launch = true
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_launch_configuration" "bastion" {
name_prefix = "${var.cluster_name}_bastion-"
image_id = "${lookup(var.ami_bastion, var.region)}"
instance_type = "${var.instance_type_bastion}"
key_name = "${var.aws_key_name}"
associate_public_ip_address = true
iam_instance_profile = "${aws_iam_instance_profile.bastion_profile.name}"
security_groups = ["${aws_security_group.allow_bastion_ingress.id}", "${aws_security_group.bastion.id}"]
user_data = "${data.template_file.launch_bastion.rendered}\n${var.user_data}"
lifecycle {
create_before_destroy = true
}
}
data "template_file" "launch_bastion" {
vars {
region = "${var.region}"
enis_map = "${join(" ",data.template_file.enis_map.*.rendered)}"
}
template = "${file("${path.module}/bastion_user_data.tpl")}"
}
data "template_file" "enis_map" {
# count = "${length(aws_network_interface.bastion.*.id)}"
count = "${var.azs_count}"
vars {
zone = "${replace(element(split(" ", element(aws_network_interface.bastion.*.description,count.index)),3),"${var.region}","")}"
eni = "${element(aws_network_interface.bastion.*.id,count.index)}"
}
template = "${file("${path.module}/enis_map.tpl")}"
}