-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.tf
248 lines (218 loc) · 9.74 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
242
243
244
245
246
247
248
#---------------------------------
# Local declarations
#---------------------------------
locals {
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 ? 0 : 1
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, )
}
data "azurerm_log_analytics_workspace" "logws" {
count = var.log_analytics_workspace_name != null ? 1 : 0
name = var.log_analytics_workspace_name
resource_group_name = local.resource_group_name
}
data "azurerm_storage_account" "storeacc" {
count = var.storage_account_name != null ? 1 : 0
name = var.storage_account_name
resource_group_name = local.resource_group_name
}
#---------------------------------------------------------
# Container Registry Resoruce - Default is "true"
#----------------------------------------------------------
resource "azurerm_container_registry" "main" {
name = format("%s", var.container_registry_config.name)
resource_group_name = local.resource_group_name
location = local.location
admin_enabled = var.container_registry_config.admin_enabled
sku = var.container_registry_config.sku
public_network_access_enabled = var.container_registry_config.public_network_access_enabled
quarantine_policy_enabled = var.container_registry_config.quarantine_policy_enabled
zone_redundancy_enabled = var.container_registry_config.zone_redundancy_enabled
tags = merge({ "Name" = format("%s", var.container_registry_config.name) }, var.tags, )
dynamic "georeplications" {
for_each = var.georeplications
content {
location = georeplications.value.location
zone_redundancy_enabled = georeplications.value.zone_redundancy_enabled
tags = merge({ "Name" = format("%s", "georep-${georeplications.value.location}") }, var.tags, )
}
}
dynamic "network_rule_set" {
for_each = var.network_rule_set != null ? [var.network_rule_set] : []
content {
default_action = lookup(network_rule_set.value, "default_action", "Allow")
dynamic "ip_rule" {
for_each = network_rule_set.value.ip_rule
content {
action = "Allow"
ip_range = ip_rule.value.ip_range
}
}
dynamic "virtual_network" {
for_each = network_rule_set.value.virtual_network
content {
action = "Allow"
subnet_id = virtual_network.value.subnet_id
}
}
}
}
dynamic "retention_policy" {
for_each = var.retention_policy != null ? [var.retention_policy] : []
content {
days = lookup(retention_policy.value, "days", 7)
enabled = lookup(retention_policy.value, "enabled", true)
}
}
dynamic "trust_policy" {
for_each = var.enable_content_trust ? [1] : []
content {
enabled = var.enable_content_trust
}
}
identity {
type = var.identity_ids != null ? "SystemAssigned, UserAssigned" : "SystemAssigned"
identity_ids = var.identity_ids
}
dynamic "encryption" {
for_each = var.encryption != null ? [var.encryption] : []
content {
enabled = true
key_vault_key_id = encryption.value.key_vault_key_id
identity_client_id = encryption.value.identity_client_id
}
}
}
#------------------------------------------------------------
# Container Registry Resoruce Scope map - Default is "false"
#------------------------------------------------------------
resource "azurerm_container_registry_scope_map" "main" {
for_each = var.scope_map != null ? { for k, v in var.scope_map : k => v if v != null } : {}
name = format("%s", each.key)
resource_group_name = local.resource_group_name
container_registry_name = azurerm_container_registry.main.name
actions = each.value["actions"]
}
#------------------------------------------------------------
# Container Registry Token - Default is "false"
#------------------------------------------------------------
resource "azurerm_container_registry_token" "main" {
for_each = var.scope_map != null ? { for k, v in var.scope_map : k => v if v != null } : {}
name = format("%s", "${each.key}-token")
resource_group_name = local.resource_group_name
container_registry_name = azurerm_container_registry.main.name
scope_map_id = element([for k in azurerm_container_registry_scope_map.main : k.id], 0)
enabled = true
}
#------------------------------------------------------------
# Container Registry webhook - Default is "true"
#------------------------------------------------------------
resource "azurerm_container_registry_webhook" "main" {
for_each = var.container_registry_webhooks != null ? { for k, v in var.container_registry_webhooks : k => v if v != null } : {}
name = format("%s", each.key)
resource_group_name = local.resource_group_name
location = local.location
registry_name = azurerm_container_registry.main.name
service_uri = each.value["service_uri"]
actions = each.value["actions"]
status = each.value["status"]
scope = each.value["scope"]
custom_headers = each.value["custom_headers"]
lifecycle {
ignore_changes = [
tags
]
}
}
#---------------------------------------------------------
# Private Link for Container Registry - Default is "false"
#---------------------------------------------------------
data "azurerm_virtual_network" "vnet01" {
count = var.enable_private_endpoint ? 1 : 0
name = var.virtual_network_name
resource_group_name = local.resource_group_name
}
resource "azurerm_subnet" "snet-ep" {
count = var.enable_private_endpoint ? 1 : 0
name = "snet-private-endpoint-shared-${local.location}"
resource_group_name = local.resource_group_name
virtual_network_name = data.azurerm_virtual_network.vnet01.0.name
address_prefixes = var.private_subnet_address_prefix
enforce_private_link_endpoint_network_policies = true
}
resource "azurerm_private_endpoint" "pep1" {
count = var.enable_private_endpoint ? 1 : 0
name = format("%s-private-endpoint", var.container_registry_config.name)
location = local.location
resource_group_name = local.resource_group_name
subnet_id = azurerm_subnet.snet-ep.0.id
tags = merge({ "Name" = format("%s-private-endpoint", var.container_registry_config.name) }, var.tags, )
private_dns_zone_group {
name = "container-registry-group"
private_dns_zone_ids = [azurerm_private_dns_zone.dnszone1.0.id]
}
private_service_connection {
name = "containerregistryprivatelink"
is_manual_connection = false
private_connection_resource_id = azurerm_container_registry.main.id
subresource_names = ["registry"]
}
}
resource "azurerm_private_dns_zone" "dnszone1" {
count = var.existing_private_dns_zone == null && var.enable_private_endpoint ? 1 : 0
name = "privatelink.azurecr.io"
resource_group_name = local.resource_group_name
tags = merge({ "Name" = format("%s", "Azure-Container-Registry-Private-DNS-Zone") }, var.tags, )
}
resource "azurerm_private_dns_zone_virtual_network_link" "vent-link1" {
count = var.existing_private_dns_zone == null && var.enable_private_endpoint ? 1 : 0
name = "vnet-private-zone-link"
resource_group_name = local.resource_group_name
private_dns_zone_name = azurerm_private_dns_zone.dnszone1.0.name
virtual_network_id = data.azurerm_virtual_network.vnet01.0.id
registration_enabled = true
tags = merge({ "Name" = format("%s", "vnet-private-zone-link") }, var.tags, )
}
#---------------------------------------------------------------
# azurerm monitoring diagnostics for Container Registry
#---------------------------------------------------------------
resource "azurerm_monitor_diagnostic_setting" "acr-diag" {
count = var.log_analytics_workspace_name != null || var.storage_account_name != null ? 1 : 0
name = lower("acr-${var.container_registry_config.name}-diag")
target_resource_id = azurerm_container_registry.main.id
storage_account_id = var.storage_account_name != null ? data.azurerm_storage_account.storeacc.0.id : null
log_analytics_workspace_id = data.azurerm_log_analytics_workspace.logws.0.id
dynamic "log" {
for_each = var.acr_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
days = 0
}
}
}
metric {
category = "AllMetrics"
retention_policy {
enabled = false
days = 0
}
}
lifecycle {
ignore_changes = [log, metric]
}
}