-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws-vpc.tf
120 lines (91 loc) · 2.64 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
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "us-east-1"
}
resource "aws_vpc" "nat-vpc" {
cidr_block = "10.0.0.0/16"
}
resource "aws_internet_gateway" "nat-gateway" {
vpc_id = "${aws_vpc.nat-vpc.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}"]
}
vpc_id = "${aws_vpc.nat-vpc.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.nat-vpc.id}"
cidr_block = "10.0.0.0/24"
availability_zone = "us-east-1b"
}
resource "aws_subnet" "us-east-1c-public" {
vpc_id = "${aws_vpc.nat-vpc.id}"
cidr_block = "10.0.2.0/24"
availability_zone = "us-east-1c"
}
# Routing table for public subnets
resource "aws_route_table" "us-east-1-public" {
vpc_id = "${aws_vpc.nat-vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.nat-gateway.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-1c-public" {
subnet_id = "${aws_subnet.us-east-1c-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.nat-vpc.id}"
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1b"
}
resource "aws_subnet" "us-east-1c-private" {
vpc_id = "${aws_vpc.nat-vpc.id}"
cidr_block = "10.0.3.0/24"
availability_zone = "us-east-1c"
}
# Routing table for private subnets
resource "aws_route_table" "us-east-1-private" {
vpc_id = "${aws_vpc.nat-vpc.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-1c-private" {
subnet_id = "${aws_subnet.us-east-1c-private.id}"
route_table_id = "${aws_route_table.us-east-1-private.id}"
}