diff --git a/docs/prompt.md b/docs/prompt.md index 1fa3d6e..37dcbe7 100644 --- a/docs/prompt.md +++ b/docs/prompt.md @@ -42,6 +42,13 @@ The project should implement the following functionality: Use only null and local providers for this infrastructure. All operations should be performed using SSH connections and local-exec/remote-exec provisioners. Please implement each file with complete, working code that follows Terraform best practices. + +IMPORTANT CONSIDERATIONS: +1. Avoid duplicate local value declarations across files. If you create local values in env.tf, don't create variables with the same name in main.tf. +2. For destroy-time provisioners, remember that they can only reference attributes of the related resource using 'self', 'count.index', or 'each.key'. Store any paths or values needed during destroy in the resource's triggers, and then reference them using self.triggers.. +3. Ensure proper dependency management between resources to maintain correct deployment sequence. +4. Use resource-specific variables in destroy provisioners rather than module-level locals. +5. Implement validation for input variables where appropriate. ``` ## Modifying the Infrastructure diff --git a/terraform/infra/dev/main.tf b/terraform/infra/dev/main.tf index 8f0617b..0936f68 100644 --- a/terraform/infra/dev/main.tf +++ b/terraform/infra/dev/main.tf @@ -1,13 +1,3 @@ -locals { - # Load environment variables if .env file exists - env_vars = fileexists("${path.module}/.env") ? { - for line in [ - for line in split("\n", file("${path.module}/.env")) - : line if length(regexall("^[A-Za-z][A-Za-z0-9_]*=.*$", line)) > 0 - ] : split("=", line)[0] => join("=", slice(split("=", line), 1, length(split("=", line)))) - } : {} -} - module "k3s_cluster" { source = "../../modules/k3s-install" diff --git a/terraform/modules/k3s-install/main.tf b/terraform/modules/k3s-install/main.tf index 611cd52..228873d 100644 --- a/terraform/modules/k3s-install/main.tf +++ b/terraform/modules/k3s-install/main.tf @@ -27,6 +27,7 @@ resource "null_resource" "setup_ssh_config" { worker_ips = join(",", var.worker_ips) ssh_user = var.ssh_user ssh_private_key = var.ssh_private_key + ssh_config_path = local.ssh_config_path } provisioner "local-exec" { @@ -73,6 +74,7 @@ resource "null_resource" "install_k3s_server" { ssh_config_path = local.ssh_config_path k3s_version = var.k3s_version k3s_extra_args = var.k3s_extra_server_args + temporary_dir = local.temporary_dir } # Install K3s server @@ -104,7 +106,7 @@ resource "null_resource" "install_k3s_server" { inline = [ "/usr/local/bin/k3s-uninstall.sh || true", - "rm -rf ${local.temporary_dir} || true" + "rm -rf ${self.triggers.temporary_dir} || true" ] on_failure = continue } @@ -233,6 +235,7 @@ resource "null_resource" "install_k3s_worker" { ssh_config_path = local.ssh_config_path k3s_version = var.k3s_version k3s_extra_args = var.k3s_extra_agent_args + temporary_dir = local.temporary_dir } # Install K3s agent on worker @@ -263,7 +266,7 @@ resource "null_resource" "install_k3s_worker" { inline = [ "/usr/local/bin/k3s-agent-uninstall.sh || true", - "rm -rf ${local.temporary_dir} || true" + "rm -rf ${self.triggers.temporary_dir} || true" ] on_failure = continue }