forked from microsoft/python-sample-vscode-flask-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_service.tf
More file actions
186 lines (159 loc) · 5.88 KB
/
app_service.tf
File metadata and controls
186 lines (159 loc) · 5.88 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
variable "web_app_name" {
type = string
description = "name of the Azure web app"
default = "testapp"
}
variable "docker_registry_name" {
type = string
description = "docker registry name in Azure, must be globally unique..."
default = ""
}
variable "docker_image_name" {
type = string
description = "name of the docker image to run (including version) as webapp (name:version)"
}
variable "resource_group_location" {
type = string
default = "eastus"
}
variable "subscription" {
type = string
}
variable "application_port" {
type = number
default = 80
}
# this bit is to help generate a globally unique ACR for each subscription webapp pair
resource "random_integer" "acr_id" {
min = 100000
max = 999999
keepers = {
subscription = "${var.subscription}"
web_app_name = "${var.web_app_name}"
}
}
# the final ACR name is either the one explicitly passed in or built up from the webapp name and a rand id
locals {
unsafe_registry_name = var.docker_registry_name != "" ? var.docker_registry_name : join(
"", ["${var.web_app_name}", "githubacr", "${random_integer.acr_id.result}"]
)
registry_name = replace(local.unsafe_registry_name, "-", "")
}
# Configure the Azure provider
provider "azurerm" {
version = "~>2.0"
subscription_id = var.subscription
features {}
}
# to save terraform state to azure, configure this bit appropriately
# https://docs.microsoft.com/en-us/azure/developer/terraform/store-state-in-azure-storage
#terraform {
# backend "azurerm" {
# resource_group_name = "tfstate"
# storage_account_name = "tfstate22894"
# container_name = "tfstate"
# key = "terraform.tfstate"
# }
#}
# Create the resources we need:
# - Resource group as a namespace for the app resources
# - ACR for the docker images
# - App service plan to back our webapp
# - App service to define our webapp
resource "azurerm_resource_group" "main" {
name = "${var.web_app_name}-resources"
location = var.resource_group_location
}
resource "azurerm_container_registry" "acr" {
name = local.registry_name
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
sku = "Basic"
admin_enabled = true
}
resource "azurerm_app_service_plan" "main" {
name = "${var.web_app_name}-appserviceplan"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
kind = "Linux"
reserved = true
sku {
tier = "Standard"
size = "S1"
}
}
resource "azurerm_app_service" "main" {
name = "${var.web_app_name}-appservice"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
app_service_plan_id = azurerm_app_service_plan.main.id
# TODO: get this Managed Identity to have proper creds to the created ACR so we don't have to pass
# admin creds through the app setting
identity {
type = "SystemAssigned"
}
site_config {
app_command_line = ""
linux_fx_version = "DOCKER|${azurerm_container_registry.acr.name}/${var.docker_image_name}"
always_on = true
}
# TODO: remove the admin username / password once we get managed identity assigned correct RBAC
app_settings = {
"WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false"
"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.application_port
}
# This will be changed by GitHub action as it pushes new versions, terraform should ignore
lifecycle {
ignore_changes = [
site_config
]
}
}
data "azurerm_subscription" "current" {
}
# These next resources create scripts in the repo configured to create Service Principals for GitHub actions to push
# docker images to ACR and configure / restart the webapp on deploy
resource "local_file" "gh_actions_sp" {
content = templatefile("${path.module}/.script_templates/gh_actions_sp.tpl", {
subscription = data.azurerm_subscription.current.subscription_id,
resource_group = azurerm_resource_group.main.name,
app_service_name = azurerm_app_service.main.name
})
filename = "${path.module}/.generated_scripts/${terraform.workspace}/generate_service_principal_for_gh_actions.sh"
file_permission = "0700"
}
resource "local_file" "gh_actions_acr_sp" {
content = templatefile("${path.module}/.script_templates/gh_actions_acr_sp.tpl", {
acr_name = azurerm_container_registry.acr.name
})
filename = "${path.module}/.generated_scripts/${terraform.workspace}/generate_service_principal_for_gh_actions_acr_push.sh"
file_permission = "0700"
}
resource "local_file" "gh_actions_workflow" {
content = templatefile("${path.module}/.script_templates/gh_actions_workflow.tpl", {
web_app_name = var.web_app_name,
app_service_name = azurerm_app_service.main.name,
docker_image_name = var.docker_image_name,
ds = "$",
ob = "{{",
cb = "}}"
})
filename = "${path.module}/.generated_scripts/${terraform.workspace}/.github/workflows/docker_webapp_deploy_to_azure.yml"
file_permission = "0700"
}
# Output data for visibility
output "subscription" {
value = data.azurerm_subscription.current.subscription_id
}
output "acr" {
value = azurerm_container_registry.acr.login_server
}
output "resource_group" {
value = azurerm_resource_group.main.name
}
output "app_name" {
value = var.web_app_name
}