-
Notifications
You must be signed in to change notification settings - Fork 35
/
vpc.tf
172 lines (150 loc) · 3.44 KB
/
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Setup our aws provider
provider "aws" {
access_key = "${var.aws_access_key_id}"
secret_key = "${var.aws_secret_access_key}"
region = "${var.vpc_region}"
}
# Define a vpc
resource "aws_vpc" "vpc_name" {
cidr_block = "${var.vpc_cidr_block}"
tags {
Name = "${var.vpc_name}"
}
}
# Internet gateway for the public subnet
resource "aws_internet_gateway" "demo_ig" {
vpc_id = "${aws_vpc.vpc_name.id}"
tags {
Name = "demo_ig"
}
}
# Public subnet
resource "aws_subnet" "vpc_public_sn" {
vpc_id = "${aws_vpc.vpc_name.id}"
cidr_block = "${var.vpc_public_subnet_1_cidr}"
availability_zone = "${lookup(var.availability_zone, var.vpc_region)}"
tags {
Name = "vpc_public_sn"
}
}
# Private subnet
resource "aws_subnet" "vpc_private_sn" {
vpc_id = "${aws_vpc.vpc_name.id}"
cidr_block = "${var.vpc_private_subnet_1_cidr}"
availability_zone = "us-east-1b"
tags {
Name = "vpc_private_sn"
}
}
# Routing table for public subnet
resource "aws_route_table" "vpc_public_sn_rt" {
vpc_id = "${aws_vpc.vpc_name.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.demo_ig.id}"
}
tags {
Name = "vpc_public_sn_rt"
}
}
# Associate the routing table to public subnet
resource "aws_route_table_association" "vpc_public_sn_rt_assn" {
subnet_id = "${aws_subnet.vpc_public_sn.id}"
route_table_id = "${aws_route_table.vpc_public_sn_rt.id}"
}
# ECS Instance Security group
resource "aws_security_group" "vpc_public_sg" {
name = "demo_pubic_sg"
description = "demo public access security group"
vpc_id = "${aws_vpc.vpc_name.id}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [
"${var.vpc_access_from_ip_range}"]
}
ingress {
from_port = 0
to_port = 0
protocol = "tcp"
cidr_blocks = [
"${var.vpc_public_subnet_1_cidr}"]
}
egress {
# allow all traffic to private SN
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = [
"0.0.0.0/0"]
}
tags {
Name = "demo_pubic_sg"
}
}
resource "aws_security_group" "vpc_private_sg" {
name = "demo_private_sg"
description = "demo security group to access private ports"
vpc_id = "${aws_vpc.vpc_name.id}"
# allow memcached port within VPC
ingress {
from_port = 11211
to_port = 11211
protocol = "tcp"
cidr_blocks = [
"${var.vpc_public_subnet_1_cidr}"]
}
# allow redis port within VPC
ingress {
from_port = 6379
to_port = 6379
protocol = "tcp"
cidr_blocks = [
"${var.vpc_public_subnet_1_cidr}"]
}
# allow postgres port within VPC
ingress {
from_port = 5432
to_port = 5432
protocol = "tcp"
cidr_blocks = [
"${var.vpc_public_subnet_1_cidr}"]
}
# allow mysql port within VPC
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = [
"${var.vpc_public_subnet_1_cidr}"]
}
egress {
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = [
"0.0.0.0/0"]
}
tags {
Name = "demo_private_sg"
}
}
output "vpc_region" {
value = "${var.vpc_region}"
}
output "vpc_id" {
value = "${aws_vpc.vpc_name.id}"
}
output "vpc_public_sn_id" {
value = "${aws_subnet.vpc_public_sn.id}"
}
output "vpc_private_sn_id" {
value = "${aws_subnet.vpc_private_sn.id}"
}
output "vpc_public_sg_id" {
value = "${aws_security_group.vpc_public_sg.id}"
}
output "vpc_private_sg_id" {
value = "${aws_security_group.vpc_private_sg.id}"
}