-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
116 lines (100 loc) · 3.62 KB
/
Copy pathmain.tf
File metadata and controls
116 lines (100 loc) · 3.62 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
# Local Variables
locals {
resource_group_name = "${var.prefix}-aci-rg"
storage_account_name = "${var.prefix}acistorage${var.suffix}"
key_vault_name = "${var.prefix}acikv${var.suffix}"
acr_name = "${var.prefix}aciacr${var.suffix}"
aci_group_name = "${var.prefix}-aci-planner-${var.suffix}"
}
# Get the current client configuration (for tenant_id)
data "azurerm_client_config" "current" {}
# Create a resource group
resource "azurerm_resource_group" "example" {
name = local.resource_group_name
location = var.location
tags = var.tags
}
# Create a storage account
resource "azurerm_storage_account" "example" {
name = local.storage_account_name
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_replication_type = var.account_replication_type
account_kind = "StorageV2"
account_tier = var.account_tier
tags = var.tags
lifecycle {
ignore_changes = [
tags
]
}
}
# Create blob container
resource "azurerm_storage_container" "example" {
name = var.blob_container_name
storage_account_id = azurerm_storage_account.example.id
container_access_type = "private"
}
# Create Key Vault
resource "azurerm_key_vault" "example" {
name = local.key_vault_name
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
enable_rbac_authorization = true
tags = var.tags
lifecycle {
ignore_changes = [
tags
]
}
}
# Store the storage connection string in Key Vault
resource "azurerm_key_vault_secret" "storage_conn" {
name = "storage-conn"
value = "DefaultEndpointsProtocol=http;AccountName=${azurerm_storage_account.example.name};AccountKey=${azurerm_storage_account.example.primary_access_key};BlobEndpoint=${azurerm_storage_account.example.primary_blob_endpoint}"
key_vault_id = azurerm_key_vault.example.id
}
# Reference the pre-created ACR (created by deploy.sh before terraform apply)
data "azurerm_container_registry" "example" {
name = local.acr_name
resource_group_name = azurerm_resource_group.example.name
}
# Create Container Instance
resource "azurerm_container_group" "example" {
name = local.aci_group_name
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
os_type = "Linux"
restart_policy = "Always"
dns_name_label = local.aci_group_name
tags = var.tags
image_registry_credential {
server = data.azurerm_container_registry.example.login_server
username = data.azurerm_container_registry.example.admin_username
password = data.azurerm_container_registry.example.admin_password
}
container {
name = local.aci_group_name
image = "${data.azurerm_container_registry.example.login_server}/${var.image_name}:${var.image_tag}"
cpu = var.cpu_cores
memory = var.memory_in_gb
ports {
port = 80
protocol = "TCP"
}
environment_variables = {
BLOB_CONTAINER_NAME = var.blob_container_name
LOGIN_NAME = var.login_name
}
secure_environment_variables = {
AZURE_STORAGE_CONNECTION_STRING = azurerm_key_vault_secret.storage_conn.value
}
}
lifecycle {
ignore_changes = [
tags
]
}
}