Skip to content

Commit 817d8f1

Browse files
committedOct 7, 2019
Initial Commit
0 parents  commit 817d8f1

8 files changed

+341
-0
lines changed
 

‎.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.tfstate
2+
*.tfstate.backup
3+
4+
.terraform/

‎.pre-commit-config.yaml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- repo: git://github.com/pre-commit/pre-commit-hooks
2+
sha: v1.2.3
3+
hooks:
4+
- id: trailing-whitespace
5+
6+
- repo: git://github.com/Lucas-C/pre-commit-hooks
7+
sha: v1.1.5
8+
hooks:
9+
- id: forbid-tabs
10+
11+
- repo: git://github.com/kintoandar/pre-commit.git
12+
sha: v2.1.0
13+
hooks:
14+
- id: terraform_fmt
15+
- id: terraform_validate

‎LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Piyush Agrawal<poush12@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

‎README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Input variables
2+
3+
- **aws_key_name:** SSH Key pair for VPN instance
4+
- **vpc_id:** The VPC id
5+
- **public_subnet_id:** One of the public subnets to create the instance
6+
- **instance_type:** Instance type of the VPN box (t2.small is mostly enough)
7+
- **whitelist:** List of office IP addresses that you can SSH and non-VPN connected users can reach temporary profile download pages
8+
- **whitelist_http:** List of IP addresses that you can allow HTTP connections.
9+
- **internal_cidrs:** List of CIDRs that will be whitelisted to access the VPN server internally.
10+
- **resource_name_prefix:** All the resources will be prefixed with the value of this variable
11+
12+
# Outputs
13+
14+
- **pritunl_private_ip:** Private IP address of the instance
15+
- **pritunl_public_ip:** EIP of the VPN box
16+
17+
# Usage
18+
19+
```
20+
provider "aws" {
21+
region = "eu-west-2"
22+
}
23+
24+
module "pritunl" {
25+
source = "github.com/poush/terraform-aws-pritunl?ref=1.0.0"
26+
27+
aws_key_name = "aws_key_name"
28+
vpc_id = "${module.vpc.vpc_id}"
29+
public_subnet_id = "${module.vpc.public_subnets[1]}"
30+
instance_type = "t2.micro"
31+
resource_name_prefix = "my-pritunl"
32+
33+
whitelist = [
34+
"<Your IP>/32",
35+
]
36+
}
37+
```
38+
39+
**Please Note that it can take few minutes (ideally 3-5 minutes) for provisioner to complete after terraform completes its process. Once completed, you should see Pritunl app on the public IP of instance**

‎main.tf

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
data "aws_region" "current" {}
2+
3+
data "aws_caller_identity" "current" {}
4+
5+
resource "aws_security_group" "pritunl" {
6+
name = "${var.resource_name_prefix}-vpn"
7+
description = "${var.resource_name_prefix}-vpn"
8+
vpc_id = "${var.vpc_id}"
9+
10+
# SSH access
11+
ingress {
12+
from_port = 22
13+
to_port = 22
14+
protocol = "tcp"
15+
cidr_blocks = var.internal_cidrs
16+
}
17+
18+
# HTTP access for Let's Encrypt validation
19+
ingress {
20+
from_port = 80
21+
to_port = 80
22+
protocol = "tcp"
23+
24+
cidr_blocks = var.whitelist_http
25+
}
26+
27+
# HTTPS access
28+
ingress {
29+
from_port = 443
30+
to_port = 443
31+
protocol = "tcp"
32+
cidr_blocks = var.internal_cidrs
33+
}
34+
35+
# VPN WAN access
36+
ingress {
37+
from_port = 10000
38+
to_port = 19999
39+
protocol = "udp"
40+
cidr_blocks = ["0.0.0.0/0"]
41+
}
42+
43+
# ICMP
44+
ingress {
45+
from_port = -1
46+
to_port = -1
47+
protocol = "icmp"
48+
cidr_blocks = var.internal_cidrs
49+
}
50+
51+
# outbound internet access
52+
egress {
53+
from_port = 0
54+
to_port = 0
55+
protocol = "-1"
56+
cidr_blocks = ["0.0.0.0/0"]
57+
}
58+
59+
tags = "${
60+
merge(
61+
map("Name", format("%s-%s", var.resource_name_prefix, "vpn")),
62+
var.tags,
63+
)
64+
}"
65+
}
66+
67+
resource "aws_security_group" "allow_from_office" {
68+
name = "${var.resource_name_prefix}-whitelist"
69+
description = "Allows SSH connections and HTTP(s) connections from office"
70+
vpc_id = "${var.vpc_id}"
71+
72+
# SSH access
73+
ingress {
74+
description = "Allow SSH access from select CIDRs"
75+
from_port = 22
76+
to_port = 22
77+
protocol = "tcp"
78+
cidr_blocks = var.whitelist
79+
}
80+
81+
# HTTPS access
82+
ingress {
83+
description = "Allow HTTPS access from select CIDRs"
84+
from_port = 443
85+
to_port = 443
86+
protocol = "tcp"
87+
cidr_blocks = var.whitelist
88+
}
89+
90+
# ICMP
91+
ingress {
92+
description = "Allow ICMPv4 from select CIDRs"
93+
from_port = -1
94+
to_port = -1
95+
protocol = "icmp"
96+
cidr_blocks = var.whitelist
97+
}
98+
99+
# outbound internet access
100+
egress {
101+
from_port = 0
102+
to_port = 0
103+
protocol = "-1"
104+
cidr_blocks = ["0.0.0.0/0"]
105+
}
106+
107+
tags = "${
108+
merge(
109+
map("Name", format("%s-%s", var.resource_name_prefix, "whitelist")),
110+
var.tags,
111+
)
112+
}"
113+
}
114+
115+
data "aws_ami" "oracle" {
116+
most_recent = true
117+
118+
filter {
119+
name = "name"
120+
values = ["OL7.6-x86_64-HVM-2019-01-29"]
121+
}
122+
123+
filter {
124+
name = "virtualization-type"
125+
values = ["hvm"]
126+
}
127+
128+
owners = ["131827586825"] # Canonical
129+
}
130+
131+
resource "aws_instance" "pritunl" {
132+
ami = "${data.aws_ami.oracle.id}"
133+
instance_type = "${var.instance_type}"
134+
key_name = "${var.aws_key_name}"
135+
user_data = "${file("${path.module}/provision.sh")}"
136+
137+
vpc_security_group_ids = [
138+
"${aws_security_group.pritunl.id}",
139+
"${aws_security_group.allow_from_office.id}",
140+
]
141+
142+
subnet_id = "${var.public_subnet_id}"
143+
144+
tags = "${
145+
merge(
146+
map("Name", format("%s-%s", var.resource_name_prefix, "vpn")),
147+
var.tags,
148+
)
149+
}"
150+
151+
}
152+
153+
# resource "aws_route53_record" "pritunl-www" {
154+
# zone_id = "${var.route53_zoneid}"
155+
# name = "${var.domain_name}"
156+
# type = "A"
157+
# ttl = "300"
158+
# records = ["${aws_instance.pritunl.public_ip}"]
159+
# }
160+
161+

