-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
111 lines (89 loc) · 2.54 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
terraform {
required_version = ">= 0.13"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
client_id = var.client_id
subscription_id = var.subscription_id
tenant_id = var.tenant_id
storage_use_azuread = true
}
resource "random_string" "storage_account_name" {
length = 14
special = false
upper = false
}
locals {
storage_account_name = var.storage_account_name == null ? "tfstate00${random_string.storage_account_name.result}" : var.storage_account_name
}
resource "azurerm_resource_group" "tfstate" {
name = var.resource_group_name
location = var.location
lifecycle {
ignore_changes = [
tags,
]
}
}
resource "azurerm_storage_account" "tfstate" {
name = local.storage_account_name
resource_group_name = azurerm_resource_group.tfstate.name
location = azurerm_resource_group.tfstate.location
account_tier = "Standard"
account_replication_type = "GRS"
shared_access_key_enabled = false
lifecycle {
ignore_changes = [
tags,
]
}
}
resource "azurerm_storage_container" "tfstate" {
name = var.container_name
storage_account_name = azurerm_storage_account.tfstate.name
container_access_type = "private"
}
resource "terraform_data" "always_run" {
input = timestamp()
}
resource "null_resource" "sanitize_state" {
count = var.remove_secrets_from_state ? 1 : 0
provisioner "local-exec" {
command = <<EOT
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "jq is required but was not found. Please install jq to continue and then re-run apply."
exit 1
fi
# Sanitize the state file of private access keys:
jq 'del(.resources[].instances[].attributes [
"primary_access_key",
"primary_connection_string",
"primary_blob_connection_string",
"secondary_access_key",
"secondary_connection_string",
"secondary_blob_connection_string"
])' terraform.tfstate > sanitized.tfstate
mv sanitized.tfstate terraform.tfstate
EOT
}
depends_on = [azurerm_storage_container.tfstate]
lifecycle {
replace_triggered_by = [ terraform_data.always_run ]
}
}
output "resource_group_name" {
value = azurerm_resource_group.tfstate.name
}
output "storage_account_name" {
value = azurerm_storage_account.tfstate.name
}
output "container_name" {
value = azurerm_storage_container.tfstate.name
}