-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathmain.tf
More file actions
379 lines (322 loc) · 11.7 KB
/
main.tf
File metadata and controls
379 lines (322 loc) · 11.7 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
terraform {
required_version = "~> 1.6"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.116, < 5.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.6"
}
}
}
# tflint-ignore: terraform_module_provider_declaration, terraform_output_separate, terraform_variable_separate
provider "azurerm" {
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}
module "naming" {
source = "Azure/naming/azurerm"
version = "~> 0.4"
}
module "regions" {
source = "Azure/avm-utl-regions/azurerm"
version = "0.3.0"
availability_zones_filter = true
}
locals {
#deployment_region = module.regions.regions[random_integer.region_index.result].name
deployment_region = "canadacentral" #temporarily pinning on single region
tags = {
scenario = "Default"
}
}
resource "random_integer" "region_index" {
max = length(module.regions.regions_by_name) - 1
min = 0
}
resource "random_integer" "zone_index" {
max = length(module.regions.regions_by_name[local.deployment_region].zones)
min = 1
}
resource "azurerm_resource_group" "this_rg" {
location = local.deployment_region
name = module.naming.resource_group.name_unique
tags = local.tags
}
module "vm_sku" {
source = "Azure/avm-utl-sku-finder/azapi"
version = "0.3.0"
location = azurerm_resource_group.this_rg.location
cache_results = true
vm_filters = {
min_vcpus = 2
max_vcpus = 2
encryption_at_host_supported = true
accelerated_networking_enabled = true
premium_io_supported = true
location_zone = random_integer.zone_index.result
}
depends_on = [random_integer.zone_index]
}
module "natgateway" {
source = "Azure/avm-res-network-natgateway/azurerm"
version = "0.2.0"
name = module.naming.nat_gateway.name_unique
enable_telemetry = true
location = azurerm_resource_group.this_rg.location
resource_group_name = azurerm_resource_group.this_rg.name
public_ips = {
public_ip_1 = {
name = "nat_gw_pip1"
}
}
}
module "vnet" {
source = "Azure/avm-res-network-virtualnetwork/azurerm"
version = "=0.7.1"
resource_group_name = azurerm_resource_group.this_rg.name
address_space = ["10.0.0.0/16"]
name = module.naming.virtual_network.name_unique
location = azurerm_resource_group.this_rg.location
subnets = {
vm_subnet_1 = {
name = "${module.naming.subnet.name_unique}-1"
address_prefixes = ["10.0.1.0/24"]
nat_gateway = {
id = module.natgateway.resource_id
}
}
vm_subnet_2 = {
name = "${module.naming.subnet.name_unique}-2"
address_prefixes = ["10.0.2.0/24"]
nat_gateway = {
id = module.natgateway.resource_id
}
}
AzureBastionSubnet = {
name = "AzureBastionSubnet"
address_prefixes = ["10.0.3.0/24"]
}
}
}
/* #uncomment these resources to enable bastion
resource "azurerm_public_ip" "bastionpip" {
allocation_method = "Static"
location = azurerm_resource_group.this_rg.location
name = module.naming.public_ip.name_unique
resource_group_name = azurerm_resource_group.this_rg.name
sku = "Standard"
}
resource "azurerm_bastion_host" "bastion" {
location = azurerm_resource_group.this_rg.location
name = module.naming.bastion_host.name_unique
resource_group_name = azurerm_resource_group.this_rg.name
ip_configuration {
name = "${module.naming.bastion_host.name_unique}-ipconf"
public_ip_address_id = azurerm_public_ip.bastionpip.id
subnet_id = module.vnet.subnets["AzureBastionSubnet"].resource_id
}
}
*/
data "azurerm_client_config" "current" {}
module "avm_res_keyvault_vault" {
source = "Azure/avm-res-keyvault-vault/azurerm"
version = "=0.9.1"
tenant_id = data.azurerm_client_config.current.tenant_id
name = module.naming.key_vault.name_unique
resource_group_name = azurerm_resource_group.this_rg.name
location = azurerm_resource_group.this_rg.location
enabled_for_disk_encryption = true
network_acls = {
default_action = "Allow"
bypass = "AzureServices"
}
role_assignments = {
deployment_user_secrets = { #give the deployment user access to secrets
role_definition_id_or_name = "Key Vault Secrets Officer"
principal_id = data.azurerm_client_config.current.object_id
}
}
wait_for_rbac_before_secret_operations = {
create = "60s"
}
tags = local.tags
}
resource "azurerm_storage_account" "app_account" {
account_replication_type = "ZRS"
account_tier = "Standard"
location = azurerm_resource_group.this_rg.location
name = module.naming.storage_account.name_unique
resource_group_name = azurerm_resource_group.this_rg.name
}
resource "azurerm_storage_container" "app_container" {
name = module.naming.storage_container.name_unique
container_access_type = "blob"
storage_account_name = azurerm_storage_account.app_account.name
}
resource "azurerm_storage_blob" "app" {
name = "install-script.ps1"
storage_account_name = azurerm_storage_account.app_account.name
storage_container_name = azurerm_storage_container.app_container.name
type = "Block"
source = "${path.module}/install-vscode.ps1"
}
#blob content = file
resource "azurerm_shared_image_gallery" "app_gallery" {
location = azurerm_resource_group.this_rg.location
name = module.naming.shared_image_gallery.name_unique
resource_group_name = azurerm_resource_group.this_rg.name
tags = local.tags
}
resource "azurerm_gallery_application" "app_gallery_sample" {
gallery_id = azurerm_shared_image_gallery.app_gallery.id
location = azurerm_resource_group.this_rg.location
name = "VSCode"
supported_os_type = "Windows"
}
resource "azurerm_gallery_application_version" "test_app_version" {
gallery_application_id = azurerm_gallery_application.app_gallery_sample.id
location = azurerm_gallery_application.app_gallery_sample.location
name = "0.1.0"
package_file = "install-script.ps1"
manage_action {
install = "powershell.exe -command ./install-script.ps1"
remove = "powershell.exe -command ./install-script.ps1 -mode uninstall"
}
source {
media_link = azurerm_storage_blob.app.id
}
target_region {
name = azurerm_gallery_application.app_gallery_sample.location
regional_replica_count = 1
}
}
resource "azurerm_resource_group" "rsv_rg" {
location = local.deployment_region
name = "RSV-rg"
tags = local.tags
}
resource "azurerm_recovery_services_vault" "test_vault" {
location = azurerm_resource_group.rsv_rg.location
name = module.naming.recovery_services_vault.name_unique
resource_group_name = azurerm_resource_group.rsv_rg.name
sku = "Standard"
soft_delete_enabled = false
storage_mode_type = "LocallyRedundant"
}
resource "azurerm_backup_policy_vm" "test_policy" {
name = "${module.naming.recovery_services_vault.name_unique}-test-policy"
recovery_vault_name = azurerm_recovery_services_vault.test_vault.name
resource_group_name = azurerm_resource_group.rsv_rg.name
backup {
frequency = "Daily"
time = "23:00"
}
retention_daily {
count = 10
}
}
resource "azurerm_maintenance_configuration" "test_maintenance_config" {
location = azurerm_resource_group.this_rg.location
name = "${module.naming.virtual_machine.name_unique}-test-maint-config"
resource_group_name = azurerm_resource_group.this_rg.name
scope = "InGuestPatch"
in_guest_user_patch_mode = "User"
install_patches {
reboot = "Always"
windows {
classifications_to_include = ["Critical", "Security", "UpdateRollup"]
}
}
window {
start_date_time = formatdate("YYYY-MM-DD hh:mm", timeadd(timestamp(), "30m"))
time_zone = "Pacific Standard Time"
duration = "04:00"
recur_every = "Month Second Friday"
}
lifecycle {
ignore_changes = [window[0].start_date_time]
}
}
module "testvm" {
source = "../../"
#source = "Azure/avm-res-compute-virtualmachine/azurerm"
#version = "0.17.0
enable_telemetry = var.enable_telemetry
location = azurerm_resource_group.this_rg.location
resource_group_name = azurerm_resource_group.this_rg.name
os_type = "Windows"
name = module.naming.virtual_machine.name_unique
sku_size = module.vm_sku.sku
encryption_at_host_enabled = true
zone = random_integer.zone_index.result
patch_assessment_mode = "AutomaticByPlatform"
patch_mode = "AutomaticByPlatform"
bypass_platform_safety_checks_on_user_schedule_enabled = true
generated_secrets_key_vault_secret_config = {
key_vault_resource_id = module.avm_res_keyvault_vault.resource_id
}
os_disk = {
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
data_disk_managed_disks = {
disk1 = {
name = "${module.naming.managed_disk.name_unique}-lun0"
storage_account_type = "Premium_LRS"
lun = 0
caching = "ReadWrite"
disk_size_gb = 32
}
}
source_image_reference = {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2022-datacenter-g2"
version = "latest"
}
network_interfaces = {
network_interface_1 = {
name = module.naming.network_interface.name_unique
ip_configurations = {
ip_configuration_1 = {
name = "${module.naming.network_interface.name_unique}-ipconfig1"
private_ip_subnet_resource_id = module.vnet.subnets["vm_subnet_1"].resource_id
}
}
role_assignments = {
role_assignment_1 = {
principal_id = data.azurerm_client_config.current.client_id
role_definition_id_or_name = "Contributor"
description = "Assign the Contributor role to the deployment user on this network interface resource scope."
principal_type = "ServicePrincipal"
}
}
}
}
gallery_applications = {
vscode = {
version_id = azurerm_gallery_application_version.test_app_version.id
order = 1
}
}
azure_backup_configurations = {
backup_config = {
recovery_vault_resource_id = azurerm_recovery_services_vault.test_vault.id
recovery_vault_name = azurerm_recovery_services_vault.test_vault.name
resource_group_name = azurerm_recovery_services_vault.test_vault.resource_group_name
backup_policy_resource_id = azurerm_backup_policy_vm.test_policy.id
exclude_disk_luns = [0]
}
}
maintenance_configuration_resource_ids = {
config_1 = azurerm_maintenance_configuration.test_maintenance_config.id
}
tags = local.tags
depends_on = [module.avm_res_keyvault_vault]
}