-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
211 lines (178 loc) · 4.67 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#
# VPC resources
#
resource "aws_vpc" "default" {
cidr_block = var.cidr_block
enable_dns_support = true
enable_dns_hostnames = true
tags = merge(
{
Name = var.name,
Project = var.project,
Environment = var.environment
},
var.tags
)
}
resource "aws_internet_gateway" "default" {
vpc_id = aws_vpc.default.id
tags = merge(
{
Name = "gwInternet",
Project = var.project,
Environment = var.environment
},
var.tags
)
}
resource "aws_route_table" "private" {
count = length(var.private_subnet_cidr_blocks)
vpc_id = aws_vpc.default.id
tags = merge(
{
Name = "PrivateRouteTable",
Project = var.project,
Environment = var.environment
},
var.tags
)
}
resource "aws_route" "private" {
count = length(var.private_subnet_cidr_blocks)
route_table_id = aws_route_table.private[count.index].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.default[count.index].id
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.default.id
tags = merge(
{
Name = "PublicRouteTable",
Project = var.project,
Environment = var.environment
},
var.tags
)
}
resource "aws_route" "public" {
route_table_id = aws_route_table.public.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.default.id
}
resource "aws_subnet" "private" {
count = length(var.private_subnet_cidr_blocks)
vpc_id = aws_vpc.default.id
cidr_block = var.private_subnet_cidr_blocks[count.index]
availability_zone = var.availability_zones[count.index]
tags = merge(
{
Name = "PrivateSubnet",
Project = var.project,
Environment = var.environment
},
var.tags
)
}
resource "aws_subnet" "public" {
count = length(var.public_subnet_cidr_blocks)
vpc_id = aws_vpc.default.id
cidr_block = var.public_subnet_cidr_blocks[count.index]
availability_zone = var.availability_zones[count.index]
map_public_ip_on_launch = true
tags = merge(
{
Name = "PublicSubnet",
Project = var.project,
Environment = var.environment
},
var.tags
)
}
resource "aws_route_table_association" "private" {
count = length(var.private_subnet_cidr_blocks)
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private[count.index].id
}
resource "aws_route_table_association" "public" {
count = length(var.public_subnet_cidr_blocks)
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
resource "aws_vpc_endpoint" "s3" {
vpc_id = aws_vpc.default.id
service_name = "com.amazonaws.${var.region}.s3"
route_table_ids = flatten([
aws_route_table.public.id,
aws_route_table.private.*.id
])
tags = merge(
{
Name = "endpointS3",
Project = var.project,
Environment = var.environment
},
var.tags
)
}
#
# NAT resources
#
resource "aws_eip" "nat" {
count = length(var.public_subnet_cidr_blocks)
}
resource "aws_nat_gateway" "default" {
depends_on = [aws_internet_gateway.default]
count = length(var.public_subnet_cidr_blocks)
allocation_id = aws_eip.nat[count.index].id
subnet_id = aws_subnet.public[count.index].id
tags = merge(
{
Name = "gwNAT",
Project = var.project,
Environment = var.environment
},
var.tags
)
}
#
# Bastion resources
#
resource "aws_security_group" "bastion" {
vpc_id = aws_vpc.default.id
tags = merge(
{
Name = "sgBastion",
Project = var.project,
Environment = var.environment
},
var.tags
)
}
resource "aws_network_interface_sg_attachment" "bastion" {
security_group_id = aws_security_group.bastion.id
network_interface_id = aws_instance.bastion.primary_network_interface_id
}
resource "aws_instance" "bastion" {
ami = var.bastion_ami
availability_zone = var.availability_zones[0]
ebs_optimized = var.bastion_ebs_optimized
instance_type = var.bastion_instance_type
key_name = var.key_name
monitoring = true
subnet_id = aws_subnet.public[0].id
associate_public_ip_address = true
iam_instance_profile = var.bastion_iam_instance_profile
user_data = <<EOF
#!/bin/bash
echo "Install EFS helper"
yum install -y amazon-efs-utils
EOF
tags = merge(
{
Name = "Bastion",
Project = var.project,
Environment = var.environment
},
var.tags
)
}