@@ -5,19 +5,20 @@ locals {
55 vpc_zone_names = [ for index in range (max (local. zone_count , var. address_prefix_count )): " ${ var . region } -${ (index % local. zone_count ) + 1 } " ]
66 prefix_name = var. name_prefix != " " && var. name_prefix != null ? var. name_prefix : var. resource_group_name
77 vpc_name = lower (replace (var. name != " " ? var. name : " ${ local . prefix_name } -vpc" , " _" , " -" ))
8- vpc_id = data . ibm_is_vpc . vpc . id
8+ vpc_id = lookup (local . vpc , " id " , " " )
99 security_group_count = var. provision ? 2 : 0
10- security_group_ids = var. provision ? [ data . ibm_is_vpc . vpc . default_security_group , data . ibm_is_security_group . base . id ] : []
11- acl_id = data . ibm_is_vpc . vpc . default_network_acl
12- crn = data . ibm_is_vpc . vpc . resource_crn
10+ security_group_ids = var. provision && var . enabled ? [ lookup (local . vpc , " default_security_group" , " " ), data . ibm_is_security_group . base [ 0 ] . id ] : []
11+ acl_id = lookup (local . vpc , " default_network_acl" , " " )
12+ crn = lookup (local . vpc , " resource_crn" , " " )
1313 ipv4_cidr_provided = var. address_prefix_count > 0 && length (var. address_prefixes ) >= var. address_prefix_count
1414 ipv4_cidr_block = local. ipv4_cidr_provided ? var. address_prefixes : [ for val in range (var. address_prefix_count ): " " ]
1515 provision_cidr = var. provision && local. ipv4_cidr_provided
1616 base_security_group_name = var. base_security_group_name != null && var. base_security_group_name != " " ? var. base_security_group_name : " ${ local . vpc_name } -base"
17+ vpc = try (var. enabled ? data. ibm_is_vpc . vpc [0 ] : tomap (false ), {})
1718}
1819
1920resource ibm_is_vpc vpc {
20- count = var. provision ? 1 : 0
21+ count = var. provision && var . enabled ? 1 : 0
2122
2223 name = local. vpc_name
2324 resource_group = var. resource_group_id
@@ -28,23 +29,26 @@ resource ibm_is_vpc vpc {
2829}
2930
3031data ibm_is_vpc vpc {
32+ count = var. enabled ? 1 : 0
3133 depends_on = [ibm_is_vpc . vpc ]
3234
3335 name = local. vpc_name
3436}
3537
3638resource ibm_is_vpc_address_prefix cidr_prefix {
37- count = local. provision_cidr ? var. address_prefix_count : 0
39+ count = local. provision_cidr && var . enabled ? var. address_prefix_count : 0
3840
3941 name = " ${ local . vpc_name } -cidr-${ format (" %02s" , count. index )} "
4042 zone = local. vpc_zone_names [count . index ]
41- vpc = data . ibm_is_vpc . vpc . id
43+ vpc = lookup (local . vpc , " id " , " " )
4244 cidr = local. ipv4_cidr_block [count . index ]
4345 is_default = count. index < local. zone_count
4446}
4547
4648resource ibm_is_network_acl_rule allow_internal_egress {
47- network_acl = data. ibm_is_vpc . vpc . default_network_acl
49+ count = var. enabled ? 1 : 0
50+
51+ network_acl = lookup (local. vpc , " default_network_acl" , " " )
4852 name = " allow-internal-egress"
4953 action = " allow"
5054 source = var. internal_cidr
@@ -53,17 +57,21 @@ resource ibm_is_network_acl_rule allow_internal_egress {
5357}
5458
5559resource ibm_is_network_acl_rule allow_internal_ingress {
56- network_acl = data. ibm_is_vpc . vpc . default_network_acl
60+ count = var. enabled ? 1 : 0
61+
62+ network_acl = lookup (local. vpc , " default_network_acl" , " " )
5763 name = " allow-internal-ingress"
5864 action = " allow"
5965 source = var. internal_cidr
6066 destination = var. internal_cidr
6167 direction = " inbound"
62- before = ibm_is_network_acl_rule. deny_external_ssh . rule_id
68+ before = lookup ( ibm_is_network_acl_rule. deny_external_ssh [ 0 ], " rule_id" , " " )
6369}
6470
6571resource ibm_is_network_acl_rule deny_external_ssh {
66- network_acl = data. ibm_is_vpc . vpc . default_network_acl
72+ count = var. enabled ? 1 : 0
73+
74+ network_acl = lookup (local. vpc , " default_network_acl" , " " )
6775 name = " deny-external-ssh"
6876 action = " deny"
6977 source = " 0.0.0.0/0"
@@ -75,11 +83,13 @@ resource ibm_is_network_acl_rule deny_external_ssh {
7583 source_port_max = 22
7684 source_port_min = 22
7785 }
78- before = ibm_is_network_acl_rule. deny_external_rdp . rule_id
86+ before = lookup ( ibm_is_network_acl_rule. deny_external_rdp [ 0 ], " rule_id" , " " )
7987}
8088
8189resource ibm_is_network_acl_rule deny_external_rdp {
82- network_acl = data. ibm_is_vpc . vpc . default_network_acl
90+ count = var. enabled ? 1 : 0
91+
92+ network_acl = lookup (local. vpc , " default_network_acl" , " " )
8393 name = " deny-external-rdp"
8494 action = " deny"
8595 source = " 0.0.0.0/0"
@@ -91,11 +101,13 @@ resource ibm_is_network_acl_rule deny_external_rdp {
91101 source_port_max = 3389
92102 source_port_min = 3389
93103 }
94- before = ibm_is_network_acl_rule. deny_external_ingress . rule_id
104+ before = lookup ( ibm_is_network_acl_rule. deny_external_ingress [ 0 ], " rule_id" , " " )
95105}
96106
97107resource ibm_is_network_acl_rule deny_external_ingress {
98- network_acl = data. ibm_is_vpc . vpc . default_network_acl
108+ count = var. enabled ? 1 : 0
109+
110+ network_acl = lookup (local. vpc , " default_network_acl" , " " )
99111 name = " deny-external-ingress"
100112 action = " deny"
101113 source = " 0.0.0.0/0"
@@ -104,30 +116,25 @@ resource ibm_is_network_acl_rule deny_external_ingress {
104116}
105117
106118resource ibm_is_security_group base {
107- count = var. provision ? 1 : 0
119+ count = var. provision && var . enabled ? 1 : 0
108120
109121 name = local. base_security_group_name
110- vpc = data . ibm_is_vpc . vpc . id
122+ vpc = lookup (local . vpc , " id " , " " )
111123 resource_group = var. resource_group_id
112124}
113125
114126data ibm_is_security_group base {
127+ count = var. enabled ? 1 : 0
115128 depends_on = [ibm_is_security_group . base ]
116129
117130 name = local. base_security_group_name
118131}
119132
120- resource null_resource print_sg_name {
121- depends_on = [data . ibm_is_security_group . base ]
122-
123- provisioner "local-exec" {
124- command = " echo 'SG name: ${ data . ibm_is_security_group . base . name != null ? data . ibm_is_security_group . base . name : " null" } '"
125- }
126- }
127-
128133# from https://cloud.ibm.com/docs/vpc?topic=vpc-service-endpoints-for-vpc
129134resource ibm_is_security_group_rule default_inbound_ping {
130- group = data. ibm_is_vpc . vpc . default_security_group
135+ count = var. enabled ? 1 : 0
136+
137+ group = lookup (local. vpc , " default_security_group" , " " )
131138 direction = " inbound"
132139 remote = " 0.0.0.0/0"
133140
@@ -137,7 +144,9 @@ resource ibm_is_security_group_rule default_inbound_ping {
137144}
138145
139146resource ibm_is_security_group_rule default_inbound_http {
140- group = data. ibm_is_vpc . vpc . default_security_group
147+ count = var. enabled ? 1 : 0
148+
149+ group = lookup (local. vpc , " default_security_group" , " " )
141150 direction = " inbound"
142151 remote = " 0.0.0.0/0"
143152
@@ -148,7 +157,7 @@ resource ibm_is_security_group_rule default_inbound_http {
148157}
149158
150159resource ibm_is_security_group_rule cse_dns_1 {
151- count = local. security_group_count
160+ count = var . enabled ? local. security_group_count : 0
152161
153162 group = local. security_group_ids [count . index ]
154163 direction = " outbound"
@@ -160,7 +169,7 @@ resource ibm_is_security_group_rule cse_dns_1 {
160169}
161170
162171resource ibm_is_security_group_rule cse_dns_2 {
163- count = local. security_group_count
172+ count = var . enabled ? local. security_group_count : 0
164173
165174 group = local. security_group_ids [count . index ]
166175 direction = " outbound"
@@ -172,7 +181,7 @@ resource ibm_is_security_group_rule cse_dns_2 {
172181}
173182
174183resource ibm_is_security_group_rule private_dns_1 {
175- count = local. security_group_count
184+ count = var . enabled ? local. security_group_count : 0
176185
177186 group = local. security_group_ids [count . index ]
178187 direction = " outbound"
@@ -184,7 +193,7 @@ resource ibm_is_security_group_rule private_dns_1 {
184193}
185194
186195resource ibm_is_security_group_rule private_dns_2 {
187- count = local. security_group_count
196+ count = var . enabled ? local. security_group_count : 0
188197
189198 group = local. security_group_ids [count . index ]
190199 direction = " outbound"
0 commit comments