-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.tf
223 lines (194 loc) · 7.66 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
terraform {
required_version = ">= 1.3"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.95.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.6.0"
}
}
}
provider "azurerm" {
features {}
}
locals {
containers_role_assignments = flatten([
for i in var.containers : [
for a in i.role_assignments : {
principal_id = a.principal_id
role_definition_name = a.role_definition_name
container_name = i.name
}
]
])
default_event_rule = {
event_delivery_schema = null
labels = null
filters = null
eventhub_id = null
service_bus_topic_id = null
service_bus_queue_id = null
included_event_types = null
}
merged_events = [for event in var.events : merge(local.default_event_rule, event)]
diag_resource_list = var.diagnostics != null ? split("/", var.diagnostics.destination) : []
parsed_diag = var.diagnostics != null ? {
log_analytics_id = contains(local.diag_resource_list, "Microsoft.OperationalInsights") ? var.diagnostics.destination : null
storage_account_id = contains(local.diag_resource_list, "Microsoft.Storage") ? var.diagnostics.destination : null
event_hub_auth_id = contains(local.diag_resource_list, "Microsoft.EventHub") ? var.diagnostics.destination : null
metric = var.diagnostics.metrics
log = var.diagnostics.logs
} : {
log_analytics_id = null
storage_account_id = null
event_hub_auth_id = null
metric = []
log = []
}
randomized_name = format("%s%ssa", lower(replace(var.name, "/[[:^alnum:]]/", "")), random_string.unique.result)
name = var.exact_name ? var.name : local.randomized_name
}
resource "azurerm_resource_group" "storage" {
count = var.resource_group_create ? 1 : 0
name = var.resource_group_name
location = var.location
tags = var.tags
}
resource "random_string" "unique" {
length = 6
special = false
upper = false
}
resource "azurerm_storage_account" "storage" {
name = local.name
resource_group_name = var.resource_group_name
location = var.location
account_kind = var.account_kind
account_tier = var.account_tier
account_replication_type = var.account_replication_type
access_tier = var.access_tier
enable_https_traffic_only = true
min_tls_version = var.min_tls_version
blob_properties {
delete_retention_policy {
days = var.soft_delete_retention
}
dynamic "cors_rule" {
for_each = var.cors_rule
content {
allowed_origins = cors_rule.value.allowed_origins
allowed_methods = cors_rule.value.allowed_methods
allowed_headers = cors_rule.value.allowed_headers
exposed_headers = cors_rule.value.exposed_headers
max_age_in_seconds = cors_rule.value.max_age_in_seconds
}
}
}
dynamic "network_rules" {
for_each = var.network_rules != null ? ["true"] : []
content {
default_action = "Deny"
ip_rules = var.network_rules.ip_rules
virtual_network_subnet_ids = var.network_rules.subnet_ids
bypass = var.network_rules.bypass
}
}
tags = var.tags
}
resource "azurerm_advanced_threat_protection" "threat_protection" {
count = var.enable_advanced_threat_protection ? 1 : 0
target_resource_id = azurerm_storage_account.storage.id
enabled = var.enable_advanced_threat_protection
}
resource "azurerm_storage_container" "storage" {
count = length(var.containers)
name = var.containers[count.index].name
storage_account_name = azurerm_storage_account.storage.name
container_access_type = var.containers[count.index].access_type
}
resource "azurerm_role_assignment" "main" {
for_each = {
for i in var.role_assignments : format("%s-%s", i.principal_id, replace(i.role_definition_name, " ", "")) => i
}
principal_id = each.value.principal_id
role_definition_name = each.value.role_definition_name
scope = azurerm_storage_account.storage.id
}
resource "azurerm_role_assignment" "containers" {
for_each = {
for i in local.containers_role_assignments : format("%s-%s-%s", i.container_name, i.principal_id, replace(i.role_definition_name, " ", "")) => i
}
principal_id = each.value.principal_id
role_definition_name = each.value.role_definition_name
scope = format("%s/blobServices/default/containers/%s", azurerm_storage_account.storage.id, each.value.container_name)
}
resource "azurerm_eventgrid_event_subscription" "storage" {
count = length(local.merged_events)
name = local.merged_events[count.index].name
scope = azurerm_storage_account.storage.id
event_delivery_schema = local.merged_events[count.index].event_delivery_schema
labels = local.merged_events[count.index].labels
included_event_types = local.merged_events[count.index].included_event_types
eventhub_endpoint_id = local.merged_events[count.index].eventhub_id
service_bus_topic_endpoint_id = local.merged_events[count.index].service_bus_topic_id
service_bus_queue_endpoint_id = local.merged_events[count.index].service_bus_queue_id
dynamic "subject_filter" {
for_each = local.merged_events[count.index].filters == null ? [] : [true]
content {
subject_begins_with = lookup(local.merged_events[count.index].filters, "subject_begins_with", null) == null ? null : var.events[count.index].filters.subject_begins_with
subject_ends_with = lookup(local.merged_events[count.index].filters, "subject_ends_with", null) == null ? null : var.events[count.index].filters.subject_ends_with
}
}
}
resource "azurerm_storage_management_policy" "storage" {
count = length(var.lifecycles) == 0 ? 0 : 1
storage_account_id = azurerm_storage_account.storage.id
dynamic "rule" {
for_each = var.lifecycles
iterator = rule
content {
name = "rule${rule.key}"
enabled = true
filters {
prefix_match = rule.value.prefix_match
blob_types = ["blockBlob"]
}
actions {
base_blob {
delete_after_days_since_modification_greater_than = rule.value.delete_after_days
}
}
}
}
}
data "azurerm_monitor_diagnostic_categories" "default" {
resource_id = "${azurerm_storage_account.storage.id}/blobServices/default"
}
resource "azurerm_monitor_diagnostic_setting" "sa" {
count = var.diagnostics != null ? 1 : 0
name = "${var.name}-sa-diag"
target_resource_id = "${azurerm_storage_account.storage.id}/blobServices/default"
log_analytics_workspace_id = local.parsed_diag.log_analytics_id
eventhub_authorization_rule_id = local.parsed_diag.event_hub_auth_id
eventhub_name = local.parsed_diag.event_hub_auth_id != null ? var.diagnostics.eventhub_name : null
storage_account_id = local.parsed_diag.storage_account_id
dynamic "enabled_log" {
for_each = {
for k, v in data.azurerm_monitor_diagnostic_categories.default.log_category_types : k => v
if contains(local.parsed_diag.log, "all") || contains(local.parsed_diag.log, v)
}
content {
category = enabled_log.value
}
}
dynamic "metric" {
for_each = data.azurerm_monitor_diagnostic_categories.default.metrics
content {
category = metric.value
enabled = contains(local.parsed_diag.metric, "all") || contains(local.parsed_diag.metric, metric.value)
}
}
}