From 338c941ad88d3e2536e99960307db8cddf939e09 Mon Sep 17 00:00:00 2001 From: viniciusdc Date: Tue, 22 Feb 2022 15:55:45 -0300 Subject: [PATCH 1/5] Modify terraform-state resource group --- qhub/template/stages/01-terraform-state/azure/main.tf | 6 +++--- .../azure/modules/terraform-state/main.tf | 6 +++--- .../azure/modules/terraform-state/variables.tf | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/qhub/template/stages/01-terraform-state/azure/main.tf b/qhub/template/stages/01-terraform-state/azure/main.tf index 992b46d149..9f44c85b48 100644 --- a/qhub/template/stages/01-terraform-state/azure/main.tf +++ b/qhub/template/stages/01-terraform-state/azure/main.tf @@ -25,9 +25,9 @@ provider "azurerm" { module "terraform-state" { source = "./modules/terraform-state" - resource_group_name = "${var.name }-${var.namespace}" - location = var.region - storage_account_postfix = var.storage_account_postfix + state_resource_group_name = "${var.name }-${var.namespace}-terraform-state" + location = var.region + storage_account_postfix = var.storage_account_postfix } terraform { diff --git a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf index b0dbc834b0..f04cea761d 100644 --- a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf +++ b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf @@ -1,10 +1,10 @@ resource "azurerm_resource_group" "terraform-resource-group" { - name = var.resource_group_name + name = var.state_resource_group_name location = var.location } resource "azurerm_storage_account" "terraform-storage-account" { - name = replace("${var.resource_group_name}${var.storage_account_postfix}", "-", "") # must be unique across the entire Azure service + name = replace("${var.state_resource_group_name}${var.storage_account_postfix}", "-", "") # must be unique across the entire Azure service resource_group_name = azurerm_resource_group.terraform-resource-group.name location = azurerm_resource_group.terraform-resource-group.location account_tier = "Standard" @@ -16,7 +16,7 @@ resource "azurerm_storage_account" "terraform-storage-account" { } resource "azurerm_storage_container" "storage_container" { - name = "${var.resource_group_name}state" + name = "${var.state_resource_group_name}state" storage_account_name = azurerm_storage_account.terraform-storage-account.name container_access_type = "private" } diff --git a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf index 52ee041ad0..9febdfc5c0 100644 --- a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf +++ b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf @@ -1,5 +1,5 @@ -variable "resource_group_name" { - description = "Prefix name for terraform state" +variable "state_resource_group_name" { + description = "Prefix name for terraform state resource group" type = string } From f8ac410c7ec2a42ba517df16a587ef48b923b893 Mon Sep 17 00:00:00 2001 From: viniciusdc Date: Tue, 22 Feb 2022 16:03:52 -0300 Subject: [PATCH 2/5] Follow other providers name var standard --- qhub/template/stages/01-terraform-state/azure/main.tf | 2 +- .../azure/modules/terraform-state/main.tf | 11 ++++++++--- .../azure/modules/terraform-state/variables.tf | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/qhub/template/stages/01-terraform-state/azure/main.tf b/qhub/template/stages/01-terraform-state/azure/main.tf index 9f44c85b48..88d8d5ebca 100644 --- a/qhub/template/stages/01-terraform-state/azure/main.tf +++ b/qhub/template/stages/01-terraform-state/azure/main.tf @@ -25,7 +25,7 @@ provider "azurerm" { module "terraform-state" { source = "./modules/terraform-state" - state_resource_group_name = "${var.name }-${var.namespace}-terraform-state" + name = "${var.name }-${var.namespace}" location = var.region storage_account_postfix = var.storage_account_postfix } diff --git a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf index f04cea761d..08a444f48d 100644 --- a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf +++ b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf @@ -1,10 +1,15 @@ +locals { + # Prefix name for terraform state resources + state_resource_name = "${var.name }-${var.namespace}-terraform-state" +} + resource "azurerm_resource_group" "terraform-resource-group" { - name = var.state_resource_group_name + name = local.state_resource_name location = var.location } resource "azurerm_storage_account" "terraform-storage-account" { - name = replace("${var.state_resource_group_name}${var.storage_account_postfix}", "-", "") # must be unique across the entire Azure service + name = replace("${local.state_resource_name}${var.storage_account_postfix}", "-", "") # must be unique across the entire Azure service resource_group_name = azurerm_resource_group.terraform-resource-group.name location = azurerm_resource_group.terraform-resource-group.location account_tier = "Standard" @@ -16,7 +21,7 @@ resource "azurerm_storage_account" "terraform-storage-account" { } resource "azurerm_storage_container" "storage_container" { - name = "${var.state_resource_group_name}state" + name = local.state_resource_name storage_account_name = azurerm_storage_account.terraform-storage-account.name container_access_type = "private" } diff --git a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf index 9febdfc5c0..b0bc854035 100644 --- a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf +++ b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf @@ -1,5 +1,5 @@ -variable "state_resource_group_name" { - description = "Prefix name for terraform state resource group" +variable "name" { + description = "Prefix of name to append resource" type = string } From c4a3b317137b2bb24bed20ac836440dd7eddaeee Mon Sep 17 00:00:00 2001 From: viniciusdc Date: Tue, 22 Feb 2022 18:42:31 -0300 Subject: [PATCH 3/5] Too confusing --- .../stages/01-terraform-state/azure/main.tf | 8 ++++++- .../azure/modules/terraform-state/main.tf | 22 ++++++++----------- .../modules/terraform-state/variables.tf | 5 +++++ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/qhub/template/stages/01-terraform-state/azure/main.tf b/qhub/template/stages/01-terraform-state/azure/main.tf index 88d8d5ebca..f766254d32 100644 --- a/qhub/template/stages/01-terraform-state/azure/main.tf +++ b/qhub/template/stages/01-terraform-state/azure/main.tf @@ -18,6 +18,11 @@ variable "storage_account_postfix" { type = string } +variable "state_resource_group_name" { + description = "Name for terraform state resource group" + type = string +} + provider "azurerm" { features {} } @@ -25,7 +30,8 @@ provider "azurerm" { module "terraform-state" { source = "./modules/terraform-state" - name = "${var.name }-${var.namespace}" + name = "${var.name}-${var.namespace}" + resource_group_name = var.state_resource_group_name location = var.region storage_account_postfix = var.storage_account_postfix } diff --git a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf index 08a444f48d..3b0fbfb38e 100644 --- a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf +++ b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/main.tf @@ -1,17 +1,13 @@ -locals { - # Prefix name for terraform state resources - state_resource_name = "${var.name }-${var.namespace}-terraform-state" -} - -resource "azurerm_resource_group" "terraform-resource-group" { - name = local.state_resource_name +resource "azurerm_resource_group" "terraform-state-resource-group" { + name = var.resource_group_name location = var.location } -resource "azurerm_storage_account" "terraform-storage-account" { - name = replace("${local.state_resource_name}${var.storage_account_postfix}", "-", "") # must be unique across the entire Azure service - resource_group_name = azurerm_resource_group.terraform-resource-group.name - location = azurerm_resource_group.terraform-resource-group.location +resource "azurerm_storage_account" "terraform-state-storage-account" { + # name, can only consist of lowercase letters and numbers, and must be between 3 and 24 characters long + name = replace("${var.name}${var.storage_account_postfix}", "-", "") # must be unique across the entire Azure service + resource_group_name = azurerm_resource_group.terraform-state-resource-group.name + location = azurerm_resource_group.terraform-state-resource-group.location account_tier = "Standard" account_replication_type = "GRS" @@ -21,7 +17,7 @@ resource "azurerm_storage_account" "terraform-storage-account" { } resource "azurerm_storage_container" "storage_container" { - name = local.state_resource_name - storage_account_name = azurerm_storage_account.terraform-storage-account.name + name = "${var.name}-state" + storage_account_name = azurerm_storage_account.terraform-state-storage-account.name container_access_type = "private" } diff --git a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf index b0bc854035..7ed3b84efc 100644 --- a/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf +++ b/qhub/template/stages/01-terraform-state/azure/modules/terraform-state/variables.tf @@ -1,3 +1,8 @@ +variable "resource_group_name" { + description = "Prefix of name to append resource" + type = string +} + variable "name" { description = "Prefix of name to append resource" type = string From 5b9b240453fc2450209d8570c41f35032ff932aa Mon Sep 17 00:00:00 2001 From: viniciusdc Date: Tue, 22 Feb 2022 18:43:29 -0300 Subject: [PATCH 4/5] Update backend config --- qhub/stages/input_vars.py | 1 + qhub/stages/state_imports.py | 11 ++++++----- qhub/stages/tf_objects.py | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/qhub/stages/input_vars.py b/qhub/stages/input_vars.py index 23a9c4d3b5..f10a756338 100644 --- a/qhub/stages/input_vars.py +++ b/qhub/stages/input_vars.py @@ -28,6 +28,7 @@ def stage_01_terraform_state(stage_outputs, config): "namespace": config["namespace"], "region": config["azure"]["region"], "storage_account_postfix": config["azure"]["storage_account_postfix"], + "state_resource_group_name": f'{config["project_name"]}-{config["namespace"]}-state', } else: return {} diff --git a/qhub/stages/state_imports.py b/qhub/stages/state_imports.py index 2ac1fa77d9..a99f2a3fdb 100644 --- a/qhub/stages/state_imports.py +++ b/qhub/stages/state_imports.py @@ -18,9 +18,10 @@ def stage_01_terraform_state(stage_outputs, config): ] elif config["provider"] == "azure": subscription_id = os.environ["ARM_SUBSCRIPTION_ID"] - resource_group_name = f"{config['project_name']}-{config['namespace']}" - resource_group_name_safe = resource_group_name.replace("-", "") - resource_group_url = f"/subscriptions/{subscription_id}/resourceGroups/{config['project_name']}-{config['namespace']}" + resource_name_prefix = f"{config['project_name']}-{config['namespace']}" + state_resource_group_name = f"{resource_name_prefix}-state" + state_resource_name_prefix_safe = resource_name_prefix.replace("-", "") + resource_group_url = f"/subscriptions/{subscription_id}/resourceGroups/{state_resource_group_name}" return [ ( @@ -29,11 +30,11 @@ def stage_01_terraform_state(stage_outputs, config): ), ( "module.terraform-state.azurerm_storage_account.terraform-storage-account", - f"{resource_group_url}/providers/Microsoft.Storage/storageAccounts/{resource_group_name_safe}{config['azure']['storage_account_postfix']}", + f"{resource_group_url}/providers/Microsoft.Storage/storageAccounts/{state_resource_name_prefix_safe}{config['azure']['storage_account_postfix']}", ), ( "module.terraform-state.azurerm_storage_container.storage_container", - f"https://{resource_group_name_safe}{config['azure']['storage_account_postfix']}.blob.core.windows.net/{resource_group_name}state", + f"https://{state_resource_name_prefix_safe}{config['azure']['storage_account_postfix']}.blob.core.windows.net/{resource_name_prefix}-state", ), ] elif config["provider"] == "aws": diff --git a/qhub/stages/tf_objects.py b/qhub/stages/tf_objects.py index e018098a49..1c71819dee 100644 --- a/qhub/stages/tf_objects.py +++ b/qhub/stages/tf_objects.py @@ -78,10 +78,10 @@ def QHubTerraformState(directory: str, qhub_config: Dict): elif qhub_config["provider"] == "azure": return TerraformBackend( "azurerm", - resource_group_name=f"{qhub_config['project_name']}-{qhub_config['namespace']}", + resource_group_name=f"{qhub_config['project_name']}-{qhub_config['namespace']}-state", # storage account must be globally unique storage_account_name=f"{qhub_config['project_name']}{qhub_config['namespace']}{qhub_config['azure']['storage_account_postfix']}", - container_name=f"{qhub_config['project_name']}-{qhub_config['namespace']}state", + container_name=f"{qhub_config['project_name']}-{qhub_config['namespace']}-state", key=f"terraform/{qhub_config['project_name']}-{qhub_config['namespace']}/{directory}", ) elif qhub_config["provider"] == "local": From 928b7cac1ddd74a338fc4cab1c4b4006e18d6555 Mon Sep 17 00:00:00 2001 From: viniciusdc Date: Tue, 22 Feb 2022 20:53:38 -0300 Subject: [PATCH 5/5] Update resource names for state-blocks --- qhub/stages/state_imports.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qhub/stages/state_imports.py b/qhub/stages/state_imports.py index a99f2a3fdb..77d3ce3679 100644 --- a/qhub/stages/state_imports.py +++ b/qhub/stages/state_imports.py @@ -25,11 +25,11 @@ def stage_01_terraform_state(stage_outputs, config): return [ ( - "module.terraform-state.azurerm_resource_group.terraform-resource-group", + "module.terraform-state.azurerm_resource_group.terraform-state-resource-group", resource_group_url, ), ( - "module.terraform-state.azurerm_storage_account.terraform-storage-account", + "module.terraform-state.azurerm_storage_account.terraform-state-storage-account", f"{resource_group_url}/providers/Microsoft.Storage/storageAccounts/{state_resource_name_prefix_safe}{config['azure']['storage_account_postfix']}", ), (