-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.tf
198 lines (172 loc) · 5.03 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
locals {
templates_path = format("%s/%s", "/var/tmp/scripts", "openvpn")
templates = {
vpn = {
install = {
template = format("%s/%s", path.module, "scripts/openvpn/install.tpl.sh")
file = format("%s/%s", local.templates_path, "/install.sh")
vars = {
public_ip = aws_eip.this.public_ip
client = var.admin_user
}
}
update_user = {
template = format("%s/%s", path.module, "scripts/openvpn/update_user.tpl.sh")
file = format("%s/%s", local.templates_path, "/update_user.sh")
vars = {
client = var.admin_user
}
}
}
}
defaults = {
rules_ingress = [
{
type = "ingress"
from_port = var.ssh_port
to_port = var.ssh_port
protocol = "tcp"
cidr_blocks = [var.ssh_cidr]
},
{
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
},
{
type = "ingress"
from_port = 943
to_port = 943
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
},
{
type = "ingress"
from_port = 1194
to_port = 1194
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
]
}
input = {
rules_ingress = try(var.rules_ingress, [])
}
generated = {
rules_ingress = concat(local.defaults.rules_ingress, local.input.rules_ingress)
}
outputs = {
rules_ingress = local.generated.rules_ingress
}
}
module "tags" {
source = "hadenlabs/tags/null"
version = ">0.1"
namespace = var.namespace
environment = var.environment
stage = var.stage
name = var.name
tags = var.tags
}
resource "aws_vpc" "this" {
cidr_block = var.vpc_cidr_block
tags = module.tags.tags
}
resource "aws_subnet" "this" {
#checkov:skip=CKV_AWS_130:Public IP required in public subnet
vpc_id = aws_vpc.this.id
map_public_ip_on_launch = true
cidr_block = var.subnet_cidr_block
}
resource "aws_internet_gateway" "this" {
vpc_id = aws_vpc.this.id
tags = module.tags.tags
}
resource "aws_route" "this" {
route_table_id = aws_vpc.this.main_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.this.id
}
resource "aws_key_pair" "this" {
key_name = module.tags.name
public_key = file(var.public_key)
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group" "this" {
name = module.tags.name
description = "Allow traffic needed by openvpn"
vpc_id = aws_vpc.this.id
tags = module.tags.tags
lifecycle {
create_before_destroy = true
}
}
#AWS security group rule ingress
resource "aws_security_group_rule" "ingress" {
depends_on = [
aws_security_group.this,
]
count = length(local.outputs.rules_ingress)
description = format("Ingress %s", module.tags.name)
security_group_id = aws_security_group.this.id
type = try(lookup(local.outputs.rules_ingress[count.index], "type"), "ingress")
from_port = lookup(local.outputs.rules_ingress[count.index], "from_port")
to_port = lookup(local.outputs.rules_ingress[count.index], "to_port")
protocol = lookup(local.outputs.rules_ingress[count.index], "protocol")
cidr_blocks = lookup(local.outputs.rules_ingress[count.index], "cidr_blocks")
}
#AWS security group rule egress
resource "aws_security_group_rule" "egress" {
depends_on = [
aws_security_group.this,
]
for_each = {
"openvpn" = aws_security_group.this.id,
}
description = format("Egress %s: %s", module.tags.name, each.key)
security_group_id = each.value
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] #tfsec:ignore:AWS007
}
resource "aws_instance" "this" {
#checkov:skip=CKV_AWS_88: The instance is necessary have ip public.
depends_on = [
aws_security_group_rule.ingress,
aws_security_group_rule.egress,
aws_security_group.this,
]
tags = module.tags.tags
lifecycle {
ignore_changes = [ami]
}
ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type
key_name = aws_key_pair.this.key_name
subnet_id = aws_subnet.this.id
associate_public_ip_address = true #tfsec:ignore:aws-autoscaling-no-public-ip
vpc_security_group_ids = [aws_security_group.this.id]
monitoring = true
root_block_device {
encrypted = true
volume_type = "gp2"
volume_size = "8"
delete_on_termination = true
}
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
}
}
resource "aws_eip" "this" {
instance = aws_instance.this.id
vpc = true
depends_on = [aws_internet_gateway.this]
tags = module.tags.tags
}