‎outputs.tf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "pritunl_private_ip" {
2+
value = "${aws_instance.pritunl.private_ip}"
3+
}
4+
5+
output "pritunl_public_ip" {
6+
value = "${aws_instance.pritunl.public_ip}"
7+
}

‎provision.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash -xe
2+
# exec > >(tee /var/log/pritunl-install-data.log|logger -t user-data -s 2>/dev/console) 2>&1yes
3+
4+
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin
5+
echo "Pritunl Installing"
6+
yum update -y
7+
8+
echo "* hard nofile 64000" >> /etc/security/limits.conf
9+
echo "* soft nofile 64000" >> /etc/security/limits.conf
10+
echo "root hard nofile 64000" >> /etc/security/limits.conf
11+
echo "root soft nofile 64000" >> /etc/security/limits.conf
12+
13+
sudo tee /etc/yum.repos.d/mongodb-org-4.0.repo << EOF
14+
[mongodb-org-4.0]
15+
name=MongoDB Repository
16+
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/
17+
gpgcheck=1
18+
enabled=1
19+
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
20+
EOF
21+
22+
sudo tee /etc/yum.repos.d/pritunl.repo << EOF
23+
[pritunl]
24+
name=Pritunl Repository
25+
baseurl=https://repo.pritunl.com/stable/yum/centos/7/
26+
gpgcheck=1
27+
enabled=1
28+
EOF
29+
30+
sudo yum -y install oracle-epel-release-el7
31+
sudo yum-config-manager --enable ol7_developer_epel
32+
gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 7568D9BB55FF9E5287D586017AE645C0CF8E292A
33+
gpg --armor --export 7568D9BB55FF9E5287D586017AE645C0CF8E292A > key.tmp; sudo rpm --import key.tmp; rm -f key.tmp
34+
sudo yum -y remove iptables-services
35+
sudo yum -y install pritunl mongodb-org
36+
sudo systemctl start mongod pritunl
37+
sudo systemctl enable mongod pritunl
38+
39+
cat <<EOF > /etc/logrotate.d/pritunl
40+
/var/log/mongodb/*.log {
41+
daily
42+
missingok
43+
rotate 60
44+
compress
45+
delaycompress
46+
copytruncate
47+
notifempty
48+
}
49+
EOF

‎variables.tf

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
variable "aws_key_name" {
2+
description = "SSH keypair name for the VPN instance"
3+
}
4+
5+
variable "vpc_id" {
6+
description = "Which VPC VPN server will be created in"
7+
}
8+
9+
variable "public_subnet_id" {
10+
description = "One of the public subnet id for the VPN instance"
11+
}
12+
13+
variable "instance_type" {
14+
description = "Instance type for VPN Box"
15+
type = "string"
16+
default = "t2.micro"
17+
}
18+
19+
variable "whitelist" {
20+
description = "[List] Office IP CIDRs for SSH and HTTPS"
21+
type = "list"
22+
}
23+
24+
variable "whitelist_http" {
25+
description = "[List] Whitelist for HTTP port"
26+
type = "list"
27+
default = ["0.0.0.0/0"]
28+
}
29+
30+
variable "tags" {
31+
description = "A map of tags to add to all resources"
32+
default = {}
33+
}
34+
35+
variable "resource_name_prefix" {
36+
description = "All the resources will be prefixed with the value of this variable"
37+
default = "pritunl"
38+
}
39+
40+
variable "internal_cidrs" {
41+
description = "[List] IP CIDRs to whitelist in the pritunl's security group"
42+
type = "list"
43+
default = ["10.0.0.0/16"]
44+
}

0 commit comments

Comments
 (0)
Failed to load comments.