-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.tf
More file actions
199 lines (196 loc) · 7.52 KB
/
Copy pathmain.tf
File metadata and controls
199 lines (196 loc) · 7.52 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
193
194
195
196
197
198
199
# Create resource group to house all resources with specified tags.
resource "azurerm_resource_group" "rg" {
name = var.rg
location = var.location
tags = {
"BusinessDescription" = var.business_description
"resCreatorAccount" = var.res_creator_account
"ReleaseEnv" = var.release_env
"ResourceCategory" = var.resource_category
"resAppFamily" = var.res_app_family
}
}
# generate a random string to be used as a suffix for the storage account name
resource "random_id" "name_suffix" {
byte_length = var.byte_length
}
# Create storage account to house the file share.
resource "azurerm_storage_account" "aznamingstgacc" {
name = "${lower(var.name_prefix)}${random_id.name_suffix.hex}"
resource_group_name = var.rg
location = var.location
account_tier = var.account_tier
account_replication_type = var.account_replication_type
blob_properties {
delete_retention_policy {
days = var.delete_retention_days
}
default_service_version = var.default_service_version
}
lifecycle {
ignore_changes = [
tags
]
}
depends_on = [
azurerm_resource_group.rg
]
}
# Create file share to house the aznamingtool files.
resource "azurerm_storage_share" "aznamingtool" {
name = var.aznamingtool_share_name
storage_account_name = azurerm_storage_account.aznamingstgacc.name
quota = var.quota
enabled_protocol = var.enabled_protocol
depends_on = [
azurerm_resource_group.rg, azurerm_storage_account.aznamingstgacc
]
}
# Create container registry to house the aznamingtool docker image.
resource "azurerm_container_registry" "acr" {
name = var.acr_name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
sku = var.acr_sku
admin_enabled = var.acr_admin_enabled
depends_on = [
azurerm_resource_group.rg
]
lifecycle {
ignore_changes = [
tags
]
}
}
# Create container registry task to build the aznamingtool docker image.
resource "azurerm_container_registry_task" "aznamingtool_task" {
name = var.acr_task_name
container_registry_id = azurerm_container_registry.acr.id
enabled = var.acr_task_enabled
depends_on = [
azurerm_container_registry.acr
]
platform {
os = var.container_platform_os
}
# The following is the docker build step
docker_step {
dockerfile_path = var.dockerfile_path
context_path = var.context_path
context_access_token = var.github_pat
image_names = [var.image_name]
}
}
# Create container registry task trigger to run the aznamingtool docker image build.
resource "azurerm_container_registry_task_schedule_run_now" "run_aznamingtool_build" {
container_registry_task_id = azurerm_container_registry_task.aznamingtool_task.id
depends_on = [
azurerm_container_registry_task.aznamingtool_task
]
}
# Output ASE ID for use with the app service plan resource
data "azurerm_app_service_environment_v3" "ase" {
name = var.ase_name
resource_group_name = var.ase_rg
}
output "ase_id" {
value = data.azurerm_app_service_environment_v3.ase.id
}
# App Service Plan for AzNamingTool web app
resource "azurerm_service_plan" "aznamingtool-asp" {
# Only create the app service plan if the "deployment_option" variable is set to "app_service"
count = var.deployment_option == "app_service" ? 1 : 0
name = var.asp_name
resource_group_name = var.rg
location = var.location
# Only include the app_service_environment_id attribute if the "use_ase" variable is true
app_service_environment_id = var.use_ase ? data.azurerm_app_service_environment_v3.ase.id : null
sku_name = var.sku_name
os_type = var.os_type
zone_balancing_enabled = var.zone_balancing_enabled
depends_on = [
azurerm_resource_group.rg
]
lifecycle {
ignore_changes = [
tags
]
}
}
# Web App for AzNamingTool
resource "azurerm_linux_web_app" "aznamingtool_web_app" {
# Only create the web app if the "deployment_option" variable is set to "app_service"
count = var.deployment_option == "app_service" ? 1 : 0
name = var.aznamingtool_webapp_name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
service_plan_id = azurerm_service_plan.aznamingtool-asp[count.index].id
storage_account {
name = azurerm_storage_account.aznamingstgacc.name
type = "AzureFiles"
account_name = azurerm_storage_account.aznamingstgacc.name
access_key = azurerm_storage_account.aznamingstgacc.primary_access_key
share_name = azurerm_storage_share.aznamingtool.name
mount_path = "/mnt/${var.aznamingtool_share_name}"
}
site_config {
always_on = true
app_command_line = ""
remote_debugging_enabled = false
}
app_settings = {
"WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "true"
"DOCKER_REGISTRY_SERVER_URL" = "https://${azurerm_container_registry.acr.login_server}"
"DOCKER_REGISTRY_SERVER_USERNAME" = azurerm_container_registry.acr.admin_username
"DOCKER_REGISTRY_SERVER_PASSWORD" = azurerm_container_registry.acr.admin_password
"WEBSITES_PORT" = var.website_port
"DOCKER_CUSTOM_IMAGE_NAME" = "DOCKER|${azurerm_container_registry.acr.login_server}/${var.aznamingtool_container_name}:latest"
# "SCM_TYPE" = "None" #source control used for deployment
# DOCKER_ENABLE_CI = "true" # enable continuous integration
}
depends_on = [
azurerm_container_registry_task.aznamingtool_task
]
}
# Create container group for AzNamingTool container app
resource "azurerm_container_group" "aznamingtool-container-app" {
# Only create the container group if the "deployment_option" variable is set to "container_instance"
count = var.deployment_option == "container_instance" ? 1 : 0
name = var.aznamingtool_container_name
location = var.location
resource_group_name = azurerm_resource_group.rg.name
os_type = var.container_os_type
dns_name_label = var.dns_name_label
# image registry credentials from the container registry
image_registry_credential {
server = azurerm_container_registry.acr.login_server
username = azurerm_container_registry.acr.admin_username
password = azurerm_container_registry.acr.admin_password
}
container {
name = var.aznamingtool_container_name
image = "${azurerm_container_registry.acr.login_server}/${var.aznamingtool_container_name}:latest"
cpu = var.container_cpu
memory = var.container_memory
ports {
port = var.container_port
protocol = var.container_protocol
}
# storage volume mount from the storage account and file share
volume {
name = var.aznamingtool_share_name
mount_path = "/mnt/${var.aznamingtool_share_name}"
storage_account_name = azurerm_storage_account.aznamingstgacc.name
storage_account_key = azurerm_storage_account.aznamingstgacc.primary_access_key
share_name = azurerm_storage_share.aznamingtool.name
}
}
depends_on = [
azurerm_container_registry_task.aznamingtool_task, azurerm_container_registry_task_schedule_run_now.run_aznamingtool_build
]
lifecycle {
ignore_changes = [
tags
]
}
}