Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate independet objects for terraform-state resources #1102

Merged
merged 5 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions qhub/stages/input_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
11 changes: 6 additions & 5 deletions qhub/stages/state_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
(
Expand All @@ -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":
Expand Down
4 changes: 2 additions & 2 deletions qhub/stages/tf_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
12 changes: 9 additions & 3 deletions qhub/template/stages/01-terraform-state/azure/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ variable "storage_account_postfix" {
type = string
}

variable "state_resource_group_name" {
description = "Name for terraform state resource group"
type = string
}

provider "azurerm" {
features {}
}

module "terraform-state" {
source = "./modules/terraform-state"

resource_group_name = "${var.name }-${var.namespace}"
location = var.region
storage_account_postfix = var.storage_account_postfix
name = "${var.name}-${var.namespace}"
resource_group_name = var.state_resource_group_name
location = var.region
storage_account_postfix = var.storage_account_postfix
}

terraform {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
resource "azurerm_resource_group" "terraform-resource-group" {
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("${var.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
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"

Expand All @@ -16,7 +17,7 @@ resource "azurerm_storage_account" "terraform-storage-account" {
}

resource "azurerm_storage_container" "storage_container" {
name = "${var.resource_group_name}state"
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"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
variable "resource_group_name" {
description = "Prefix name for terraform state"
description = "Prefix of name to append resource"
type = string
}

variable "name" {
description = "Prefix of name to append resource"
type = string
}

Expand Down