This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
142 lines (114 loc) · 5.73 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
locals {
default_resource_group_name = "${var.product}-${var.env}${var.deployment_target}"
resource_group_name = "${var.resource_group_name != "" ? var.resource_group_name : local.default_resource_group_name}"
production_slot_app_settings = {
SLOT = "PRODUCTION"
WEBSITE_LOCAL_CACHE_OPTION = "${var.website_local_cache_sizeinmb == "0" ? "Never" : "Always"}"
WEBSITE_LOCAL_CACHE_SIZEINMB = "${var.website_local_cache_sizeinmb}"
}
asp_name = "${var.asp_name != "null" ? var.asp_name : local.default_resource_group_name}"
asp_rg = "${var.asp_rg != "null" ? var.asp_rg : local.default_resource_group_name}"
sp_name = "${var.env != "preview" ? local.asp_name : local.default_resource_group_name}"
sp_rg = "${var.env != "preview" ? local.asp_rg : local.default_resource_group_name}"
preview = "${var.env != "preview" ? 0 : 1}"
envcore = "${var.deployment_target != "" ? "env" : "core" }"
ase_enabled = "${var.enable_ase ? 1 : 0}"
delete_ase = "${var.enable_ase ? 0 : 1}"
}
resource "azurerm_resource_group" "rg" {
name = "${local.resource_group_name}"
location = "${var.location}"
tags = "${merge(var.common_tags,
map("lastUpdated", "${timestamp()}")
)}"
}
resource "azurerm_resource_group" "rg2" {
count = "${local.preview}"
name = "${var.asp_rg}"
location = "${var.location}"
tags = "${merge(var.common_tags,
map("lastUpdated", "${timestamp()}")
)}"
}
# The ARM template that creates a web app and app service plan
data "template_file" "sitetemplate" {
template = "${file("${path.module}/templates/asp-app.json")}"
}
# Create Application Insights for the service only if an instrumentation key to a specific instance wasn't provided
resource "azurerm_application_insights" "appinsights" {
count = "${var.appinsights_instrumentation_key == "" ? 1 : 0}"
name = "${var.product}-appinsights-${var.env}${var.deployment_target}"
location = "${var.appinsights_location}"
resource_group_name = "${local.resource_group_name}"
application_type = "${var.application_type}"
tags = "${merge(var.common_tags,
map("lastUpdated", "${timestamp()}")
)}"
}
locals {
# https://www.terraform.io/upgrade-guides/0-11.html#referencing-attributes-from-resources-with-count-0
service_app_insights_instrumentation_key = "${element(concat(azurerm_application_insights.appinsights.*.instrumentation_key, list("")), 0)}"
effective_app_insights_instrumentation_key = "${var.appinsights_instrumentation_key == "" ? local.service_app_insights_instrumentation_key : var.appinsights_instrumentation_key}"
app_settings_evaluated = {
APPLICATION_INSIGHTS_IKEY = "${local.effective_app_insights_instrumentation_key}"
# Support for nodejs apps (java apps to migrate to this env var in future PR)
APPINSIGHTS_INSTRUMENTATIONKEY = "${local.effective_app_insights_instrumentation_key}"
# Value used in the spring boot starter.
AZURE_APPLICATIONINSIGHTS_INSTRUMENTATIONKEY = "${local.effective_app_insights_instrumentation_key}"
}
}
# Create Application Service site
resource "azurerm_template_deployment" "app_service_site" {
count = "${local.ase_enabled}"
template_body = "${data.template_file.sitetemplate.rendered}"
name = "${var.product}-${var.env}${var.deployment_target}-webapp"
resource_group_name = "${local.resource_group_name}"
deployment_mode = "Incremental"
parameters = {
name = "${var.product}-${var.env}${var.deployment_target}"
location = "${var.location}"
env = "${var.env}${var.deployment_target}"
app_settings = "${jsonencode(merge(local.production_slot_app_settings, var.app_settings_defaults, local.app_settings_evaluated, var.app_settings))}"
staging_app_settings = "${jsonencode(merge(var.staging_slot_app_settings, var.app_settings_defaults, local.app_settings_evaluated, var.app_settings))}"
additional_host_name = "${var.additional_host_name}"
stagingSlotName = "${var.staging_slot_name}"
is_frontend = "${var.is_frontend}"
https_only = "${var.https_only}"
capacity = "${var.capacity}"
instance_size = "${var.instance_size}"
web_sockets_enabled = "${var.web_sockets_enabled}"
asp_name = "${local.asp_name}"
asp_rg = "${local.asp_rg}"
java_version = "${var.java_version}"
java_container_type = "${var.java_container_type}"
java_container_version = "${var.java_container_version}"
}
}
data "template_file" "ssltemplate" {
template = "${file("${path.module}/templates/app-ssl.json")}"
}
resource "azurerm_template_deployment" "app_service_ssl" {
count = "${var.certificate_name == "" ? 0 : 1 * local.ase_enabled}"
template_body = "${data.template_file.ssltemplate.rendered}"
name = "${var.product}-${var.env}${var.deployment_target}-cert"
resource_group_name = "${local.resource_group_name}"
deployment_mode = "Incremental"
parameters = {
name = "${var.product}-${var.env}${var.deployment_target}"
asp_name = "${local.asp_name}"
asp_rg = "${local.asp_rg}"
certificate_name = "${var.certificate_name}"
key_vault_id = "${var.certificate_key_vault_id}"
hostname = "${var.additional_host_name}"
}
depends_on = ["azurerm_template_deployment.app_service_site"]
}
resource "null_resource" "azcli_exec" {
count = "${local.delete_ase}"
triggers = {
force_run = "${timestamp()}"
}
provisioner "local-exec" {
command = "env AZURE_CONFIG_DIR=/opt/jenkins/.azure-${var.subscription} az webapp delete --name ${var.product}-${var.env} --resource-group ${local.resource_group_name} || true"
}
}