Skip to content

Commit

Permalink
feature: randomnize rg, st names for tfstate
Browse files Browse the repository at this point in the history
  • Loading branch information
florianow committed Jul 26, 2023
1 parent 720b839 commit 7b61d7c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 deletions.
8 changes: 4 additions & 4 deletions kit/azure/bootstrap/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ data "azurerm_management_group" "root" {
module "terraform_state" {
count = var.terraform_state_storage != null ? 1 : 0

source = "./terraform-state"
location = var.terraform_state_storage.location
file_path = var.file_path
resource_group_tfstate = var.resource_group_tfstate
source = "./terraform-state"
location = var.terraform_state_storage.location
file_path = var.file_path
resources_tfstate = var.resources_tfstate
}

# Set permissions on the blob store
Expand Down
6 changes: 5 additions & 1 deletion kit/azure/bootstrap/template/platform-module/terragrunt.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ EOF
}

inputs = {
resource_group_tfstate = "cloudfoundation_tfstate" #TODO change, name your rg fo the statefiles
# for creation of the resource_group and storage container we are using the
# https://registry.terraform.io/providers/aztfmod/azurecaf/latest/docs/resources/azurecaf_naming_convention
# you only need the the name of your foundation like likvid the result would like rg-tfstate-likvid-ewt

resources_tfstate = "cloudfoundation" #TODO change, name your rg fo the statefiles
service_principal_name = "cloudfoundation_tf_deploy" #TODO change, name your spn

terraform_state_storage = {
Expand Down
23 changes: 15 additions & 8 deletions kit/azure/bootstrap/terraform-state/main.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
resource "random_string" "resource_code" {
length = 5
special = false
upper = false
resource "azurecaf_name" "cafrandom_rg" {
name = var.resources_tfstate
resource_type = "azurerm_resource_group"
prefixes = ["tfstate"]
random_length = 3
}
resource "azurecaf_name" "cafrandom_st" {
name = var.resources_tfstate
resource_type = "azurerm_storage_account"
prefixes = ["tfstate"]
random_length = 3
}

resource "azurerm_resource_group" "tfstates" {
name = var.resource_group_tfstate
name = azurecaf_name.cafrandom_rg.result
location = var.location
}

resource "azurerm_storage_account" "tfstates" {
name = "tfstates${random_string.resource_code.result}"
name = azurecaf_name.cafrandom_st.result
resource_group_name = azurerm_resource_group.tfstates.name
location = azurerm_resource_group.tfstates.location
account_tier = "Standard"
Expand All @@ -27,9 +34,9 @@ resource "azurerm_storage_container" "tfstates" {
resource "local_file" "tfstates_yaml" {
filename = var.file_path
content = <<-EOT
storage_account_name: ${azurerm_storage_account.tfstates.name}
storage_account_name: ${azurecaf_name.cafrandom_st.result}
container_name: ${azurerm_storage_container.tfstates.name}
resource_group_name: ${azurerm_resource_group.tfstates.name}
resource_group_name: ${azurecaf_name.cafrandom_rg.result}
EOT
}
2 changes: 1 addition & 1 deletion kit/azure/bootstrap/terraform-state/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ variable "file_path" {
description = "tfstate-config file for running the bootstrap"
}

variable "resource_group_tfstate" {
variable "resources_tfstate" {
type = string
nullable = false
description = "tfstate resource group for the statefiles"
Expand Down
9 changes: 9 additions & 0 deletions kit/azure/bootstrap/terraform-state/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.0"
required_providers {
azurecaf = {
source = "aztfmod/azurecaf"
version = "~> 1.2.26"
}
}
}
2 changes: 1 addition & 1 deletion kit/azure/bootstrap/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ variable "file_path" {
description = "tfstate-config file for running the bootstrap"
}

variable "resource_group_tfstate" {
variable "resources_tfstate" {
type = string
nullable = false
description = "tfstate resource group for the statefiles"
Expand Down

0 comments on commit 7b61d7c

Please sign in to comment.