forked from terraform-community-modules/tf_aws_vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
102 lines (86 loc) · 4.17 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
resource "aws_vpc" "mod" {
cidr_block = "${var.cidr}"
enable_dns_hostnames = "${var.enable_dns_hostnames}"
enable_dns_support = "${var.enable_dns_support}"
tags = "${merge(var.tags, map("Name", format("%s", var.name)))}"
}
resource "aws_internet_gateway" "mod" {
vpc_id = "${aws_vpc.mod.id}"
tags = "${merge(var.tags, map("Name", format("%s-igw", var.name)))}"
}
resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.mod.id}"
propagating_vgws = ["${var.public_propagating_vgws}"]
tags = "${merge(var.tags, map("Name", format("%s-rt-public", var.name)))}"
}
resource "aws_route" "public_internet_gateway" {
route_table_id = "${aws_route_table.public.id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.mod.id}"
}
resource "aws_route" "private_nat_gateway" {
route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = "${element(aws_nat_gateway.natgw.*.id, count.index)}"
count = "${length(var.private_subnets) * lookup(map(var.enable_nat_gateway, 1), "true", 0)}"
}
resource "aws_route_table" "private" {
vpc_id = "${aws_vpc.mod.id}"
propagating_vgws = ["${var.private_propagating_vgws}"]
count = "${length(var.private_subnets)}"
tags = "${merge(var.tags, map("Name", format("%s-rt-private-%s", var.name, element(var.azs, count.index))))}"
}
resource "aws_subnet" "private" {
vpc_id = "${aws_vpc.mod.id}"
cidr_block = "${var.private_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"
count = "${length(var.private_subnets)}"
tags = "${merge(var.tags, map("Name", format("%s-subnet-private-%s", var.name, element(var.azs, count.index))))}"
}
resource "aws_subnet" "database" {
vpc_id = "${aws_vpc.mod.id}"
cidr_block = "${var.database_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"
count = "${length(var.database_subnets)}"
tags = "${merge(var.tags, map("Name", format("%s-database-subnet-%s", var.name, element(var.azs, count.index))))}"
}
resource "aws_db_subnet_group" "database" {
name = "${var.name}-rds-subnet-group"
description = "Database subnet groups for ${var.name}"
subnet_ids = ["${aws_subnet.database.*.id}"]
tags = "${merge(var.tags, map("Name", format("%s-database-subnet-group", var.name)))}"
count = "${length(var.database_subnets) > 0 ? 1 : 0}"
}
resource "aws_subnet" "public" {
vpc_id = "${aws_vpc.mod.id}"
cidr_block = "${var.public_subnets[count.index]}"
availability_zone = "${element(var.azs, count.index)}"
count = "${length(var.public_subnets)}"
tags = "${merge(var.tags, map("Name", format("%s-subnet-public-%s", var.name, element(var.azs, count.index))))}"
map_public_ip_on_launch = "${var.map_public_ip_on_launch}"
}
resource "aws_eip" "nateip" {
vpc = true
count = "${length(var.private_subnets) * lookup(map(var.enable_nat_gateway, 1), "true", 0)}"
}
resource "aws_nat_gateway" "natgw" {
allocation_id = "${element(aws_eip.nateip.*.id, count.index)}"
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
count = "${length(var.private_subnets) * lookup(map(var.enable_nat_gateway, 1), "true", 0)}"
depends_on = ["aws_internet_gateway.mod"]
}
resource "aws_route_table_association" "private" {
count = "${length(var.private_subnets)}"
subnet_id = "${element(aws_subnet.private.*.id, count.index)}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
}
resource "aws_route_table_association" "database" {
count = "${length(var.database_subnets)}"
subnet_id = "${element(aws_subnet.database.*.id, count.index)}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
}
resource "aws_route_table_association" "public" {
count = "${length(var.public_subnets)}"
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
route_table_id = "${aws_route_table.public.id}"
}