forked from hectcastro/terraform-aws-vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-vpc.tf
155 lines (121 loc) · 3.37 KB
/
aws-vpc.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
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "us-east-1"
}
resource "aws_vpc" "default" {
cidr_block = "10.0.0.0/16"
}
resource "aws_internet_gateway" "default" {
vpc_id = "${aws_vpc.default.id}"
}
# NAT instance
resource "aws_security_group" "nat" {
name = "nat"
description = "Allow services from the private subnet through NAT"
ingress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["${aws_subnet.us-east-1b-private.cidr_block}"]
}
ingress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["${aws_subnet.us-east-1d-private.cidr_block}"]
}
vpc_id = "${aws_vpc.default.id}"
}
resource "aws_instance" "nat" {
ami = "${var.aws_nat_ami}"
availability_zone = "us-east-1b"
instance_type = "m1.small"
key_name = "${var.aws_key_name}"
security_groups = ["${aws_security_group.nat.id}"]
subnet_id = "${aws_subnet.us-east-1b-public.id}"
associate_public_ip_address = true
source_dest_check = false
}
resource "aws_eip" "nat" {
instance = "${aws_instance.nat.id}"
vpc = true
}
# Public subnets
resource "aws_subnet" "us-east-1b-public" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "10.0.0.0/24"
availability_zone = "us-east-1b"
}
resource "aws_subnet" "us-east-1d-public" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "10.0.2.0/24"
availability_zone = "us-east-1d"
}
# Routing table for public subnets
resource "aws_route_table" "us-east-1-public" {
vpc_id = "${aws_vpc.default.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.default.id}"
}
}
resource "aws_route_table_association" "us-east-1b-public" {
subnet_id = "${aws_subnet.us-east-1b-public.id}"
route_table_id = "${aws_route_table.us-east-1-public.id}"
}
resource "aws_route_table_association" "us-east-1d-public" {
subnet_id = "${aws_subnet.us-east-1d-public.id}"
route_table_id = "${aws_route_table.us-east-1-public.id}"
}
# Private subsets
resource "aws_subnet" "us-east-1b-private" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1b"
}
resource "aws_subnet" "us-east-1d-private" {
vpc_id = "${aws_vpc.default.id}"
cidr_block = "10.0.3.0/24"
availability_zone = "us-east-1d"
}
# Routing table for private subnets
resource "aws_route_table" "us-east-1-private" {
vpc_id = "${aws_vpc.default.id}"
route {
cidr_block = "0.0.0.0/0"
instance_id = "${aws_instance.nat.id}"
}
}
resource "aws_route_table_association" "us-east-1b-private" {
subnet_id = "${aws_subnet.us-east-1b-private.id}"
route_table_id = "${aws_route_table.us-east-1-private.id}"
}
resource "aws_route_table_association" "us-east-1d-private" {
subnet_id = "${aws_subnet.us-east-1d-private.id}"
route_table_id = "${aws_route_table.us-east-1-private.id}"
}
# Bastion
resource "aws_security_group" "bastion" {
name = "bastion"
description = "Allow SSH traffic from the internet"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
vpc_id = "${aws_vpc.default.id}"
}
resource "aws_instance" "bastion" {
ami = "${var.aws_ubuntu_ami}"
availability_zone = "us-east-1b"
instance_type = "t2.micro"
key_name = "${var.aws_key_name}"
security_groups = ["${aws_security_group.bastion.id}"]
subnet_id = "${aws_subnet.us-east-1b-public.id}"
}
resource "aws_eip" "bastion" {
instance = "${aws_instance.bastion.id}"
vpc = true
}