forked from kumarvna/terraform-azurerm-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
150 lines (136 loc) · 5.57 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
#-------------------------------
# Local Declarations
#-------------------------------
locals {
account_tier = (var.account_kind == "FileStorage" ? "Premium" : split("_", var.skuname)[0])
account_replication_type = (local.account_tier == "Premium" ? "LRS" : split("_", var.skuname)[1])
resource_group_name = element(coalescelist(data.azurerm_resource_group.rgrp.*.name, azurerm_resource_group.rg.*.name, [""]), 0)
location = element(coalescelist(data.azurerm_resource_group.rgrp.*.location, azurerm_resource_group.rg.*.location, [""]), 0)
}
#---------------------------------------------------------
# Resource Group Creation or selection - Default is "false"
#----------------------------------------------------------
data "azurerm_resource_group" "rgrp" {
count = var.create_resource_group == false ? 1 : 0
name = var.resource_group_name
}
resource "azurerm_resource_group" "rg" {
count = var.create_resource_group ? 1 : 0
name = lower(var.resource_group_name)
location = var.location
tags = merge({ "ResourceName" = format("%s", var.resource_group_name) }, var.tags, )
}
#---------------------------------------------------------
# Storage Account Creation or selection
#----------------------------------------------------------
resource "random_string" "unique" {
length = 6
special = false
upper = false
}
resource "azurerm_storage_account" "storeacc" {
name = lower(replace(var.storage_account_name, "/[[:^alnum:]]/", ""))
resource_group_name = local.resource_group_name
location = local.location
account_kind = var.account_kind
account_tier = local.account_tier
account_replication_type = local.account_replication_type
enable_https_traffic_only = true
min_tls_version = var.min_tls_version
allow_blob_public_access = var.enable_advanced_threat_protection == true ? true : false
tags = var.tags
identity {
type = var.identity_ids != null ? "SystemAssigned, UserAssigned" : "SystemAssigned"
identity_ids = var.identity_ids
}
blob_properties {
delete_retention_policy {
days = var.blob_soft_delete_retention_days
}
container_delete_retention_policy {
days = var.container_soft_delete_retention_days
}
versioning_enabled = var.enable_versioning
last_access_time_enabled = var.last_access_time_enabled
change_feed_enabled = var.change_feed_enabled
}
dynamic "network_rules" {
for_each = var.network_rules != null ? ["true"] : []
content {
default_action = "Deny"
bypass = var.network_rules.bypass
ip_rules = var.network_rules.ip_rules
virtual_network_subnet_ids = var.network_rules.subnet_ids
}
}
}
#--------------------------------------
# Storage Advanced Threat Protection
#--------------------------------------
resource "azurerm_advanced_threat_protection" "atp" {
target_resource_id = azurerm_storage_account.storeacc.id
enabled = var.enable_advanced_threat_protection
}
#-------------------------------
# Storage Container Creation
#-------------------------------
resource "azurerm_storage_container" "container" {
count = length(var.containers_list)
name = var.containers_list[count.index].name
storage_account_name = azurerm_storage_account.storeacc.name
container_access_type = var.containers_list[count.index].access_type
}
#-------------------------------
# Storage Fileshare Creation
#-------------------------------
resource "azurerm_storage_share" "fileshare" {
count = length(var.file_shares)
name = var.file_shares[count.index].name
storage_account_name = azurerm_storage_account.storeacc.name
quota = var.file_shares[count.index].quota
}
#-------------------------------
# Storage Tables Creation
#-------------------------------
resource "azurerm_storage_table" "tables" {
count = length(var.tables)
name = var.tables[count.index]
storage_account_name = azurerm_storage_account.storeacc.name
}
#-------------------------------
# Storage Queue Creation
#-------------------------------
resource "azurerm_storage_queue" "queues" {
count = length(var.queues)
name = var.queues[count.index]
storage_account_name = azurerm_storage_account.storeacc.name
}
#-------------------------------
# Storage Lifecycle Management
#-------------------------------
resource "azurerm_storage_management_policy" "lcpolicy" {
count = length(var.lifecycles) == 0 ? 0 : 1
storage_account_id = azurerm_storage_account.storeacc.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 {
tier_to_cool_after_days_since_modification_greater_than = rule.value.tier_to_cool_after_days
tier_to_archive_after_days_since_modification_greater_than = rule.value.tier_to_archive_after_days
delete_after_days_since_modification_greater_than = rule.value.delete_after_days
}
snapshot {
delete_after_days_since_creation_greater_than = rule.value.snapshot_delete_after_days
}
}
}
}
}