-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
142 lines (124 loc) · 4.3 KB
/
Copy pathmain.tf
File metadata and controls
142 lines (124 loc) · 4.3 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
# Local Variables
locals {
resource_group_name = "${var.prefix}-rg"
cosmosdb_account_name = "${var.prefix}-mongodb-${var.suffix}"
app_service_plan_name = "${var.prefix}-app-service-plan-${var.suffix}"
web_app_name = "${var.prefix}-webapp-${var.suffix}"
}
# Create a resource group
resource "azurerm_resource_group" "example" {
name = local.resource_group_name
location = var.location
tags = var.tags
}
# Create a cosmosdb account
resource "azurerm_cosmosdb_account" "example" {
name = local.cosmosdb_account_name
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
offer_type = "Standard"
kind = "MongoDB"
mongo_server_version = var.mongodb_server_version
automatic_failover_enabled = false
tags = var.tags
consistency_policy {
consistency_level = var.consistency_level
max_interval_in_seconds = 300
max_staleness_prefix = 100000
}
geo_location {
location = var.primary_region
failover_priority = 0
}
geo_location {
location = var.secondary_region
failover_priority = 1
}
lifecycle {
ignore_changes = [
tags
]
}
}
resource "azurerm_cosmosdb_mongo_database" "example" {
name = var.cosmosdb_database_name
resource_group_name = azurerm_resource_group.example.name
account_name = azurerm_cosmosdb_account.example.name
throughput = 400
}
resource "azurerm_cosmosdb_mongo_collection" "example" {
name = var.cosmosdb_collection_name
resource_group_name = azurerm_resource_group.example.name
account_name = azurerm_cosmosdb_account.example.name
database_name = azurerm_cosmosdb_mongo_database.example.name
default_ttl_seconds = "777"
shard_key = "username"
throughput = 400
# Dynamically create the 'index' blocks using a for_each loop over the variable
dynamic "index" {
# The for_each expression iterates over the list of keys from the variable
for_each = var.mongodb_index_keys
content {
# The value of the current item in the iteration (e.g., "$**", "_id", etc.)
keys = [index.value]
}
}
}
# Create a service plan
resource "azurerm_service_plan" "example" {
name = local.app_service_plan_name
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
sku_name = var.sku_name
os_type = var.os_type
zone_balancing_enabled = var.zone_balancing_enabled
tags = var.tags
lifecycle {
ignore_changes = [
tags
]
}
}
# Create a web app
resource "azurerm_linux_web_app" "example" {
name = local.web_app_name
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
service_plan_id = azurerm_service_plan.example.id
https_only = var.https_only
public_network_access_enabled = var.public_network_access_enabled
client_affinity_enabled = false
tags = var.tags
identity {
type = "SystemAssigned"
}
site_config {
always_on = var.always_on
http2_enabled = var.http2_enabled
minimum_tls_version = var.minimum_tls_version
application_stack {
python_version = var.python_version
}
}
app_settings = {
SCM_DO_BUILD_DURING_DEPLOYMENT = "true"
COSMOSDB_CONNECTION_STRING = azurerm_cosmosdb_account.example.primary_mongodb_connection_string
COSMOSDB_DATABASE_NAME = azurerm_cosmosdb_mongo_database.example.name
COSMOSDB_COLLECTION_NAME = var.cosmosdb_collection_name
LOGIN_NAME = var.login_name
}
lifecycle {
ignore_changes = [
tags
]
}
}
# Deploy code from a public GitHub repo
resource "azurerm_app_service_source_control" "example" {
count = var.repo_url == "" ? 0 : 1
app_id = azurerm_linux_web_app.example.id
repo_url = var.repo_url
branch = "main"
use_manual_integration = true
use_mercurial = false
}