-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvpc.tf
45 lines (38 loc) · 1.44 KB
/
vpc.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
locals {
// Logic for AZs is azs variable > az_num variable > max azs for region
az_num = chunklist(data.alicloud_zones.az.ids, var.num_azs)[0]
az_max = data.alicloud_zones.az.ids
azs = coalescelist(var.azs, local.az_num, local.az_max)
num_azs = length(local.azs)
// TODO: If making additional subnets, this will change
subnet_num = 2
subnet_count = local.subnet_num * local.num_azs
subnet_bits = ceil(log(local.subnet_count, 2))
public_subnets = [for subnet_num in range(local.num_azs) : cidrsubnet(
var.cidr,
local.subnet_bits,
subnet_num)]
private_subnets = [for subnet_num in range(local.num_azs) : cidrsubnet(
var.cidr,
local.subnet_bits,
local.num_azs + subnet_num,
)]
}
resource "alicloud_vpc" "vpc" {
name = var.vpc_name
cidr_block = var.cidr
}
resource "alicloud_vswitch" "public" {
count = var.create_public_subnets ? length(local.public_subnets) : 0
vpc_id = alicloud_vpc.vpc.id
cidr_block = local.public_subnets[count.index]
availability_zone = element(local.azs, count.index)
name = "public-${count.index}"
}
resource "alicloud_vswitch" "private" {
count = var.create_private_subnets ? length(local.private_subnets) : 0
vpc_id = alicloud_vpc.vpc.id
cidr_block = local.private_subnets[count.index]
availability_zone = element(local.azs, count.index)
name = "private-${count.index}"
}