-
Notifications
You must be signed in to change notification settings - Fork 5
/
02_Storage_Account.tf
116 lines (92 loc) · 2.95 KB
/
02_Storage_Account.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
#############################################################################
# VARIABLES
#############################################################################
variable "resource_group_name" {
type = string
}
variable "location" {
type = string
default = "westeurope"
}
variable "naming_prefix" {
type = string
default = "itma"
}
##################################################################################
# PROVIDERS
##################################################################################
provider "azurerm" {
features {}
}
##################################################################################
# RESOURCES
##################################################################################
resource "random_integer" "sa_num" {
min = 10000
max = 99999
}
resource "azurerm_resource_group" "setup" {
name = var.resource_group_name
location = var.location
}
resource "azurerm_storage_account" "sa" {
name = "${lower(var.naming_prefix)}${random_integer.sa_num.result}"
resource_group_name = azurerm_resource_group.setup.name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_storage_container" "ct" {
name = "terraform-state"
storage_account_name = azurerm_storage_account.sa.name
}
data "azurerm_storage_account_sas" "state" {
connection_string = azurerm_storage_account.sa.primary_connection_string
https_only = true
resource_types {
service = true
container = true
object = true
}
services {
blob = true
queue = false
table = false
file = false
}
start = timestamp()
expiry = timeadd(timestamp(), "17520h")
permissions {
read = true
write = true
delete = true
list = true
add = true
create = true
update = false
process = false
}
}
#############################################################################
# PROVISIONERS
#############################################################################
resource "null_resource" "post-config" {
depends_on = [azurerm_storage_container.ct]
provisioner "local-exec" {
command = <<EOT
echo 'storage_account_name = "${azurerm_storage_account.sa.name}"' >> backend-config.txt
echo 'container_name = "terraform-state"' >> backend-config.txt
echo 'key = "terraform.tfstate"' >> backend-config.txt
echo 'sas_token = "${data.azurerm_storage_account_sas.state.sas}"' >> backend-config.txt
EOT
}
}
##################################################################################
# OUTPUT
##################################################################################
output "storage_account_name" {
value = azurerm_storage_account.sa.name
}
output "resource_group_name" {
value = azurerm_resource_group.setup.name
}