-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.tf
142 lines (113 loc) · 3.39 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
resource "aws_vpc" "environment" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = var.enable_dns_hostnames
enable_dns_support = var.enable_dns_support
tags = {
Name = var.environment
}
}
resource "aws_internet_gateway" "environment" {
vpc_id = aws_vpc.environment.id
tags = {
Name = "${var.environment}-igw"
}
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.environment.id
tags = {
Name = "${var.environment}-public"
}
}
resource "aws_route_table" "private" {
count = length(var.private_subnets)
vpc_id = aws_vpc.environment.id
tags = {
Name = "${var.environment}-private"
}
}
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.environment.id
}
resource "aws_route" "private_nat_gateway" {
count = length(var.private_subnets)
route_table_id = aws_route_table.private[count.index].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.environment[count.index].id
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.environment.id
cidr_block = var.public_subnets[count.index]
map_public_ip_on_launch = var.map_public_ip_on_launch
tags = {
Name = "${var.environment}-public-${count.index}"
}
count = length(var.public_subnets)
}
resource "aws_subnet" "private" {
vpc_id = aws_vpc.environment.id
cidr_block = var.private_subnets[count.index]
map_public_ip_on_launch = "false"
tags = {
Name = "${var.environment}-private-${count.index}"
}
count = length(var.private_subnets)
}
resource "aws_route_table_association" "private" {
count = length(var.private_subnets)
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_subnets)
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
resource "aws_eip" "environment" {
count = length(var.public_subnets)
vpc = true
}
resource "aws_nat_gateway" "environment" {
count = length(var.public_subnets)
allocation_id = aws_eip.environment[count.index].id
subnet_id = aws_subnet.public[count.index].id
}
resource "aws_security_group" "bastion" {
vpc_id = aws_vpc.environment.id
name = "${var.environment}-bastion-host"
description = "Allow SSH to bastion host"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8
to_port = 0
protocol = "icmp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.environment}-bastion-sg"
}
}
resource "aws_instance" "bastion" {
ami = var.bastion_ami[var.region]
instance_type = var.bastion_instance_type
key_name = var.key_name
monitoring = true
vpc_security_group_ids = [aws_security_group.bastion.id]
subnet_id = aws_subnet.public[0].id
associate_public_ip_address = true
tags = {
Name = "${var.environment}-bastion"
}
}