-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
136 lines (116 loc) · 3.41 KB
/
main.tf
File metadata and controls
136 lines (116 loc) · 3.41 KB
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
133
134
135
#
# Defines VPC, subnets, security groups, NAT instance, etc.
#
# VPC
resource "aws_vpc" "default" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
tags = {
Environment = var.env
Name = "spire-vpc-${var.env}"
Provisoning = "terraform"
}
}
## Public subnets
resource "aws_subnet" "public" {
vpc_id = aws_vpc.default.id
cidr_block = var.public_subnet_cidr_blocks["zone${count.index}"]
availability_zone = "${var.region}${element(var.zones, count.index)}"
count = length(var.zones)
tags = {
Environment = var.env
Name = "spire-subnet-${var.env}-public-${count.index}"
Type = "spire-subnet-${var.env}-public"
Provisoning = "terraform"
}
}
### Custom route table for public subnets
resource "aws_route_table" "custom" {
vpc_id = aws_vpc.default.id
tags = {
Environment = var.env
Name = "spire-${var.env}-public-rt"
Provisoning = "terraform"
}
}
#### Route for customer route table
resource "aws_route" "custom" {
route_table_id = aws_route_table.custom.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.default.id
depends_on = [aws_route_table.custom]
}
### NAT GW eip
resource "aws_eip" "natgw" {
vpc = true
tags = {
Name = "spire-vpc-igw"
Environment = var.env
Provisoning = "terraform"
}
}
### Internet Gateway
resource "aws_internet_gateway" "default" {
vpc_id = aws_vpc.default.id
tags = {
Name = "spire-nat-sg"
Environment = var.env
Provisoning = "terraform"
}
}
### NAT Gateway
resource "aws_nat_gateway" "natgw" {
allocation_id = aws_eip.natgw.id
subnet_id = element(aws_subnet.public.*.id, 0)
tags = {
Environment = var.env
Name = "natgw-${var.env}"
Provisoning = "terraform"
}
}
#### Associate custom route table with public subnets
resource "aws_route_table_association" "public" {
subnet_id = element(aws_subnet.public.*.id, count.index)
route_table_id = aws_route_table.custom.id
count = length(var.zones)
}
## Private subnets
resource "aws_subnet" "private" {
vpc_id = aws_vpc.default.id
cidr_block = var.private_subnet_cidr_blocks["zone${count.index}"]
availability_zone = "${var.region}${element(var.zones, count.index)}"
count = length(var.zones)
tags = {
Environment = var.env
Name = "spire-subnet-${var.env}-private-${count.index}"
Type = "spire-subnet-${var.env}-private"
Provisoning = "terraform"
}
}
### Main route table
resource "aws_route_table" "main" {
vpc_id = aws_vpc.default.id
tags = {
Environment = var.env
Name = "spire-${var.env}-private-rt"
Provisoning = "terraform"
}
}
### Update main route table to use NAT
resource "aws_route" "main" {
route_table_id = aws_route_table.main.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.natgw.id
depends_on = [aws_nat_gateway.natgw]
}
### Associate main route table with private subnets
resource "aws_route_table_association" "private" {
subnet_id = element(aws_subnet.private.*.id, count.index)
route_table_id = aws_route_table.main.id
count = length(var.zones)
}
### Set main route table
resource "aws_main_route_table_association" "main" {
vpc_id = aws_vpc.default.id
route_table_id = aws_route_table.main.id
}