Open jongio opened 9 months ago
I hacked terraform workspace support like this:
main.tf
locals {
workspace = var.workspace
is_default_workspace = local.workspace == "default"
}
main.tfvars.json
{
"workspace": "${WORKSPACE=default}"
}
variables.tf
variable "workspace" {
description = "value of workspace"
type = string
default = "dev"
}
preprovision.sh script to set the workspace
TF_WORKSPACE_DIR="${GITHUB_WORKSPACE:+$GITHUB_WORKSPACE/}.azure/${AZURE_ENV_NAME}/infra/.terraform"
# Create the directory if it doesn't exist
mkdir -p "$TF_WORKSPACE_DIR"
# Use the variable with the terraform command
terraform -chdir="$TF_WORKSPACE_DIR" workspace select -or-create "$WORKSPACE"
Then when a user wants to switch workspaces they would run
azd env set WORKSPACE azure
And conditionally provision resources like this:
resource "azurecaf_name" "aks_name" {
count = local.is_default_workspace ? 0 : 1
name = local.resource_token
resource_type = "azurerm_kubernetes_cluster"
random_length = 0
clean_input = true
}
When you do that you need to ref the conditional resources by index like this:
queue_id = azurerm_servicebus_queue.sb_queue[0].id
You also want to use try
when accessing a resource that is dependent on another resource so TF doesn't fail.
count = try(local.is_default_workspace ? 0 : 1, 0)
Terraform developers are accustomed to using terraform workspaces to configure different resources for different environments.
We should natively support this in azd.
https://developer.hashicorp.com/terraform/language/state/workspaces