-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
188 lines (159 loc) · 5 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
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = ">= 3.49.0"
}
}
}
provider "azurerm" {
# Configuration options
features {
key_vault {
purge_soft_deleted_secrets_on_destroy = true
recover_soft_deleted_secrets = true
}
}
}
resource "azurerm_resource_group" "base" {
name = "rg-${var.environment}-${var.product}-tf"
location = var.location
tags = merge(local.main_tags, var.user_tags)
lifecycle {
ignore_changes = [
tags,
location
]
}
}
resource "azurerm_storage_account" "stg_base" {
name = "stgtf${var.environment}${var.product}"
resource_group_name = azurerm_resource_group.base.name
location = azurerm_resource_group.base.location
account_kind = "StorageV2"
account_tier = "Standard"
account_replication_type = "LRS"
is_hns_enabled = false
tags = merge(local.main_tags, var.user_tags)
lifecycle {
ignore_changes = [
tags,
location
]
}
}
resource "azurerm_storage_container" "terraform" {
name = "terraform"
storage_account_name = azurerm_storage_account.stg_base.name
depends_on = [
azurerm_storage_account.stg_base
]
}
resource "azurerm_container_registry" "acr_devops" {
name = "acr${var.product}${var.environment}"
resource_group_name = azurerm_resource_group.base.name
location = azurerm_resource_group.base.location
sku = "Basic"
admin_enabled = true
tags = merge(local.main_tags, var.user_tags)
lifecycle {
ignore_changes = [
tags,
location
]
}
}
resource "azurerm_key_vault" "base_tf_keyvault" {
name = "kv-${var.environment}-${var.product}-tf"
location = azurerm_resource_group.base.location
resource_group_name = azurerm_resource_group.base.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
enable_rbac_authorization = true
purge_protection_enabled = true
soft_delete_retention_days = 7
depends_on = [
azurerm_resource_group.base,
data.azuread_service_principal.sp_devops_automation
]
tags = merge(local.main_tags, var.user_tags)
lifecycle {
ignore_changes = [
tags,
location
]
}
}
resource "azurerm_role_assignment" "keyvault_role_assignment" {
scope = azurerm_key_vault.base_tf_keyvault.id
for_each = { for entry in local.keyvault_role_service_principal_assignment : "${entry.role}.${entry.principal}" => entry }
role_definition_name = each.value.role
principal_id = each.value.principal
depends_on = [
azurerm_key_vault.base_tf_keyvault
]
}
resource "azurerm_key_vault_secret" "tf_stg_accesskey" {
name = "stg-terraform-access-key"
value = azurerm_storage_account.stg_base.primary_access_key
key_vault_id = azurerm_key_vault.base_tf_keyvault.id
depends_on = [
azurerm_role_assignment.keyvault_role_assignment
]
}
resource "azurerm_key_vault_secret" "tf_clientId" {
name = "tf-client-id"
value = var.sp_client_id
key_vault_id = azurerm_key_vault.base_tf_keyvault.id
depends_on = [
azurerm_role_assignment.keyvault_role_assignment
]
}
resource "azurerm_key_vault_secret" "tf_client_secret" {
name = "tf-client-secret"
value = var.sp_client_secret
key_vault_id = azurerm_key_vault.base_tf_keyvault.id
depends_on = [
azurerm_role_assignment.keyvault_role_assignment
]
}
resource "azurerm_key_vault_secret" "tf_subscription_id" {
name = "tf-subscription-id"
value = data.azurerm_client_config.current.subscription_id
key_vault_id = azurerm_key_vault.base_tf_keyvault.id
depends_on = [
azurerm_role_assignment.keyvault_role_assignment
]
}
resource "azurerm_key_vault_secret" "tf_tenant_id" {
name = "tf-tenant-id"
value = data.azurerm_client_config.current.tenant_id
key_vault_id = azurerm_key_vault.base_tf_keyvault.id
depends_on = [
azurerm_role_assignment.keyvault_role_assignment
]
}
resource "azurerm_key_vault_secret" "acr_base_login" {
name = "acr-base-login"
value = azurerm_container_registry.acr_devops.admin_username
key_vault_id = azurerm_key_vault.base_tf_keyvault.id
depends_on = [
azurerm_role_assignment.keyvault_role_assignment
]
}
resource "azurerm_key_vault_secret" "acr_base_password" {
name = "acr-base-password"
value = azurerm_container_registry.acr_devops.admin_password
key_vault_id = azurerm_key_vault.base_tf_keyvault.id
depends_on = [
azurerm_role_assignment.keyvault_role_assignment
]
}
resource "azurerm_key_vault_secret" "acr_base_url" {
name = "acr-base-url"
value = azurerm_container_registry.acr_devops.login_server
key_vault_id = azurerm_key_vault.base_tf_keyvault.id
depends_on = [
azurerm_role_assignment.keyvault_role_assignment
]
}