1+ # ---------------------------------
2+ # Local declarations
3+ # ---------------------------------
4+ locals {
5+ resource_group_name = element (coalescelist (data. azurerm_resource_group . rgrp . * . name , azurerm_resource_group. rg . * . name , [" " ]), 0 )
6+ location = element (coalescelist (data. azurerm_resource_group . rgrp . * . location , azurerm_resource_group. rg . * . location , [" " ]), 0 )
7+
8+ # Default configuration for Site config block
9+ default_site_config = {
10+ always_on = " true"
11+ }
12+
13+ # Enabling the App Insights on app service - default configuration for agent
14+ app_insights = try (data. azurerm_application_insights . main . 0 , try (azurerm_application_insights. main . 0 , {}))
15+
16+ default_app_settings = var. application_insights_enabled ? {
17+ APPLICATION_INSIGHTS_IKEY = try (local. app_insights . instrumentation_key , " " )
18+ APPINSIGHTS_INSTRUMENTATIONKEY = try (local. app_insights . instrumentation_key , " " )
19+ APPLICATIONINSIGHTS_CONNECTION_STRING = try (local. app_insights . connection_string , " " )
20+ ApplicationInsightsAgent_EXTENSION_VERSION = " ~2"
21+ } : {}
22+
23+ # App service IP Address, Subnet_ids and Service_Tag restrictions
24+ ip_address = [for ip_address in var . ips_allowed : {
25+ name = " ip_restriction_cidr_${ join (" " , [1 , index (var. ips_allowed , ip_address)])} "
26+ ip_address = ip_address
27+ virtual_network_subnet_id = null
28+ service_tag = null
29+ subnet_id = null
30+ priority = join (" " , [1 , index (var. ips_allowed , ip_address)])
31+ action = " Allow"
32+ }]
33+
34+ subnets = [for subnet in var . subnet_ids_allowed : {
35+ name = " ip_restriction_subnet_${ join (" " , [1 , index (var. subnet_ids_allowed , subnet)])} "
36+ ip_address = null
37+ virtual_network_subnet_id = subnet
38+ service_tag = null
39+ subnet_id = subnet
40+ priority = join (" " , [1 , index (var. subnet_ids_allowed , subnet)])
41+ action = " Allow"
42+ }]
43+
44+ service_tags = [for service_tag in var . service_tags_allowed : {
45+ name = " service_tag_restriction_${ join (" " , [1 , index (var. service_tags_allowed , service_tag)])} "
46+ ip_address = null
47+ virtual_network_subnet_id = null
48+ service_tag = service_tag
49+ subnet_id = null
50+ priority = join (" " , [1 , index (var. service_tags_allowed , service_tag)])
51+ action = " Allow"
52+ }]
53+
54+ # App service SCM IP Address, SCM Subnet_ids andSCM Service_Tag restrictions
55+ scm_ip_address = [for ip_address in var . scm_ips_allowed : {
56+ name = " scm_ip_restriction_cidr_${ join (" " , [1 , index (var. scm_ips_allowed , ip_address)])} "
57+ ip_address = ip_address
58+ virtual_network_subnet_id = null
59+ service_tag = null
60+ subnet_id = null
61+ priority = join (" " , [1 , index (var. scm_ips_allowed , ip_address)])
62+ action = " Allow"
63+ }]
64+
65+ scm_subnets = [for subnet in var . scm_subnet_ids_allowed : {
66+ name = " scm_ip_restriction_subnet_${ join (" " , [1 , index (var. scm_subnet_ids_allowed , subnet)])} "
67+ ip_address = null
68+ virtual_network_subnet_id = subnet
69+ service_tag = null
70+ subnet_id = subnet
71+ priority = join (" " , [1 , index (var. scm_subnet_ids_allowed , subnet)])
72+ action = " Allow"
73+ }]
74+
75+ scm_service_tags = [for service_tag in var . scm_service_tags_allowed : {
76+ name = " scm_service_tag_restriction_${ join (" " , [1 , index (var. scm_service_tags_allowed , service_tag)])} "
77+ ip_address = null
78+ virtual_network_subnet_id = null
79+ service_tag = service_tag
80+ subnet_id = null
81+ priority = join (" " , [1 , index (var. scm_service_tags_allowed , service_tag)])
82+ action = " Allow"
83+ }]
84+
85+ }
86+
187# ---------------------------------------------------------
288# Resource Group Creation or selection - Default is "true"
389# ----------------------------------------------------------
@@ -15,6 +101,58 @@ resource "azurerm_resource_group" "rg" {
15101 tags = merge ({ " ResourceName" = format (" %s" , var. resource_group_name ) }, var. tags , )
16102}
17103
104+ # ---------------------------------------------------------
105+ # Generating Storage SAS URL - Default is "false"
106+ # ----------------------------------------------------------
107+ data "azurerm_storage_account" "storeacc" {
108+ count = var. enable_backup ? 1 : 0
109+ name = var. storage_account_name
110+ resource_group_name = local. resource_group_name
111+ }
112+
113+ resource "azurerm_storage_container" "storcont" {
114+ count = var. enable_backup ? 1 : 0
115+ name = var. storage_container_name == null ? " appservice-backup" : var. storage_container_name
116+ storage_account_name = data. azurerm_storage_account . storeacc . 0 . name
117+ container_access_type = " private"
118+ }
119+
120+ resource "time_rotating" "main" {
121+ count = var. enable_backup ? 1 : 0
122+ rotation_rfc3339 = var. password_end_date
123+ rotation_years = var. password_rotation_in_years
124+
125+ triggers = {
126+ end_date = var.password_end_date
127+ years = var.password_rotation_in_years
128+ }
129+ }
130+
131+ data "azurerm_storage_account_blob_container_sas" "main" {
132+ count = var. enable_backup ? 1 : 0
133+ connection_string = data. azurerm_storage_account . storeacc . 0 . primary_connection_string
134+ container_name = azurerm_storage_container. storcont . 0 . name
135+ https_only = true
136+
137+ start = timestamp ()
138+ expiry = time_rotating. main . 0 . rotation_rfc3339
139+
140+ permissions {
141+ read = true
142+ add = true
143+ create = true
144+ write = true
145+ delete = true
146+ list = true
147+ }
148+
149+ cache_control = " max-age=5"
150+ content_disposition = " inline"
151+ content_encoding = " deflate"
152+ content_language = " en-US"
153+ content_type = " application/json"
154+ }
155+
18156# ---------------------------------------------------------
19157# App Service Plan definition - Default is "true"
20158# ----------------------------------------------------------
@@ -174,3 +312,23 @@ resource "azurerm_app_service_custom_hostname_binding" "cust-host-bind" {
174312 thumbprint = lookup (azurerm_app_service_certificate. main , each. key , false ) != false ? azurerm_app_service_certificate. main [each . key ]. thumbprint : null
175313}
176314
315+
316+ # ---------------------------------------------------------
317+ # Application Insights resoruces - Default is "false"
318+ # ----------------------------------------------------------
319+ data "azurerm_application_insights" "main" {
320+ count = var. application_insights_enabled && var. application_insights_id != null ? 1 : 0
321+ name = split (" /" , var. application_insights_id )[8 ]
322+ resource_group_name = split (" /" , var. application_insights_id )[4 ]
323+ }
324+
325+ resource "azurerm_application_insights" "main" {
326+ count = var. application_insights_enabled && var. application_insights_id == null ? 1 : 0
327+ name = lower (format (" appi-%s" , var. app_insights_name ))
328+ location = local. location
329+ resource_group_name = local. resource_group_name
330+ application_type = var. application_insights_type
331+ retention_in_days = var. retention_in_days
332+ disable_ip_masking = var. disable_ip_masking
333+ tags = merge ({ " ResourceName" = " ${ var . app_insights_name } " }, var. tags , )
334+ }
0 commit comments