-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsubnet.tf
More file actions
63 lines (51 loc) · 2.89 KB
/
subnet.tf
File metadata and controls
63 lines (51 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
##############################################################################
# Multizone subnets
##############################################################################
locals {
subnet_object = module.dynamic_values.subnet_map
subnets = var.create_subnets ? ibm_is_subnet.subnet : { for subnet in data.ibm_is_subnet.subnet :
subnet.name => subnet }
}
##############################################################################
##############################################################################
# Create new address prefixes
##############################################################################
resource "ibm_is_vpc_address_prefix" "subnet_prefix" {
# Address prefixes replace subnet prefixes
# Only create prefix if creating subnets, flag not set to disable prefix creation, and no specific prefixes were supplied
for_each = { for k, v in local.subnet_object : k => v if(v.no_prefix == false && var.create_subnets == true && length(local.address_prefixes) == 0) }
name = each.value.prefix_name
zone = each.value.zone_name
vpc = local.vpc_id
cidr = each.value.cidr
}
##############################################################################
##############################################################################
# Create Subnets
##############################################################################
resource "ibm_is_subnet" "subnet" {
for_each = var.create_subnets ? local.subnet_object : {}
vpc = local.vpc_id
name = each.key
zone = each.value.zone_name
resource_group = var.resource_group_id
ipv4_cidr_block = length(keys(local.address_prefixes)) == 0 && !each.value.no_prefix ? ibm_is_vpc_address_prefix.subnet_prefix[each.value.prefix_name].cidr : each.value.cidr
network_acl = ibm_is_network_acl.network_acl[each.value.acl].id
public_gateway = each.value.public_gateway
tags = var.tags
access_tags = var.access_tags
depends_on = [ibm_is_vpc_address_prefix.address_prefixes]
}
data "ibm_is_subnet" "subnet" {
for_each = var.create_subnets == false ? { for subnet in var.existing_subnets : subnet.id => subnet } : {}
identifier = each.key
}
# if using existing subnets, attach public gateways as configured
resource "ibm_is_subnet_public_gateway_attachment" "exist_subnet_gw" {
# only choose subnets marked for gateways
for_each = var.create_subnets == false ? { for subnet in var.existing_subnets : subnet.id => subnet if subnet.public_gateway } : {}
subnet = each.key
# find gateway detail using format of 'zone-#', determine '#' by getting last character of the 'zone' value of an existing subnet
public_gateway = ibm_is_public_gateway.gateway["zone-${substr(data.ibm_is_subnet.subnet[each.key].zone, length(data.ibm_is_subnet.subnet[each.key].zone) - 1, 1)}"].id
}
##############################################################################