-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.tf
More file actions
192 lines (166 loc) · 5.43 KB
/
main.tf
File metadata and controls
192 lines (166 loc) · 5.43 KB
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
module "container_app_identity" {
source = "../managed-identity"
resource_group_name = var.resource_group_name
location = var.location
uai_name = "mi-${var.name}"
}
# Allow the container app to read secrets from keyvaults in the resource groups
module "key_vault_reader_role_app" {
count = var.fetch_secrets_from_app_key_vault ? 1 : 0
source = "../rbac-assignment"
scope = var.app_key_vault_id
role_definition_name = "Key Vault Secrets User"
principal_id = module.container_app_identity.principal_id
}
module "key_vault_reader_role_infra" {
count = var.enable_auth ? 1 : 0
source = "../rbac-assignment"
scope = data.azurerm_key_vault.infra[0].id
role_definition_name = "Key Vault Secrets User"
principal_id = module.container_app_identity.principal_id
}
# Merge the identity created above with any passed in via a variables list:
locals {
all_identity_ids = compact(concat(
[var.acr_managed_identity_id],
[module.container_app_identity.id]
))
}
resource "azurerm_container_app" "main" {
name = var.name
container_app_environment_id = var.container_app_environment_id
resource_group_name = var.resource_group_name
revision_mode = "Single"
workload_profile_name = var.workload_profile_name
identity {
type = "UserAssigned"
identity_ids = local.all_identity_ids
}
dynamic "secret" {
for_each = concat(var.fetch_secrets_from_app_key_vault ? data.azurerm_key_vault_secrets.app[0].secrets : [],
var.enable_auth ? [for s in data.azurerm_key_vault_secret.infra : { name = s.name, id = s.id }] : [])
content {
# KV secrets are uppercase and hyphen separated
# app container secrets are lowercase and hyphen separated
name = lower(secret.value.name)
identity = module.container_app_identity.id
key_vault_secret_id = secret.value.id
}
}
dynamic "secret" {
for_each = var.secret_variables
content {
name = replace(lower(secret.key), "_", "-")
value = secret.value
}
}
template {
container {
name = var.name
image = var.docker_image
cpu = local.cpu
memory = local.memory
dynamic "env" {
for_each = var.environment_variables
content {
name = env.key
value = env.value
}
}
dynamic "env" {
for_each = var.secret_variables
content {
# Env vars are uppercase and underscore separated
name = upper(replace(env.key, "-", "_"))
secret_name = replace(lower(env.key), "_", "-")
}
}
dynamic "env" {
for_each = var.fetch_secrets_from_app_key_vault ? data.azurerm_key_vault_secrets.app[0].secrets : []
content {
# Env vars are uppercase and underscore separated
name = upper(replace(env.value.name, "-", "_"))
# KV secrets are uppercase and hyphen separated
# app container secrets are lowercase and hyphen separated
secret_name = lower(env.value.name)
}
}
}
min_replicas = var.min_replicas
}
dynamic "registry" {
for_each = var.acr_login_server != null ? [1] : []
content {
server = var.acr_login_server
identity = var.acr_managed_identity_id
}
}
dynamic "ingress" {
for_each = var.is_web_app ? [1] : []
content {
external_enabled = true
target_port = var.port
allow_insecure_connections = false
traffic_weight {
percentage = 100
latest_revision = true
}
}
}
dynamic "ingress" {
for_each = var.is_tcp_app ? [1] : []
content {
external_enabled = true
target_port = var.port
exposed_port = var.port
allow_insecure_connections = false
transport = "tcp"
traffic_weight {
percentage = 100
latest_revision = true
}
}
}
}
# Enable Microsoft Entra ID authentication if specified
# Reqquires infra key vault to contain secrets:
## - aad-client-id
## - aad-client-secret
## - aad-client-audiences
resource "azapi_resource" "auth" {
count = var.enable_auth ? 1 : 0
type = "Microsoft.App/containerApps/authConfigs@2025-01-01"
name = "current"
parent_id = azurerm_container_app.main.id
body = {
properties = {
platform = {
enabled = true
}
globalValidation = {
unauthenticatedClientAction = var.unauthenticated_action
}
identityProviders = {
azureActiveDirectory = {
enabled = true
registration = {
clientId = data.azurerm_key_vault_secret.infra["aad-client-id"].value
clientSecretSettingName = "aad-client-secret"
openIdIssuer = "https://sts.windows.net/${data.azurerm_client_config.current.tenant_id}/v2.0"
}
validation = {
# Values within the Key Vault secret are separated by commas
allowedAudiences = split(",", data.azurerm_key_vault_secret.infra["aad-client-audiences"].value)
}
}
}
httpSettings = {
forwardProxy = {
convention = "Custom"
customHostHeaderName = "X-forwarded-Host"
}
}
}
}
depends_on = [data.azurerm_key_vault_secret.infra]
}