From 7b61d7c541536f61553facb30b76e79dc051c342 Mon Sep 17 00:00:00 2001 From: Florian Date: Wed, 26 Jul 2023 17:36:57 +0200 Subject: [PATCH] feature: randomnize rg, st names for tfstate --- kit/azure/bootstrap/main.tf | 8 +++---- .../template/platform-module/terragrunt.hcl | 6 ++++- kit/azure/bootstrap/terraform-state/main.tf | 23 ++++++++++++------- .../bootstrap/terraform-state/variables.tf | 2 +- .../bootstrap/terraform-state/version.tf | 9 ++++++++ kit/azure/bootstrap/variables.tf | 2 +- 6 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 kit/azure/bootstrap/terraform-state/version.tf diff --git a/kit/azure/bootstrap/main.tf b/kit/azure/bootstrap/main.tf index 46d37c8c..6ec21575 100644 --- a/kit/azure/bootstrap/main.tf +++ b/kit/azure/bootstrap/main.tf @@ -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 diff --git a/kit/azure/bootstrap/template/platform-module/terragrunt.hcl b/kit/azure/bootstrap/template/platform-module/terragrunt.hcl index aec6c6ec..92607759 100644 --- a/kit/azure/bootstrap/template/platform-module/terragrunt.hcl +++ b/kit/azure/bootstrap/template/platform-module/terragrunt.hcl @@ -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 = { diff --git a/kit/azure/bootstrap/terraform-state/main.tf b/kit/azure/bootstrap/terraform-state/main.tf index 4a179f13..f9c17a00 100644 --- a/kit/azure/bootstrap/terraform-state/main.tf +++ b/kit/azure/bootstrap/terraform-state/main.tf @@ -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" @@ -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 } diff --git a/kit/azure/bootstrap/terraform-state/variables.tf b/kit/azure/bootstrap/terraform-state/variables.tf index 7fa7a16a..ef8c86f8 100644 --- a/kit/azure/bootstrap/terraform-state/variables.tf +++ b/kit/azure/bootstrap/terraform-state/variables.tf @@ -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" diff --git a/kit/azure/bootstrap/terraform-state/version.tf b/kit/azure/bootstrap/terraform-state/version.tf new file mode 100644 index 00000000..4764a177 --- /dev/null +++ b/kit/azure/bootstrap/terraform-state/version.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 1.0" + required_providers { + azurecaf = { + source = "aztfmod/azurecaf" + version = "~> 1.2.26" + } + } +} diff --git a/kit/azure/bootstrap/variables.tf b/kit/azure/bootstrap/variables.tf index 30123fd6..b7a151b1 100644 --- a/kit/azure/bootstrap/variables.tf +++ b/kit/azure/bootstrap/variables.tf @@ -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"