-
Notifications
You must be signed in to change notification settings - Fork 10
/
container-app.tf
195 lines (160 loc) · 6.89 KB
/
container-app.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
resource "azurerm_container_app_environment" "container_app_env" {
name = "${local.resource_prefix}containerapp"
location = local.resource_group.location
resource_group_name = local.resource_group.name
log_analytics_workspace_id = azurerm_log_analytics_workspace.container_app.id
infrastructure_subnet_id = local.launch_in_vnet ? azurerm_subnet.container_apps_infra_subnet[0].id : null
internal_load_balancer_enabled = local.container_app_environment_internal_load_balancer_enabled
tags = local.tags
}
resource "azurerm_monitor_diagnostic_setting" "container_app_env" {
name = "${local.resource_prefix}-containerapp-diag"
target_resource_id = azurerm_container_app_environment.container_app_env.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.container_app.id
eventhub_name = local.enable_event_hub ? azurerm_eventhub.container_app[0].name : null
enabled_log {
category_group = "Audit"
}
# The below metrics are kept in to avoid a diff in the Terraform Plan output
metric {
category = "AllMetrics"
enabled = false
}
}
resource "azurerm_container_app_environment_storage" "container_app_env" {
count = local.enable_container_app_file_share ? 1 : 0
name = "h${local.resource_prefix_sha_short}-storage"
container_app_environment_id = azurerm_container_app_environment.container_app_env.id
account_name = azurerm_storage_account.container_app[0].name
share_name = azurerm_storage_share.container_app[0].name
access_key = azurerm_storage_account.container_app[0].primary_access_key
access_mode = "ReadWrite"
}
resource "azurerm_container_app" "container_apps" {
for_each = toset(concat(
["main"],
local.enable_worker_container ? ["worker"] : [],
))
name = each.value == "worker" ? "${local.container_app_name}-worker" : local.container_app_name
container_app_environment_id = azurerm_container_app_environment.container_app_env.id
resource_group_name = local.resource_group.name
revision_mode = "Single"
dynamic "ingress" {
for_each = each.value == "main" ? [1] : []
content {
external_enabled = true
target_port = local.container_port
traffic_weight {
percentage = 100
latest_revision = true
}
}
}
dynamic "secret" {
for_each = local.escrow_container_app_secrets_in_key_vault ? {} : local.container_app_secrets
content {
name = secret.value["name"]
value = secret.value["value"]
}
}
dynamic "secret" {
for_each = local.container_app_secrets_in_key_vault
content {
name = secret.value["name"]
key_vault_secret_id = secret.value["key_vault_secret_id"]
identity = azurerm_user_assigned_identity.containerapp[0].id
}
}
dynamic "identity" {
for_each = length(local.container_app_identity_ids) > 0 ? [1] : []
content {
type = "UserAssigned"
identity_ids = local.container_app_identity_ids
}
}
registry {
server = local.registry_server
username = local.registry_use_managed_identity == false ? local.registry_username : null
password_secret_name = local.registry_use_managed_identity == false ? "acr-password" : null
identity = local.registry_use_managed_identity ? azurerm_user_assigned_identity.containerapp[0].id : null
}
template {
dynamic "init_container" {
for_each = each.value == "main" && local.enable_init_container ? [1] : []
content {
name = "${each.value}-init"
image = local.init_container_image != "" ? local.init_container_image : "${local.registry_server}/${local.image_name}:${local.image_tag}"
cpu = local.container_cpu
memory = "${local.container_memory}Gi"
command = local.init_container_command
dynamic "env" {
for_each = local.container_app_env_vars
content {
name = env.value["name"]
secret_name = lookup(env.value, "secretRef", null)
value = lookup(env.value, "value", null)
}
}
}
}
container {
name = each.value
image = "${local.registry_server}/${local.image_name}:${local.image_tag}"
cpu = local.container_cpu
memory = "${local.container_memory}Gi"
command = each.value == "worker" ? local.worker_container_command : local.container_command
dynamic "volume_mounts" {
for_each = local.enable_container_app_file_share ? [1] : []
content {
name = azurerm_container_app_environment_storage.container_app_env[0].name
path = local.container_app_file_share_mount_path
}
}
dynamic "liveness_probe" {
for_each = each.value == "main" && local.enable_container_health_probe ? [1] : []
content {
interval_seconds = lookup(local.container_health_probe, "interval_seconds", null)
transport = lookup(local.container_health_probe, "transport", null)
port = lookup(local.container_health_probe, "port", local.container_port)
path = lookup(local.container_health_probe, "path", null)
}
}
dynamic "env" {
for_each = local.container_app_env_vars
content {
name = env.value["name"]
secret_name = lookup(env.value, "secretRef", null)
value = lookup(env.value, "value", null)
}
}
}
min_replicas = each.value == "worker" ? local.worker_container_min_replicas : local.container_min_replicas
max_replicas = each.value == "worker" ? local.worker_container_max_replicas : local.container_max_replicas
http_scale_rule {
name = "scale-up-down-http-requests"
concurrent_requests = local.container_scale_http_concurrency
}
dynamic "custom_scale_rule" {
for_each = local.container_scale_out_at_defined_time ? [1] : []
content {
name = "scale-down-out-of-hours"
custom_rule_type = "cron"
metadata = {
timezone = "Europe/London"
start = local.container_scale_out_rule_start
end = local.container_scale_out_rule_end
desiredReplicas = each.value == "worker" ? local.worker_container_max_replicas : local.container_max_replicas
}
}
}
dynamic "volume" {
for_each = local.enable_container_app_file_share ? [1] : []
content {
name = azurerm_container_app_environment_storage.container_app_env[0].name
storage_name = azurerm_container_app_environment_storage.container_app_env[0].name
storage_type = "AzureFile"
}
}
}
tags = local.tags
}