forked from pauldotyu/azure-avdops-terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
242 lines (202 loc) · 7.32 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
229
230
231
232
233
234
235
236
237
238
239
240
241
provider "azurerm" {
features {}
}
data "azurerm_client_config" "current" {}
resource "random_pet" "avd" {
length = 2
separator = ""
}
data "azurerm_subscription" "current" {}
##############################################
# AZURE IMAGE BUILDER
##############################################
resource "azurerm_resource_group" "avd" {
name = "rg-${random_pet.avd.id}"
location = var.location
tags = var.tags
}
resource "azurerm_user_assigned_identity" "avd" {
name = "msi-${random_pet.avd.id}"
resource_group_name = azurerm_resource_group.avd.name
location = azurerm_resource_group.avd.location
tags = var.tags
}
resource "azurerm_role_definition" "avd" {
name = "role-${random_pet.avd.id}"
scope = data.azurerm_subscription.current.id
description = "Azure Image Builder access to create resources for the image build"
permissions {
actions = [
"Microsoft.Compute/galleries/read",
"Microsoft.Compute/galleries/images/read",
"Microsoft.Compute/galleries/images/versions/read",
"Microsoft.Compute/galleries/images/versions/write",
"Microsoft.Compute/images/write",
"Microsoft.Compute/images/read",
"Microsoft.Compute/images/delete"
]
not_actions = []
}
assignable_scopes = [
data.azurerm_subscription.current.id,
azurerm_resource_group.avd.id
]
}
resource "azurerm_role_assignment" "avd" {
scope = azurerm_resource_group.avd.id
role_definition_id = azurerm_role_definition.avd.role_definition_resource_id
principal_id = azurerm_user_assigned_identity.avd.principal_id
}
resource "azurerm_shared_image_gallery" "avd" {
name = "sig${random_pet.avd.id}"
resource_group_name = azurerm_resource_group.avd.name
location = azurerm_resource_group.avd.location
tags = var.tags
}
resource "azurerm_shared_image" "avd" {
name = "avd-${random_pet.avd.id}"
gallery_name = azurerm_shared_image_gallery.avd.name
resource_group_name = azurerm_resource_group.avd.name
location = azurerm_resource_group.avd.location
os_type = "Windows"
#hyper_v_generation = "V2"
identifier {
publisher = var.publisher
offer = var.offer
sku = "avd-${random_pet.avd.id}"
}
}
##############################################
# AZURE FILES
##############################################
resource "azurerm_storage_account" "avd" {
name = "sa${random_pet.avd.id}"
resource_group_name = azurerm_resource_group.avd.name
location = azurerm_resource_group.avd.location
account_tier = "Premium"
account_kind = "FileStorage"
account_replication_type = "LRS"
allow_blob_public_access = false
}
resource "azurerm_storage_share" "avd" {
name = "profiles"
storage_account_name = azurerm_storage_account.avd.name
quota = 100
}
#################################################
# AZURE EVENT GRID FOR SECURE AIB
#################################################
data "azuread_group" "aib" {
display_name = "AIB"
security_enabled = true
}
resource "azurerm_storage_account" "avd_installs" {
name = "sa${random_pet.avd.id}installs"
resource_group_name = azurerm_resource_group.avd.name
location = azurerm_resource_group.avd.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_role_assignment" "example" {
scope = azurerm_storage_account.avd_installs.id
role_definition_name = "Storage Blob Data Reader"
principal_id = data.azuread_group.aib.object_id
}
resource "azurerm_storage_account" "avd_func" {
name = "func${random_pet.avd.id}storage"
resource_group_name = azurerm_resource_group.avd.name
location = azurerm_resource_group.avd.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_application_insights" "avd" {
name = "func${random_pet.avd.id}-appinsights"
resource_group_name = azurerm_resource_group.avd.name
location = azurerm_resource_group.avd.location
application_type = "web"
}
resource "azurerm_app_service_plan" "avd" {
name = "func${random_pet.avd.id}-plan"
resource_group_name = azurerm_resource_group.avd.name
location = azurerm_resource_group.avd.location
kind = "FunctionApp"
sku {
tier = "Dynamic"
size = "Y1"
}
}
resource "azurerm_function_app" "avd" {
name = "func${random_pet.avd.id}"
resource_group_name = azurerm_resource_group.avd.name
location = azurerm_resource_group.avd.location
app_service_plan_id = azurerm_app_service_plan.avd.id
storage_account_name = azurerm_storage_account.avd_func.name
storage_account_access_key = azurerm_storage_account.avd_func.primary_access_key
identity {
type = "SystemAssigned"
}
app_settings = {
FUNCTIONS_WORKER_RUNTIME = "powershell"
FUNCTIONS_WORKER_RUNTIME_VERSION = "~7"
APPINSIGHTS_INSTRUMENTATIONKEY = azurerm_application_insights.avd.instrumentation_key
APPLICATIONINSIGHTS_CONNECTION_STRING = azurerm_application_insights.avd.connection_string
}
version = "~3"
}
resource "azurerm_role_assignment" "avd_func" {
scope = data.azurerm_subscription.current.id
role_definition_name = "Contributor"
principal_id = azurerm_function_app.avd.identity[0].principal_id
}
resource "azurerm_eventgrid_system_topic" "avd" {
name = "eg-${random_pet.avd.id}Topic"
resource_group_name = azurerm_resource_group.avd.name
location = "Global"
source_arm_resource_id = data.azurerm_subscription.current.id
topic_type = "Microsoft.Resources.Subscriptions"
}
# this cannot be done until the function app has been deployed
resource "azurerm_eventgrid_system_topic_event_subscription" "avd" {
name = "eg-${random_pet.avd.id}Subscription"
resource_group_name = azurerm_resource_group.avd.name
system_topic = azurerm_eventgrid_system_topic.avd.name
event_delivery_schema = "EventGridSchema"
included_event_types = [
"Microsoft.Resources.ResourceWriteSuccess"
]
advanced_filter {
string_contains {
key = "data.resourceProvider"
values = [
"Microsoft.Compute"
]
}
string_contains {
key = "data.operationName"
values = [
"Microsoft.Compute/virtualMachines/write"
]
}
string_contains {
key = "data.authorization.evidence.principalId"
values = [
azurerm_user_assigned_identity.avd.principal_id
]
}
}
azure_function_endpoint {
function_id = "${azurerm_function_app.avd.id}/functions/AIBIdentity"
max_events_per_batch = 1
preferred_batch_size_in_kilobytes = 64
}
retry_policy {
max_delivery_attempts = 30
event_time_to_live = 1440
}
}
# resource "azurerm_automation_account" "avd" {
# name = "aa-${random_pet.avd.id}"
# location = azurerm_resource_group.avd.location
# resource_group_name = azurerm_resource_group.avd.name
# sku_name = "Basic"
# }