-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
140 lines (122 loc) · 4.98 KB
/
main.tf
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
##############################################################################
# Create VPC
##############################################################################
resource "ibm_is_vpc" "edge_vpc" {
count = var.vpc_id == null ? 1 : 0
name = "${var.prefix}-edge-vpc"
resource_group = var.resource_group_id
address_prefix_management = "manual"
tags = var.tags
classic_access = var.create_vpc_options.classic_access
default_network_acl_name = var.create_vpc_options.default_network_acl_name
default_security_group_name = var.create_vpc_options.default_security_group_name
default_routing_table_name = var.create_vpc_options.default_routing_table_name
}
locals {
vpc_id = var.vpc_id != null ? var.vpc_id : ibm_is_vpc.edge_vpc[0].id
}
##############################################################################
##############################################################################
# Create Public Gateways
##############################################################################
module "public_gateways" {
source = "github.com/Cloud-Schematics/vpc-public-gateway-module"
prefix = var.prefix
vpc_id = local.vpc_id
region = var.region
resource_group_id = var.resource_group_id
public_gateways = {
for zone in [1, 2, 3] :
"zone-${zone}" => true if(
var.existing_public_gateways["zone-${zone}"] == null # no gateway in that zone
&& var.create_public_gateways == true # create gateways is true
&& zone <= var.zones # zone is less than or equal to total
)
}
}
##############################################################################
##############################################################################
# Create Address Prefixes
##############################################################################
module "address_prefixes" {
source = "github.com/Cloud-Schematics/vpc-address-prefix-module"
prefix = var.prefix
region = var.region
vpc_id = local.vpc_id
address_prefixes = {
for zone in [1, 2, 3] :
"zone-${zone}" => (
# if zone is more than total number
zone > var.zones
# return empty array
? []
# otherwise return cidr
: ["10.${4 + zone}.0.0/16"]
)
}
}
##############################################################################
##############################################################################
# Create ACL
##############################################################################
module "network_acl" {
source = "github.com/Cloud-Schematics/vpc-network-acl-module"
prefix = var.prefix
tags = var.tags
vpc_id = local.vpc_id
network_acls = [local.edge_network_acl]
}
##############################################################################
##############################################################################
# Create Subnets
##############################################################################
module "subnets" {
source = "github.com/Cloud-Schematics/vpc-subnet-module"
prefix = var.prefix
region = var.region
tags = var.tags
resource_group_id = var.resource_group_id
vpc_id = local.vpc_id
use_manual_address_prefixes = true
network_acls = module.network_acl.acls
public_gateways = {
for zone in [1, 2, 3] :
"zone-${zone}" => (
var.existing_public_gateways["zone-${zone}"] == null
? module.public_gateways.gateways["zone-${zone}"]
: var.existing_public_gateways["zone-${zone}"]
)
}
subnets = {
for zone in [1, 2, 3] :
"zone-${zone}" => (
zone > var.zones
? []
: [
for tier in local.create_tier_list :
{
name = "edge-${tier}-zone-${zone}"
cidr = local.subnet_tiers["zone-${zone}"][tier]
acl_name = "edge-acl"
public_gateway = (
tier != "bastion" && tier != "f5-external"
? false
: true
)
} if tier != "bastion" || (tier == "bastion" && zone <= var.bastion_subnet_zones)
]
)
}
depends_on = [module.address_prefixes] # Force dependecy on address prefixes to prevent creation errors
}
##############################################################################
##############################################################################
# Get subnets for each interface
##############################################################################
module "subnets_by_tier" {
source = "github.com/Cloud-Schematics/get-subnets"
for_each = toset(local.all_subnet_tiers)
subnet_zone_list = module.subnets.subnet_zone_list
regex = each.key == "bastion" ? "edge-bastion" : each.key
}
##############################################################################