I have a terraform script to provision environment based on condition dev prod or both
Now the issue is if i assign value as dev .It is provisioning dev resouces .If “prod” its destroying dev resources and creating prod resources.But i want the dev resouces to exist
Insted of destroy i want to target sepcific resoucres.
EX
if both → both dev and prod
if prod —> the it should target prod resouces not delete dev
if dev → it should target dev resources not delete prod.
How to achieve this in the best way possible.I tried for_each too in place of count. But its still destroying .
variable “environment_type_env” {
type = string
description = “Specifies which environment(s) to provision: ‘dev’, ‘prod’, or ‘both’”
validation {
condition = contains([“dev”, “prod”, “both”], var.environment_type_env)
error_message = “The environment_type variable must be one of ‘dev’, ‘prod’, or ‘both’.”
}
}
locals {
create_dev_env = contains([“dev”, “both”], var.environment_type_env)
create_prod_env = contains([“prod”, “both”], var.environment_type_env)
}
resource “google_compute_network” “client_network_private” {
count = var.operation_type == “Failover” ? 0 : (local.create_dev_env || local.create_prod_env ? 1 : 0)
name = join(“-”, [“${var.customer_name}”, “network-private”])
auto_create_subnetworks = false
}
resource “google_compute_subnetwork” “client_subnet_private” {
count = var.operation_type == “Failover” ? 0 : (local.create_dev_env || local.create_prod_env ? 1 : 0)
name = var.operation_type == “Failover” ? “{join("-", ["{var.customer_name}”, “subnet-private-dr”])}" : “{join("-", ["{var.customer_name}”, “subnet-private”])}"
region = var.operation_type == “Failover” ? “{lookup(var.dr_region_selection, var.customer_region, "")}" : "{lookup(var.region_selection, var.customer_region, “”)}”
ip_cidr_range = var.operation_type == “Failover” ? “10.7.0.0/16” : “10.3.0.0/16”
network = join(“-”, [“${var.customer_name}”, “network-private”])
secondary_ip_range {
range_name = var.operation_type == “Failover” ? “pod-network-dr” : “pod-network”
ip_cidr_range = var.operation_type == “Failover” ? “10.5.0.0/16” : “10.1.0.0/16”
}
secondary_ip_range {
range_name = var.operation_type == “Failover” ? “service-network-dr” : “service-network”
ip_cidr_range = var.operation_type == “Failover” ? “10.6.0.0/16” : “10.2.0.0/16”
}
dynamic “secondary_ip_range” {
for_each = local.create_prod_env ? [1] :
content {
range_name = var.operation_type == “Failover” ? “customer-pod-network-dr” : “customer-pod-network”
ip_cidr_range = var.operation_type == “Failover” ? “10.8.0.0/16” : “10.9.0.0/16”
}
}
dynamic “secondary_ip_range” {
for_each = local.create_prod_env ? [1] :
content {
range_name = var.operation_type == “Failover” ? “customer-service-network-dr” : “customer-service-network”
ip_cidr_range = var.operation_type == “Failover” ? “10.10.0.0/16” : “10.11.0.0/16”
}
}
depends_on = [google_compute_network.client_network_private]
}
resource “google_compute_router” “client_router” {
count = var.operation_type == “Failover” ? 0 : (local.create_dev_env || local.create_prod_env ? 1 : 0)
name = var.operation_type == “Failover” ? “{join("-", ["{var.customer_name}”, “router-dr”])}" : “{join("-", ["{var.customer_name}”, “router”])}"
region = google_compute_subnetwork.client_subnet_private[0].region
network = join(“-”, [“${var.customer_name}”, “network-private”])
depends_on = [google_compute_subnetwork.client_subnet_private]
}
resource “google_compute_router_nat” “client_nat” {
count = var.operation_type == “Failover” ? 0 : (local.create_dev_env || local.create_prod_env ? 1 : 0)
router = google_compute_router.client_router[0].name
region = google_compute_router.client_router[0].region
name = var.operation_type == “Failover” ? “{join("-", ["{var.customer_name}”, “router-nat-dr”])}" : “{join("-", ["{var.customer_name}”, “router-nat”])}"
nat_ip_allocate_option = “MANUAL_ONLY”
nat_ips = [google_compute_address.client_nat_ip[0].self_link]
source_subnetwork_ip_ranges_to_nat = “ALL_SUBNETWORKS_ALL_IP_RANGES”
depends_on = [google_compute_router.client_router, google_compute_address.client_nat_ip]
}
resource “google_compute_global_address” “private_service_ip” {
count = var.operation_type == “Failover” ? 0 : (local.create_dev_env || local.create_prod_env ? 1 : 0)
name = join(“-”, [“{var.customer_name}", "private-service-ip"])
purpose = "VPC_PEERING"
address_type = "INTERNAL"
address = "10.4.0.0"
prefix_length = 16
network = join("-", ["{var.customer_name}”, “network-private”])
depends_on = [google_compute_subnetwork.client_subnet_private]
}
resource “google_service_networking_connection” “private_service_connection” {
count = var.operation_type == “Failover” ? 0 : (local.create_dev_env || local.create_prod_env ? 1 : 0)
network = join(“-”, [“{var.customer_name}", "network-private"])
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = ["{join(”-“, [”${var.customer_name}“, “private-service-ip”])}”]
depends_on = [google_compute_global_address.private_service_ip]
}
resource “google_compute_address” “client_nat_ip” {
count = local.create_dev_env || local.create_prod_env ? 1 : 0
name = var.operation_type == “Failover” || var.operation_type == “Failback” ? “{join("-", ["{var.customer_name}”, “client-nat-ip-dr”])}" : “{join("-", ["{var.customer_name}”, “client-nat-ip-prod”])}"
region = var.operation_type == “Failover” || var.operation_type == “Failback” ? lookup(var.dr_region_selection, var.customer_region, “”) : lookup(var.region_selection, var.customer_region, “”)
}
data “google_compute_address” “client_nat_address” {
count = local.create_dev_env || local.create_prod_env ? 1 : 0
name = var.operation_type == “Failover” || var.operation_type == “Failback” ? “{join("-", ["{var.customer_name}”, “client-nat-ip-dr”])}" : “{join("-", ["{var.customer_name}”, “client-nat-ip-prod”])}"
region = var.operation_type == “Failover” || var.operation_type == “Failback” ? lookup(var.dr_region_selection, var.customer_region, “”) : lookup(var.region_selection, var.customer_region, “”)
depends_on = [google_compute_address.client_nat_ip]
}
resource “google_compute_address” “client_public_ip_prod” {
count = local.create_prod_env ? 1 : 0
name = var.operation_type == “Failover” || var.operation_type == “Failback” ? “{join("-", ["{var.customer_name}”, “public-ip-prod-dr”])}" : “{join("-", ["{var.customer_name}”, “public-ip-prod”])}"
region = var.operation_type == “Failover” || var.operation_type == “Failback” ? lookup(var.dr_region_selection, var.customer_region, “”) : lookup(var.region_selection, var.customer_region, “”)
}
data “google_compute_address” “public_ip_prod” {
count = local.create_prod_env ? 1 : 0
name = var.operation_type == “Failover” || var.operation_type == “Failback” ? “{join("-", ["{var.customer_name}”, “public-ip-prod-dr”])}" : “{join("-", ["{var.customer_name}”, “public-ip-prod”])}"
region = var.operation_type == “Failover” || var.operation_type == “Failback” ? lookup(var.dr_region_selection, var.customer_region, “”) : lookup(var.region_selection, var.customer_region, “”)
depends_on = [google_compute_address.client_public_ip_prod]
}
resource “google_compute_address” “client_public_ip_nonprod” {
count = local.create_dev_env ? 1 : 0
name = var.operation_type == “Failover” || var.operation_type == “Failback” ? “{join("-", ["{var.customer_name}”, “public-ip-nonprod-dr”])}" : “{join("-", ["{var.customer_name}”, “public-ip-nonprod”])}"
region = var.operation_type == “Failover” || var.operation_type == “Failback” ? lookup(var.dr_region_selection, var.customer_region, “”) : lookup(var.region_selection, var.customer_region, “”)
}
data “google_compute_address” “public_ip_nonprod” {
count = local.create_dev_env ? 1 : 0
name = var.operation_type == “Failover” || var.operation_type == “Failback” ? “{join("-", ["{var.customer_name}”, “public-ip-nonprod-dr”])}" : “{join("-", ["{var.customer_name}”, “public-ip-nonprod”])}"
region = var.operation_type == “Failover” || var.operation_type == “Failback” ? lookup(var.dr_region_selection, var.customer_region, “”) : lookup(var.region_selection, var.customer_region, “”)
depends_on = [google_compute_address.client_public_ip_nonprod]
}