-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
266 lines (208 loc) · 6.71 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
locals {
prefix = "${random_pet.rg.id}-${var.environment}"
prefixSafe = "${random_pet.rg.id}${var.environment}"
image_name = "containerapps-helloworld:latest"
redis_sample_app_image = "sample-service-redis:latest"
}
data "azurerm_client_config" "current" {}
resource "random_id" "random" {
byte_length = 4
}
resource "random_pet" "rg" {
length = 1
}
resource "azurerm_resource_group" "rg" {
name = local.prefix
location = var.location
}
resource "azurerm_user_assigned_identity" "ca_identity" {
location = var.location
name = "ca_identity"
resource_group_name = azurerm_resource_group.rg.name
}
resource "azurerm_role_assignment" "acrpull_mi" {
scope = module.container_registry.id
role_definition_name = "AcrPull"
principal_id = azurerm_user_assigned_identity.ca_identity.principal_id
}
resource "azurerm_log_analytics_workspace" "log_analytics" {
name = "${local.prefix}-la"
location = var.location
resource_group_name = azurerm_resource_group.rg.name
sku = var.la_sku
retention_in_days = var.la_retenction_days
}
module "virtual_network" {
source = "./modules/virtual_network"
resource_group_name = azurerm_resource_group.rg.name
location = var.location
name = "${local.prefix}-vnet"
address_space = var.address_space
subnet_address_prefix_map = var.subnet_address_prefix_map
prefix = local.prefix
}
module "container_registry" {
source = "./modules/container_registry"
resource_group_name = azurerm_resource_group.rg.name
location = var.location
name = "${local.prefixSafe}acr"
}
resource "azurerm_container_app_environment" "app_env" {
name = "${local.prefix}-environment"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
log_analytics_workspace_id = azurerm_log_analytics_workspace.log_analytics.id
infrastructure_subnet_id = module.virtual_network.app_subnet_id
infrastructure_resource_group_name = "${azurerm_resource_group.rg.name}-infra"
workload_profile {
name = "test-1"
workload_profile_type = "D4"
maximum_count = 1
minimum_count = 1
}
workload_profile {
name = "Consumption"
workload_profile_type = "Consumption"
maximum_count = 0
minimum_count = 0
}
}
resource "time_sleep" "wait_60_seconds" {
depends_on = [module.container_registry]
create_duration = "60s"
}
resource "null_resource" "acr_import" {
provisioner "local-exec" {
command = <<-EOT
az acr import \
--name ${module.container_registry.name} \
--source mcr.microsoft.com/azuredocs/${local.image_name} \
--image ${local.image_name}
EOT
}
depends_on = [ time_sleep.wait_60_seconds ]
}
resource "null_resource" "redis_acr_import" {
provisioner "local-exec" {
command = <<-EOT
az acr import \
--name ${module.container_registry.name} \
--source mcr.microsoft.com/k8se/samples/${local.redis_sample_app_image} \
--image ${local.redis_sample_app_image}
EOT
}
depends_on = [ time_sleep.wait_60_seconds ]
}
resource "azurerm_container_app" "sampleapi" {
name = "${local.prefix}-app"
container_app_environment_id = azurerm_container_app_environment.app_env.id
resource_group_name = azurerm_resource_group.rg.name
revision_mode = "Single"
identity {
type = "SystemAssigned, UserAssigned"
identity_ids = [azurerm_user_assigned_identity.ca_identity.id]
}
registry {
identity = azurerm_user_assigned_identity.ca_identity.id
server = module.container_registry.url
}
template {
container {
name = "sampleapi"
image = "${module.container_registry.url}/${local.image_name}"
cpu = 0.25
memory = "0.5Gi"
}
min_replicas = 0
max_replicas = 5
}
ingress {
allow_insecure_connections = false
external_enabled = true
target_port = 80
traffic_weight {
percentage = 100
latest_revision = true
}
}
depends_on = [null_resource.acr_import]
}
resource "azurerm_container_app" "sampleapi_dedicated" {
name = "${local.prefix}-dedicated-app"
container_app_environment_id = azurerm_container_app_environment.app_env.id
resource_group_name = azurerm_resource_group.rg.name
revision_mode = "Single"
workload_profile_name = "test-1"
identity {
type = "SystemAssigned, UserAssigned"
identity_ids = [azurerm_user_assigned_identity.ca_identity.id]
}
registry {
identity = azurerm_user_assigned_identity.ca_identity.id
server = module.container_registry.url
}
template {
container {
name = "sampleapi"
image = "${module.container_registry.url}/${local.image_name}"
cpu = 0.25
memory = "0.5Gi"
}
min_replicas = 0
max_replicas = 5
}
ingress {
allow_insecure_connections = false
external_enabled = true
target_port = 80
traffic_weight {
percentage = 100
latest_revision = true
}
}
depends_on = [null_resource.acr_import]
}
resource "azurerm_container_app" "sampleredis_dedicated" {
name = "${local.prefix}-redis-app"
container_app_environment_id = azurerm_container_app_environment.app_env.id
resource_group_name = azurerm_resource_group.rg.name
revision_mode = "Single"
workload_profile_name = "test-1"
identity {
type = "SystemAssigned, UserAssigned"
identity_ids = [azurerm_user_assigned_identity.ca_identity.id]
}
registry {
identity = azurerm_user_assigned_identity.ca_identity.id
server = module.container_registry.url
}
template {
container {
name = "sampleredisapp"
image = "${module.container_registry.url}/${local.redis_sample_app_image}"
cpu = 0.25
memory = "0.5Gi"
}
min_replicas = 0
max_replicas = 5
}
ingress {
allow_insecure_connections = false
external_enabled = true
target_port = 8080
traffic_weight {
percentage = 100
latest_revision = true
}
}
depends_on = [null_resource.redis_acr_import]
}
output "url_sampleapi" {
value = "https://${azurerm_container_app.sampleapi.ingress[0].fqdn}"
}
output "url_sampleapi_dedicated" {
value = "https://${azurerm_container_app.sampleapi_dedicated.ingress[0].fqdn}"
}
output "url_sampleapi_redis_dedicated" {
value = "https://${azurerm_container_app.sampleredis_dedicated.ingress[0].fqdn}"
}