Skip to content

Commit 61e2731

Browse files
committed
Add random suffix to network resources to prevent naming collisions
- Add random_id resource for network naming - Apply random suffix to VPC, subnet, NAT gateway, router, and VPC connector names - This prevents conflicts with existing resources from previous test runs
1 parent 35a3414 commit 61e2731

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

locals.tf

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Random suffix for network resources to avoid naming collisions
2+
resource "random_id" "network_suffix" {
3+
byte_length = 4
4+
}
5+
16
locals {
27
# Resource naming
38
resource_prefix = var.name_prefix
@@ -23,13 +28,14 @@ locals {
2328
# Service account names
2429
app_service_account = "${local.resource_prefix}-app"
2530

26-
# Network names
27-
vpc_name = "${local.resource_prefix}-vpc"
28-
private_subnet_name = "${local.resource_prefix}-private-subnet"
29-
nat_gateway_name = "${local.resource_prefix}-nat-gateway"
30-
router_name = "${local.resource_prefix}-router"
31+
# Network names with random suffix to avoid naming collisions
32+
vpc_name = "${local.resource_prefix}-vpc-${random_id.network_suffix.hex}"
33+
private_subnet_name = "${local.resource_prefix}-subnet-${random_id.network_suffix.hex}"
34+
nat_gateway_name = "${local.resource_prefix}-nat-${random_id.network_suffix.hex}"
35+
router_name = "${local.resource_prefix}-router-${random_id.network_suffix.hex}"
3136
# VPC connector name must be <= 25 chars and match ^[a-z][-a-z0-9]{0,23}[a-z0-9]$
32-
vpc_connector_name = length("${local.resource_prefix}-connector") <= 25 ? "${local.resource_prefix}-connector" : "${substr(local.resource_prefix, 0, 15)}-connector"
37+
# Add random suffix to avoid naming collisions
38+
vpc_connector_name = length("${local.resource_prefix}-conn-${random_id.network_suffix.hex}") <= 25 ? "${local.resource_prefix}-conn-${random_id.network_suffix.hex}" : "${substr(local.resource_prefix, 0, 11)}-conn-${random_id.network_suffix.hex}"
3339

3440
# Cloud Run service names
3541
main_app_service_name = "${local.resource_prefix}-app"

0 commit comments

Comments
 (0)