-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
127 lines (112 loc) · 3.47 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
locals {
id = "${replace(var.name, " ", "-")}"
}
# -----------------------------------------------
# Create Public Routing
# -----------------------------------------------
resource "aws_route_table" "this" {
vpc_id = "${var.vpc_id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${var.vpc_ig}"
}
tags = "${merge(var.tags, map("Name", "${var.name}"))}"
}
# -----------------------------------------------
# NAT EIP
# -----------------------------------------------
resource "aws_eip" "this" {
vpc = true
tags = "${merge(var.tags, map("Name", "${var.name}"))}"
lifecycle {
prevent_destroy = true
}
}
# -----------------------------------------------
# Create NAT subnet
# -----------------------------------------------
resource "aws_subnet" "this" {
count = "1"
vpc_id = "${var.vpc_id}"
cidr_block = "${cidrsubnet(var.vpc_cidr, var.newbits, var.netnum + count.index)}"
availability_zone = "${element(var.azs, count.index)}"
map_public_ip_on_launch = true
tags = "${merge(var.tags, map("Name", "${var.name} ${var.env} ${count.index}"))}"
}
# ---------------------------------------------
# NAT
# ---------------------------------------------
resource "aws_nat_gateway" "this" {
allocation_id = "${aws_eip.this.id}"
subnet_id = "${aws_subnet.this.0.id}"
tags = {
Name = "NAT GEB"
}
}
# ---------------------------------------------
# Associate
# ---------------------------------------------
resource "aws_route_table_association" "this" {
subnet_id = "${aws_subnet.this.0.id}"
route_table_id = "${aws_route_table.this.id}"
}
# Network ACL Web Access
resource "aws_network_acl" "this" {
vpc_id = "${var.vpc_id}"
subnet_ids = ["${aws_subnet.this.0.id}"]
tags = "${merge(var.tags, map("Name", "${var.name} ${var.env} ACL"),map("Description", "Use a Custom ACL to avoid adding the NAT Subnet to the Default ACL."))}"
}
resource "aws_network_acl_rule" "egress" {
network_acl_id = "${aws_network_acl.this.id}"
rule_number = 100
egress = true
protocol = -1
rule_action = "allow"
cidr_block = "0.0.0.0/0"
from_port = -1
to_port = -1
}
resource "aws_network_acl_rule" "ingress_ephemeral" {
count = "${length(var.ephemeral_open)}"
network_acl_id = "${aws_network_acl.this.id}"
rule_number = "${100 + count.index}"
egress = false
protocol = "tcp"
rule_action = "allow"
cidr_block = "${element(var.ephemeral_open, count.index)}"
from_port = 1024
to_port = 65535
}
resource "aws_network_acl_rule" "ingress_ssh" {
count = "${length(var.ssh_open)}"
network_acl_id = "${aws_network_acl.this.id}"
rule_number = "${200 + count.index}"
egress = false
protocol = "tcp"
rule_action = "allow"
cidr_block = "${element(var.ssh_open, count.index)}"
from_port = 22
to_port = 22
}
resource "aws_network_acl_rule" "ingress_http" {
count = "${length(var.http_open)}"
network_acl_id = "${aws_network_acl.this.id}"
rule_number = "${300 + count.index}"
egress = false
protocol = "tcp"
rule_action = "allow"
cidr_block = "${element(var.http_open, count.index)}"
from_port = 80
to_port = 80
}
resource "aws_network_acl_rule" "ingress_https" {
count = "${length(var.https_open)}"
network_acl_id = "${aws_network_acl.this.id}"
rule_number = "${400 + count.index}"
egress = false
protocol = "tcp"
rule_action = "allow"
cidr_block = "${element(var.https_open, count.index)}"
from_port = 443
to_port = 443
}