-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.tf
228 lines (205 loc) · 7.51 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
##############################################################################
# Locals
##############################################################################
locals {
ssh_key_id = var.ssh_key != null ? data.ibm_is_ssh_key.existing_ssh_key[0].id : resource.ibm_is_ssh_key.ssh_key[0].id
}
##############################################################################
# Resource Group
##############################################################################
module "resource_group" {
source = "terraform-ibm-modules/resource-group/ibm"
version = "1.1.6"
# if an existing resource group is not set (null) create a new one using prefix
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
existing_resource_group_name = var.resource_group
}
##############################################################################
# Key Protect All Inclusive
##############################################################################
module "key_protect_all_inclusive" {
source = "terraform-ibm-modules/kms-all-inclusive/ibm"
version = "4.17.1"
resource_group_id = module.resource_group.resource_group_id
region = var.region
key_protect_instance_name = "${var.prefix}-kp"
resource_tags = var.resource_tags
keys = [
{
key_ring_name = "slz-vsi"
keys = [
{
key_name = "${var.prefix}-vsi"
force_delete = true
}
]
}
]
}
##############################################################################
# Create new SSH key
##############################################################################
resource "tls_private_key" "tls_key" {
count = var.ssh_key != null ? 0 : 1
algorithm = "RSA"
rsa_bits = 4096
}
resource "ibm_is_ssh_key" "ssh_key" {
count = var.ssh_key != null ? 0 : 1
name = "${var.prefix}-ssh-key"
public_key = resource.tls_private_key.tls_key[0].public_key_openssh
}
data "ibm_is_ssh_key" "existing_ssh_key" {
count = var.ssh_key != null ? 1 : 0
name = var.ssh_key
}
#############################################################################
# Provision VPC
#############################################################################
module "slz_vpc" {
source = "terraform-ibm-modules/landing-zone-vpc/ibm"
version = "7.19.1"
resource_group_id = module.resource_group.resource_group_id
region = var.region
prefix = var.prefix
tags = var.resource_tags
name = "vpc"
}
#############################################################################
# Placement group
#############################################################################
resource "ibm_is_placement_group" "placement_group" {
name = "${var.prefix}-host-spread"
resource_group = module.resource_group.resource_group_id
strategy = "host_spread"
tags = var.resource_tags
}
#############################################################################
# Provision Secondary Subnets
#############################################################################
locals {
secondary_subnet_map = {
"${var.prefix}-second-subnet-a" = {
zone = "${var.region}-1"
cidr = "10.10.20.0/24"
}
"${var.prefix}-second-subnet-b" = {
zone = "${var.region}-2"
cidr = "10.20.20.0/24"
}
"${var.prefix}-second-subnet-c" = {
zone = "${var.region}-3"
cidr = "10.30.20.0/24"
}
}
}
resource "ibm_is_vpc_address_prefix" "secondary_address_prefixes" {
for_each = local.secondary_subnet_map
name = "${each.key}-prefix"
vpc = module.slz_vpc.vpc_id
zone = each.value.zone
cidr = each.value.cidr
}
resource "ibm_is_subnet" "secondary_subnet" {
depends_on = [ibm_is_vpc_address_prefix.secondary_address_prefixes]
for_each = local.secondary_subnet_map
ipv4_cidr_block = each.value.cidr
name = each.key
vpc = module.slz_vpc.vpc_id
zone = each.value.zone
}
#############################################################################
# Provision Secondary Security Groups
#############################################################################
locals {
secondary_security_groups = [
for subnet in module.slz_vpc.subnet_zone_list : {
security_group_id = ibm_is_security_group.secondary_security_group.id
interface_name = "secondary-subnet-${subnet.zone}"
}
]
}
resource "ibm_is_security_group" "secondary_security_group" {
name = "${var.prefix}-sg"
vpc = module.slz_vpc.vpc_id
}
#############################################################################
# Provision VSI
#############################################################################
locals {
secondary_subnet_zone_list = [
for subnet in ibm_is_subnet.secondary_subnet : {
name = subnet.name
id = subnet.id
zone = subnet.zone
cidr = subnet.ipv4_cidr_block
}
]
}
module "slz_vsi" {
source = "../../"
resource_group_id = module.resource_group.resource_group_id
image_id = var.image_id
create_security_group = false
tags = var.resource_tags
access_tags = var.access_tags
subnets = module.slz_vpc.subnet_zone_list
vpc_id = module.slz_vpc.vpc_id
prefix = var.prefix
placement_group_id = ibm_is_placement_group.placement_group.id
machine_type = "cx2-2x4"
user_data = null
boot_volume_encryption_key = module.key_protect_all_inclusive.keys["slz-vsi.${var.prefix}-vsi"].crn
kms_encryption_enabled = true
existing_kms_instance_guid = module.key_protect_all_inclusive.kms_guid
vsi_per_subnet = 1
primary_vni_additional_ip_count = 2
ssh_key_ids = [local.ssh_key_id]
secondary_subnets = local.secondary_subnet_zone_list
secondary_security_groups = local.secondary_security_groups
# Create a floating IPs for the additional VNI
secondary_floating_ips = [
for subnet in local.secondary_subnet_zone_list :
subnet.name
]
# Create a floating IP for each virtual server created
enable_floating_ip = true
secondary_use_vsi_security_group = var.secondary_use_vsi_security_group
# Add 1 additional data volume to each VSI
block_storage_volumes = [
{
name = var.prefix
profile = "10iops-tier"
}]
load_balancers = [
{
name = "${var.prefix}-lb"
type = "public"
listener_port = 9080
listener_protocol = "http"
connection_limit = 100
idle_connection_timeout = 50
algorithm = "round_robin"
protocol = "http"
health_delay = 60
health_retries = 5
health_timeout = 30
health_type = "http"
pool_member_port = 8080
},
{
name = "${var.prefix}-nlb"
type = "public"
profile = "network-fixed"
listener_port = 3128
listener_protocol = "tcp"
algorithm = "round_robin"
protocol = "tcp"
health_delay = 60
health_retries = 5
health_timeout = 30
health_type = "tcp"
pool_member_port = 3120
}
]
}