-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.tf
149 lines (126 loc) · 4.01 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
data "ibm_is_image" "image" {
name = var.image_name
}
data "ibm_is_subnet" "subnet" {
identifier = var.subnet_id
}
resource "ibm_is_security_group" "bastion" {
name = "${var.name}-group"
vpc = var.vpc_id
resource_group = var.resource_group_id
}
resource "ibm_is_security_group_rule" "ssh_inbound" {
group = ibm_is_security_group.bastion.id
direction = "inbound"
remote = var.allow_ssh_from
tcp {
port_min = 22
port_max = 22
}
}
resource "ibm_is_security_group_rule" "ssh_to_host_in_maintenance" {
group = ibm_is_security_group.bastion.id
direction = "outbound"
remote = ibm_is_security_group.maintenance.id
tcp {
port_min = 22
port_max = 22
}
}
resource "ibm_is_security_group_rule" "ssh_to_self_public_ip" {
count = var.create_public_ip ? 1 : 0
group = ibm_is_security_group.bastion.id
direction = "outbound"
remote = ibm_is_floating_ip.bastion[0].address
tcp {
port_min = 22
port_max = 22
}
}
resource "ibm_is_security_group_rule" "additional_all_rules" {
for_each = {
for rule in var.security_group_rules : rule.name => rule if lookup(rule, "tcp", null) == null && lookup(rule, "udp", null) == null && lookup(rule, "icmp", null) == null
}
group = ibm_is_security_group.bastion.id
direction = each.value.direction
remote = each.value.remote
ip_version = lookup(each.value, "ip_version", null)
}
resource "ibm_is_security_group_rule" "additional_tcp_rules" {
for_each = {
for rule in var.security_group_rules : rule.name => rule if lookup(rule, "tcp", null) != null
}
group = ibm_is_security_group.bastion.id
direction = each.value.direction
remote = each.value.remote
ip_version = lookup(each.value, "ip_version", null)
tcp {
port_min = each.value.tcp.port_min
port_max = each.value.tcp.port_max
}
}
resource "ibm_is_security_group_rule" "additional_udp_rules" {
for_each = {
for rule in var.security_group_rules : rule.name => rule if lookup(rule, "udp", null) != null
}
group = ibm_is_security_group.bastion.id
direction = each.value.direction
remote = each.value.remote
ip_version = lookup(each.value, "ip_version", null)
udp {
port_min = each.value.udp.port_min
port_max = each.value.udp.port_max
}
}
resource "ibm_is_security_group_rule" "additional_icmp_rules" {
for_each = {
for rule in var.security_group_rules : rule.name => rule if lookup(rule, "icmp", null) != null
}
group = ibm_is_security_group.bastion.id
direction = each.value.direction
remote = each.value.remote
ip_version = lookup(each.value, "ip_version", null)
icmp {
type = each.value.icmp.type
code = lookup(each.value.icmp, "code", null) == null ? null : each.value.icmp.code
}
}
resource "ibm_is_instance" "bastion" {
name = var.name
vpc = var.vpc_id
zone = data.ibm_is_subnet.subnet.zone
profile = var.profile_name
image = data.ibm_is_image.image.id
keys = var.ssh_key_ids
resource_group = var.resource_group_id
user_data = var.init_script != "" ? var.init_script : file("${path.module}/init-script-ubuntu.sh")
primary_network_interface {
subnet = data.ibm_is_subnet.subnet.id
security_groups = [ibm_is_security_group.bastion.id]
}
boot_volume {
name = "${var.name}-boot"
}
tags = var.tags
}
resource "ibm_is_floating_ip" "bastion" {
count = var.create_public_ip ? 1 : 0
name = "${var.name}-ip"
target = ibm_is_instance.bastion.primary_network_interface[0].id
resource_group = var.resource_group_id
tags = var.tags
}
resource "ibm_is_security_group" "maintenance" {
name = "${var.name}-maintenance"
vpc = var.vpc_id
resource_group = var.resource_group_id
}
resource "ibm_is_security_group_rule" "maintenance_ssh_inbound" {
group = ibm_is_security_group.maintenance.id
direction = "inbound"
remote = ibm_is_security_group.bastion.id
tcp {
port_min = 22
port_max = 22
}
}