This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
113 lines (101 loc) · 4.3 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
#---------------------------------------------------------
# Local declarations
#----------------------------------------------------------
locals {
account_tier = (var.account_kind == "FileStorage" ? "Premium" : split("_", var.sku)[0])
account_replication_type = (local.account_tier == "Premium" ? "LRS" : split("_", var.sku)[1])
resource_group_name = element(
coalescelist(azurerm_resource_group.rg.*.name, [var.resource_group_name]), 0)
location = element(
coalescelist(azurerm_resource_group.rg.*.location, [var.location]), 0)
if_static_website_enabled = var.enable_static_website ? [{}] : []
}
resource "azurerm_resource_group" "rg" {
count = var.create_resource_group ? 1 : 0
name = var.resource_group_name
location = var.location
tags = merge({ "Name" = format("%s", var.resource_group_name) }, var.tags, )
}
#---------------------------------------------------------
# Storage Account Creation and enable static website
#----------------------------------------------------------
resource "azurerm_storage_account" "storeacc" {
name = var.storage_account_name
resource_group_name = local.resource_group_name
location = local.location
account_kind = var.account_kind
account_tier = local.account_tier
account_replication_type = local.account_replication_type
enable_https_traffic_only = var.enable_https_traffic
tags = merge({ "Name" = format("%s", var.storage_account_name) }, var.tags, )
dynamic "static_website" {
for_each = local.if_static_website_enabled
content {
index_document = var.index_path
error_404_document = var.custom_404_path
}
}
blob_properties {
cors_rule {
allowed_methods = var.allowed_methods
allowed_origins = var.allowed_origins
allowed_headers = var.allowed_headers
exposed_headers = var.exposed_headers
max_age_in_seconds = var.max_age_in_seconds
}
}
identity {
type = var.assign_identity ? "SystemAssigned" : null
}
}
# Following resource is not removed when we update the terraform plan with `false` after initial run. Need to check for the option to remove `$web` folder if we disable static website and update the plan.
resource "null_resource" "copyfilesweb" {
count = var.enable_static_website ? 1 : 0
provisioner "local-exec" {
command = "az storage blob upload-batch --no-progress --account-name ${azurerm_storage_account.storeacc.name} -s ${var.static_website_source_folder} -d '$web' --output none"
}
}
#---------------------------------------------------------
# Add CDN profile and endpoint to static website
#----------------------------------------------------------
resource "azurerm_cdn_profile" "cdn-profile" {
count = var.enable_static_website && var.enable_cdn_profile ? 1 : 0
name = var.cdn_profile_name
resource_group_name = local.resource_group_name
location = local.location
sku = var.cdn_sku_profile
tags = merge({ "Name" = format("%s", var.cdn_profile_name) }, var.tags, )
}
resource "random_string" "unique" {
count = var.enable_static_website && var.enable_cdn_profile ? 1 : 0
length = 8
special = false
upper = false
}
resource "azurerm_cdn_endpoint" "cdn-endpoint" {
count = var.enable_static_website && var.enable_cdn_profile ? 1 : 0
name = random_string.unique.0.result
profile_name = azurerm_cdn_profile.cdn-profile.0.name
location = local.location
resource_group_name = local.resource_group_name
origin_host_header = azurerm_storage_account.storeacc.primary_web_host
querystring_caching_behaviour = "IgnoreQueryString"
origin {
name = "websiteorginaccount"
host_name = azurerm_storage_account.storeacc.primary_web_host
}
}
resource "null_resource" "add_custom_domain" {
count = var.custom_domain_name != null ? 1 : 0
depends_on = [
azurerm_cdn_endpoint.cdn-endpoint
]
provisioner "local-exec" {
command = "pwsh ${path.cwd}/Setup-AzCdnCustomDomain.ps1"
environment = {
CUSTOM_DOMAIN = var.custom_domain_name
RG_NAME = var.resource_group_name
FRIENDLY_NAME = var.friendly_name
}
}
}