-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule.tf
103 lines (82 loc) · 3.17 KB
/
module.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
data "aws_availability_zones" "available" {}
resource "aws_vpc" "vpc" {
cidr_block = "10.220.0.0/16"
tags = {
Name = "${var.vpc_name}"
}
}
resource "aws_internet_gateway" "gateway" {
vpc_id = "${aws_vpc.vpc.id}"
}
resource "aws_route_table" "route_table" {
vpc_id = "${aws_vpc.vpc.id}"
count = "${length(data.aws_availability_zones.available.names)}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gateway.id}"
}
}
resource "aws_route_table_association" "route_association" {
subnet_id = "${element(aws_subnet.subnet.*.id, count.index)}"
route_table_id = "${element(aws_route_table.route_table.*.id, count.index)}"
count = "${length(data.aws_availability_zones.available.names)}"
}
// With a non-default VPC we have to create a subnet also
resource "aws_subnet" "subnet" {
vpc_id = "${aws_vpc.vpc.id}"
availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
cidr_block = "${cidrsubnet(aws_vpc.vpc.cidr_block, 8, count.index)}"
map_public_ip_on_launch = true
tags {
Name = "subnet-${data.aws_availability_zones.available.names[count.index]}"
}
count = "${length(data.aws_availability_zones.available.names)}"
}
# nat shit
resource "aws_eip" "nat" {
count = "${var.support_nat ? length(data.aws_availability_zones.available.names) : 0}"
vpc = true
}
resource "aws_nat_gateway" "gw" {
count = "${var.support_nat ? length(data.aws_availability_zones.available.names) : 0}"
allocation_id = "${aws_eip.nat.*.id[count.index]}"
subnet_id = "${aws_subnet.subnet.*.id[count.index]}"
}
resource "aws_route_table" "nat_route_table" {
count = "${var.support_nat ? length(data.aws_availability_zones.available.names) : 0}"
vpc_id = "${aws_vpc.vpc.id}"
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = "${aws_nat_gateway.gw.*.id[count.index]}"
}
}
resource "aws_route_table_association" "nat_route_association" {
count = "${var.support_nat ? length(data.aws_availability_zones.available.names) : 0}"
subnet_id = "${element(aws_subnet.subnet-point-to-nat.*.id, count.index)}"
route_table_id = "${element(aws_route_table.nat_route_table.*.id, count.index)}"
}
resource "aws_subnet" "subnet-point-to-nat" {
count = "${var.support_nat ? length(data.aws_availability_zones.available.names) : 0}"
vpc_id = "${aws_vpc.vpc.id}"
availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
cidr_block = "${cidrsubnet(aws_vpc.vpc.cidr_block, 8, length(data.aws_availability_zones.available.names) + count.index)}"
map_public_ip_on_launch = true
tags {
Name = "subnet-point-to-nat-${data.aws_availability_zones.available.names[count.index]}"
}
}
output "vpc_nat_subnet_ids" {
value = "${jsonencode(aws_subnet.subnet-point-to-nat.*.id)}"
}
output "vpc_nat_subnet_ids_list" {
value = ["${aws_subnet.subnet-point-to-nat.*.id}"]
}
output "vpc_id" {
value = "${aws_vpc.vpc.id}"
}
output "vpc_subnet_ids_list" {
value = ["${aws_subnet.subnet.*.id}"]
}
output "vpc_subnet_cidr_blocks_list" {
value = ["${aws_subnet.subnet.*.cidr_block}"]
}