-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.tf
139 lines (109 loc) · 3.55 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
###############################################################################
# VARIABLES
###############################################################################
variable "aws_region" {
default = "eu-central-1"
}
variable "ssh_remote_user" {
default = "docker"
}
variable "ssh_public_key_path" {
default = "~/.ssh/id_rsa.pub"
}
variable "vpn_data" {
default = "openvpn-data-default"
}
variable "vpn_port" {
default = 1194
}
variable "vpn_client_name" {
default = "awesome-personal-vpn"
}
###############################################################################
# PROVIDERS
###############################################################################
provider "aws" {
region = "${var.aws_region}"
}
###############################################################################
# RESOURCES
###############################################################################
resource "aws_key_pair" "deployer" {
key_name = "terraform-deployer-key"
public_key = "${file(var.ssh_public_key_path)}"
}
resource "aws_security_group" "vpn" {
name = "terraform-vpn-security-group"
ingress {
from_port = "${var.vpn_port}"
to_port = "${var.vpn_port}"
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "ssh" {
name = "terraform-ssh-security-group"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "outgoing" {
name = "terraform-outgoing-security-group"
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "vpn" {
instance_type = "t2.micro"
ami = "ami-633ba70c"
vpc_security_group_ids = [
"${aws_security_group.vpn.id}",
"${aws_security_group.ssh.id}",
"${aws_security_group.outgoing.id}",
]
key_name = "terraform-deployer-key"
connection {
user = "${var.ssh_remote_user}"
agent = true
}
provisioner "remote-exec" {
inline = [
"docker volume create --name ${var.vpn_data}",
"docker run -v ${var.vpn_data}:/etc/openvpn --rm kylemanna/openvpn ovpn_genconfig -u udp://${aws_instance.vpn.public_dns}",
"yes 'yes' | docker run -v ${var.vpn_data}:/etc/openvpn --rm -i kylemanna/openvpn ovpn_initpki nopass",
"docker run -v ${var.vpn_data}:/etc/openvpn -d -p ${var.vpn_port}:${var.vpn_port}/udp --cap-add=NET_ADMIN kylemanna/openvpn",
"docker run -v ${var.vpn_data}:/etc/openvpn --rm -it kylemanna/openvpn easyrsa build-client-full ${var.vpn_client_name} nopass",
"docker run -v ${var.vpn_data}:/etc/openvpn --rm kylemanna/openvpn ovpn_getclient ${var.vpn_client_name} > ~/${var.vpn_client_name}.ovpn",
]
}
provisioner "local-exec" {
command = "ssh-keyscan -T 120 ${aws_instance.vpn.public_ip} >> ~/.ssh/known_hosts"
}
provisioner "local-exec" {
command = "scp ${var.ssh_remote_user}@${aws_instance.vpn.public_ip}:~/${var.vpn_client_name}.ovpn ."
}
tags {
Name = "terraform-openvpn"
}
}
###############################################################################
# OUTPUT
###############################################################################
output "aws_instance_public_dns" {
value = "${aws_instance.vpn.public_dns}"
}
output "aws_instance_public_ip" {
value = "${aws_instance.vpn.public_ip}"
}
output "client_configuration_file" {
value = "${var.vpn_client_name}.ovpn"
}
output "closing_message" {
value = "Your VPN is ready! Check out client configuration file to configure your client! Have fun!'"
}