-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprivate_subnets.tf
74 lines (60 loc) · 2.61 KB
/
private_subnets.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
# Private Subnets Tier
resource "aws_subnet" "private_subnets" {
/*
Creates a dynamic number of subnets, 1 per AZ, with a max being equal to
the num of AZ available in the set region
*/
count = "${(var.max_zones > length(data.aws_availability_zones.available.names) ?
length(data.aws_availability_zones.available.names):var.max_zones)}"
vpc_id = "${aws_vpc.the_vpc.id}"
cidr_block = "${cidrsubnet(var.vpc_cidr, 8, (count.index + 11))}"
availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
map_public_ip_on_launch = false
tags {
Name = "${var.vpc_name}-private-${count.index + 1}"
Environment = "${var.environment}"
}
}
# Private Route Table
resource "aws_route_table" "private_subnets" {
count = "${(var.max_zones > length(data.aws_availability_zones.available.names) ?
length(data.aws_availability_zones.available.names):var.max_zones)}"
vpc_id = "${aws_vpc.the_vpc.id}"
tags {
Name = "Private Subnet ${count.index + 1} Route"
Environment = "${var.environment}"
}
}
# Private Routes
resource "aws_route" "private_subnets" {
count = "${(var.max_zones > length(data.aws_availability_zones.available.names) ?
length(data.aws_availability_zones.available.names):var.max_zones)}"
route_table_id = "${element(aws_route_table.private_subnets.*.id, count.index)}"
destination_cidr_block = "${var.public_route_cidr}"
nat_gateway_id = "${element(aws_nat_gateway.nat_gw.*.id, count.index)}"
}
# Private Route Table Associations
resource "aws_route_table_association" "private_subnets" {
count = "${(var.max_zones > length(data.aws_availability_zones.available.names) ?
length(data.aws_availability_zones.available.names):var.max_zones)}"
route_table_id = "${element(aws_route_table.private_subnets.*.id, count.index)}"
subnet_id = "${element(aws_subnet.private_subnets.*.id, count.index)}"
}
# RDS Subnet Groups
resource "aws_db_subnet_group" "main-db-subnet-group" {
count = "${var.create_rds_subnet_group ? 1 : 0}"
name = "rds-subnet-group"
description = "RDS Subnet Group"
subnet_ids = ["${aws_subnet.private_subnets.*.id}"]
tags {
Name = "RDS Subnet Group"
Environment = "${var.environment}"
}
}
# Elasticache Subnet Groups
resource "aws_elasticache_subnet_group" "main-ec-subnet-group" {
count = "${var.create_elasticache_subnet_group ? 1 : 0}"
name = "elasticache-subnet-group"
description = "Elasticache Subnet Group"
subnet_ids = ["${aws_subnet.private_subnets.*.id}"]
}