-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
88 lines (73 loc) · 2.45 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
provider "azurerm" {
version = "=2.10.0"
features {}
}
resource "random_string" "unique" {
length = 6
special = false
upper = false
}
resource "azurerm_resource_group" "rg" {
name = "rg-${random_string.unique.result}"
location = "westeurope"
}
data "azurerm_client_config" "current" {}
resource "azurerm_key_vault" "kv" {
name = "kv-${random_string.unique.result}"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
network_acls {
default_action = "Allow"
bypass = "AzureServices"
}
}
# Set permissions for currently logged-in Terraform SP to be able to manage secrets
resource "azurerm_key_vault_access_policy" "kv" {
key_vault_id = azurerm_key_vault.kv.id
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id
secret_permissions = [
"Get",
"Set",
"Delete"
]
}
resource "azurerm_key_vault_secret" "demo" {
name = "demo-secret"
value = "demo-value"
key_vault_id = azurerm_key_vault.kv.id
# Must wait for Terraform SP policy to kick in before creating secrets
depends_on = [azurerm_key_vault_access_policy.kv]
}
resource "azurerm_app_service_plan" "app" {
name = "plan-${random_string.unique.result}"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
sku {
tier = "Free"
size = "F1"
}
}
locals {
kv_format = "@Microsoft.KeyVault(SecretUri=${azurerm_key_vault.kv.vault_uri}secrets/%s/)"
}
resource "azurerm_app_service" "app" {
name = "app-${random_string.unique.result}"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
app_service_plan_id = azurerm_app_service_plan.app.id
identity {
type = "SystemAssigned"
}
app_settings = {
"demo" = format(local.kv_format, azurerm_key_vault_secret.demo.name)
}
}
resource "azurerm_key_vault_access_policy" "app" {
key_vault_id = azurerm_key_vault.kv.id
tenant_id = azurerm_app_service.app.identity.0.tenant_id
object_id = azurerm_app_service.app.identity.0.principal_id
secret_permissions = ["Get"]
}