-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
132 lines (92 loc) · 2.89 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
# VPC
resource "aws_vpc" "vpc" {
cidr_block = var.cidr_block
instance_tenancy = var.instance_tenancy
enable_dns_hostnames = var.enable_dns_hostnames
enable_dns_support = var.enable_dns_support
assign_generated_ipv6_cidr_block = var.assign_generated_ipv6_cidr_block
tags = {
Name = var.name
}
}
resource "aws_eip" "eip" {
count = var.nat_gateway && length(var.azs) > 0 ? length(var.azs) : 0
vpc = true
lifecycle {
create_before_destroy = true
}
}
resource "aws_nat_gateway" "nat_gateway" {
count = var.nat_gateway && length(var.azs) > 0 ? length(var.azs) : 0
# count = length(var.azs) && var.nat_gateway > 0 ? : 0
allocation_id = element(aws_eip.eip.*.id, count.index)
subnet_id = element(aws_subnet.public_subnet.*.id, count.index)
lifecycle {
create_before_destroy = true
}
}
# Public subnets
resource "aws_subnet" "public_subnet" {
count = length(var.public_subnets)
vpc_id = aws_vpc.vpc.id
cidr_block = element(var.public_subnets, count.index)
availability_zone = element(var.azs, count.index)
map_public_ip_on_launch = true
lifecycle {
create_before_destroy = true
}
}
# Private subnets
resource "aws_subnet" "private_subnet" {
count = length(var.private_subnets)
# vpc_id = aws_vpc.vpc.*.id
vpc_id = aws_vpc.vpc.id
cidr_block = element(var.private_subnets, count.index)
availability_zone = element(var.azs, count.index)
# tags {
# Name = var.name.element(var.azs, count.index).element(var.private_subnet_tags, count.index)
# }
lifecycle {
create_before_destroy = true
}
}
# Private routes
resource "aws_route_table" "private_route_table" {
vpc_id = aws_vpc.vpc.id
count = length(var.private_subnets)
route {
cidr_block = "0.0.0.0/0"
# nat_gateway_id = aws_nat_gateway.nat.*.id[count.index]
nat_gateway_id = element(aws_nat_gateway.nat_gateway.*.id, count.index)
}
# tags {
# Name = var.name.element(var.azs, count.index)
# }
lifecycle {
create_before_destroy = true
}
}
resource "aws_internet_gateway" "internet_gateway" {
count = var.internet_gateway && length(var.public_subnets) > 0 ? 1 : 0
vpc_id = aws_vpc.vpc.id
tags = merge(
{
"Name" = format("%s", var.name)
},
var.tags
)
}
resource "aws_route_table" "public_route_table" {
count = length(var.public_subnets)
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = element(aws_internet_gateway.internet_gateway.*.id, count.index)
}
# tags { Name = var.name}.${element(split(",", var.azs), count.index) }
}
resource "aws_route_table_association" "public" {
count = length(var.public_subnets)
subnet_id = element(aws_subnet.public_subnet.*.id, count.index)
route_table_id = element(aws_route_table.public_route_table.*.id, count.index)
}