-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
190 lines (157 loc) · 5.79 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
data "azurerm_client_config" "current" {
}
// data "azurerm_resource_group" "hub" {
// name = var.resource_group
// depends_on = [var.module_depends_on]
// }
locals {
//location = var.location != "" ? var.location : data.azurerm_resource_group.hub.location
//tags = merge(data.azurerm_resource_group.hub.tags, var.tags)
ssh_public_keys = {
for object in var.ssh_public_keys :
object.username => file(object.ssh_public_key_file)
}
}
resource "random_string" "hub" {
length = 10
special = false
upper = false
lower = true
number = true
}
data "azurerm_resource_group" "shared" {
name = var.resource_group_name
}
resource "azurerm_key_vault" "hub" {
depends_on = [var.module_depends_on]
name = "${substr(var.key_vault_name, 0, 13)}-${random_string.hub.result}"
resource_group_name = data.azurerm_resource_group.shared.name
location = length(var.location) > 0 ? var.location : data.azurerm_resource_group.shared.location
tags = length(var.tags) > 0 ? var.tags : data.azurerm_resource_group.shared.tags
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
enabled_for_deployment = false // Change manually or parameterise?
enabled_for_template_deployment = false // Change manually or parameterise?
enabled_for_disk_encryption = false // Change manually or parameterise?
access_policy {
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id
key_permissions = [
"Create",
"Get",
"List",
"Update",
]
secret_permissions = [
"Set",
"Get",
"List",
"Delete",
]
}
}
resource "random_pet" "example" {}
resource "azurerm_key_vault_secret" "example" {
key_vault_id = azurerm_key_vault.hub.id
name = "example-secret"
content_type = "example"
value = random_pet.example.id
}
resource "azurerm_key_vault_secret" "ssh_pub_key" {
// Loop through the list and create a secret for each user and their public key
// for_)each only works on simple maps or sets, so generate one
key_vault_id = azurerm_key_vault.hub.id
for_each = local.ssh_public_keys
name = each.key
value = each.value
content_type = "ssh-pub-key"
}
resource "null_resource" "ssh_pub_key_sleep" {
# Any changes to these key vault secrets will trigger a sleep
triggers = local.ssh_public_keys
provisioner "local-exec" {
command = "sleep 10"
}
}
resource "azurerm_log_analytics_workspace" "hub" {
depends_on = [var.module_depends_on]
name = "${var.workspace_name}-${random_string.hub.result}"
resource_group_name = data.azurerm_resource_group.shared.name
location = length(var.location) > 0 ? var.location : data.azurerm_resource_group.shared.location
tags = length(var.tags) > 0 ? var.tags : data.azurerm_resource_group.shared.tags
sku = "PerGB2018"
retention_in_days = var.workspace_retention
}
resource "azurerm_key_vault_secret" "hub_workspace_name" {
key_vault_id = azurerm_key_vault.hub.id
name = "hub-workspace-name"
value = azurerm_log_analytics_workspace.hub.name
content_type = "workspace-name"
}
resource "azurerm_key_vault_secret" "hub_workspace_key" {
key_vault_id = azurerm_key_vault.hub.id
name = "hub-workspace-key"
value = azurerm_log_analytics_workspace.hub.secondary_shared_key
content_type = "workspace-key"
}
resource "azurerm_storage_account" "diags" {
depends_on = [var.module_depends_on]
resource_group_name = data.azurerm_resource_group.shared.name
name = "${substr(lower(var.diagnostics_storage_account), 0, 14)}${random_string.hub.result}"
location = length(var.location) > 0 ? var.location : data.azurerm_resource_group.shared.location
tags = length(var.tags) > 0 ? var.tags : data.azurerm_resource_group.shared.tags
// tenant_id = data.azurerm_client_config.current.tenant_id
account_kind = "StorageV2"
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_key_vault_secret" "hub_diags_name" {
key_vault_id = azurerm_key_vault.hub.id
name = "hub-diags-name"
value = azurerm_storage_account.diags.name
content_type = "storage-name"
}
resource "azurerm_key_vault_secret" "hub_diags_key" {
key_vault_id = azurerm_key_vault.hub.id
name = "hub-diags-key"
value = azurerm_storage_account.diags.secondary_access_key
content_type = "storage-key"
}
resource "azurerm_recovery_services_vault" "hub" {
depends_on = [var.module_depends_on]
name = var.recovery_vault_name
resource_group_name = data.azurerm_resource_group.shared.name
location = length(var.location) > 0 ? var.location : data.azurerm_resource_group.shared.location
tags = length(var.tags) > 0 ? var.tags : data.azurerm_resource_group.shared.tags
// tenant_id = data.azurerm_client_config.current.tenant_id
sku = "Standard"
soft_delete_enabled = true
}
resource "azurerm_backup_policy_vm" "default" {
name = "default"
resource_group_name = data.azurerm_resource_group.shared.name
recovery_vault_name = azurerm_recovery_services_vault.hub.name
timezone = "UTC"
backup {
frequency = "Daily"
time = "23:00"
}
retention_daily {
count = 14 //Between 1 & 9999
}
retention_weekly {
count = 13
weekdays = ["Wednesday", "Sunday"]
}
retention_monthly {
count = 12
weekdays = ["Sunday"]
weeks = ["Last"]
}
retention_yearly {
count = 3
weekdays = ["Sunday"]
weeks = ["Last"]
months = ["January"]
}
}