-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_vm.tf
246 lines (208 loc) · 7.57 KB
/
create_vm.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#
# Must be dynamic
#
variable "cluster_id" {
default = "cacib-ocp-disconnected-install"
}
variable "ssh_key_private" {
default = "~/.ssh/terraform_id_rsa"
}
locals {
tags = merge(
{
"kubernetes.io_cluster.${var.cluster_id}" = "owned"
},
var.azure_extra_tags,
)
}
variable "azure_bastion_vm_size" {
default = "Standard_A2m_v2"
}
variable "azure_master_vm_size" {
default = "Standard_DS3_v2"
}
variable "azure_node_vm_size" {
default = "Standard_DS3_v2"
}
module "bootstrap" {
source = "./bootstrap"
resource_group_name = azurerm_resource_group.main.name
region = var.azure_region
vm_size = var.azure_bootstrap_vm_type
# vm_image = azurerm_image.cluster.id
# identity = azurerm_user_assigned_identity.main.id
# subnet_id = module.vnet.master_subnet_id
public_subnet_id = azurerm_subnet.cacib_ocp_public_subnet.id
private_subnet_id = azurerm_subnet.myterraformprivatesubnet.id
public_ip_address_id = azurerm_public_ip.myterraformpublicip.id
public_ip_address_name = azurerm_public_ip.myterraformpublicip.name
tags = local.tags
# storage_account = azurerm_storage_account.cluster
# nsg_name = module.vnet.master_nsg_name
storage_account = azurerm_storage_account.mystorageaccount
nsg_name = azurerm_network_security_group.myterraformnsg.id
ssh_key_private = var.ssh_key_private
admin_username = "ocpadmin"
}
module "bastion" {
source = "./bastion"
resource_group_name = azurerm_resource_group.main.name
region = var.azure_region
vm_size = var.azure_bastion_vm_size
public_subnet_id = azurerm_subnet.cacib_ocp_public_subnet.id
private_subnet_id = azurerm_subnet.myterraformprivatesubnet.id
tags = local.tags
storage_account = azurerm_storage_account.mystorageaccount
nsg_name = azurerm_network_security_group.myterraformnsg.id
ssh_key_private = var.ssh_key_private
admin_username = "ocpadmin"
}
module "master" {
source = "./master"
resource_group_name = azurerm_resource_group.main.name
region = var.azure_region
vm_size = var.azure_master_vm_size
public_subnet_id = azurerm_subnet.cacib_ocp_public_subnet.id
private_subnet_id = azurerm_subnet.myterraformprivatesubnet.id
tags = local.tags
storage_account = azurerm_storage_account.mystorageaccount
nsg_name = azurerm_network_security_group.myterraformnsg.id
instance_count = var.master_count
#
# TODO: Refactor this
#
address_space = var.address_space
ssh_key_private = var.ssh_key_private
admin_username = "ocpadmin"
}
module "node" {
source = "./node"
resource_group_name = azurerm_resource_group.main.name
region = var.azure_region
vm_size = var.azure_node_vm_size
public_subnet_id = azurerm_subnet.cacib_ocp_public_subnet.id
private_subnet_id = azurerm_subnet.myterraformprivatesubnet.id
tags = local.tags
storage_account = azurerm_storage_account.mystorageaccount
nsg_name = azurerm_network_security_group.myterraformnsg.id
instance_count = var.node_count
#
# TODO: Refactor this
#
address_space = var.address_space
ssh_key_private = var.ssh_key_private
admin_username = "ocpadmin"
}
# Create a resource group if it doesn’t exist
variable "address_space" {
default = "10.5.0.0/16"
}
variable "admin_username" {
default = "ocpadmin"
}
resource "null_resource" "reset_ip_addresses" {
provisioner "local-exec" {
command = "rm -f private-ips.txt"
}
}
resource "azurerm_resource_group" "main" {
name = "${var.cluster_id}-rg"
location = var.azure_region
tags = local.tags
}
# Create virtual network
resource "azurerm_virtual_network" "myterraformnetwork" {
name = "cacib-ocp-public-vnet"
address_space = [var.address_space]
location = var.azure_region
resource_group_name = "${azurerm_resource_group.main.name}"
tags = {
environment = "CA-CIB OCP Terraform Setup"
}
}
# Create primary subnet
resource "azurerm_subnet" "cacib_ocp_public_subnet" {
name = "cacib-ocp-public-subnet"
resource_group_name = "${azurerm_resource_group.main.name}"
virtual_network_name = "${azurerm_virtual_network.myterraformnetwork.name}"
address_prefix = cidrsubnet(var.address_space, 8, 1)
}
# Create private subnet
resource "azurerm_subnet" "myterraformprivatesubnet" {
name = "cacib-ocp-private-subnet"
resource_group_name = "${azurerm_resource_group.main.name}"
virtual_network_name = "${azurerm_virtual_network.myterraformnetwork.name}"
route_table_id = "${azurerm_route_table.cacibprivateroutetable.id}"
address_prefix = cidrsubnet(var.address_space, 8, 2)
}
# Create public IPs
resource "azurerm_public_ip" "myterraformpublicip" {
name = "cacib-sat-publicIP"
location = var.azure_region
resource_group_name = "${azurerm_resource_group.main.name}"
allocation_method = "Dynamic"
tags = {
environment = "CA-CIB OCP Terraform Setup"
}
}
# Create Network Security Group and rule
resource "azurerm_network_security_group" "myterraformnsg" {
name = "cacib-ocp-public-sg"
location = var.azure_region
resource_group_name = "${azurerm_resource_group.main.name}"
security_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "HTTPS"
priority = 1011
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "443"
source_address_prefix = "*"
destination_address_prefix = "*"
}
security_rule {
name = "HTTP"
priority = 1021
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
environment = "CA-CIB OCP Terraform Setup"
}
}
# Generate random text for a unique storage account name
resource "random_id" "randomId" {
keepers = {
# Generate a new ID only when a new resource group is defined
resource_group = "${azurerm_resource_group.main.name}"
}
byte_length = 8
}
# Create storage account for boot diagnostics
resource "azurerm_storage_account" "mystorageaccount" {
name = "diag${random_id.randomId.hex}"
resource_group_name = "${azurerm_resource_group.main.name}"
location = var.azure_region
account_tier = "Standard"
account_replication_type = "LRS"
tags = {
environment = "CA-CIB OCP Terraform Setup"
}
}