-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstance.tf
128 lines (105 loc) · 2.88 KB
/
instance.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
resource "aws_instance" "this" {
ami = "${data.aws_ami.this.image_id}"
availability_zone = "${var.availability_zone}"
instance_type = "${var.instance_type}"
key_name = "${var.instance_key_name}"
iam_instance_profile = "${var.instance_profile_name}"
user_data = "${data.template_cloudinit_config.user_data.rendered}"
# The EC2 instance should always be assigned to the default
# VPC since tunnel traffics are useing the public interface.
security_groups = ["${aws_security_group.this.name}"]
tags {
Name = "vpn"
}
}
resource "aws_volume_attachment" "ebs_att" {
count = "${var.ebs_volume_id != "" ? 1 : 0}"
device_name = "/dev/sdf"
force_detach = true
volume_id = "${var.ebs_volume_id}"
instance_id = "${aws_instance.this.id}"
}
resource "aws_security_group" "this" {
name_prefix = "vpn-instance"
ingress {
protocol = "tcp"
from_port = 443
to_port = 443
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
protocol = "udp"
from_port = 1194
to_port = 1194
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
protocol = "tcp"
from_port = 22
to_port = 22
cidr_blocks = ["0.0.0.0/0"]
}
egress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
tags {
Name = "vpn"
}
}
data "aws_ami" "this" {
most_recent = true
filter {
name = "name"
values = ["amzn-ami-2018.03.u-amazon-ecs-optimized"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["amazon"]
}
data "template_cloudinit_config" "user_data" {
gzip = true
base64_encode = true
part {
content_type = "text/cloud-config"
content = "${data.template_file.cloud_init.rendered}"
}
part {
content_type = "text/x-shellscript"
content = "${data.template_file.user_data.rendered}"
}
}
data "template_file" "cloud_init" {
template = "${file("${path.module}/templates/cloud-init.yml")}"
vars {
openvpn = "${data.template_file.openvpn_script.rendered}"
}
}
data "template_file" "user_data" {
template = "${file("${path.module}/templates/user-data.sh")}"
vars {
ecs_cluster = "${aws_ecs_cluster.this.name}"
ecs_attributes = ""
ecs_volume = "${var.ecs_volume_data}"
ebs_volume = "/dev/xvdf"
}
}
data "template_file" "openvpn_script" {
template = "${file("${path.module}/templates/openvpn.sh")}"
vars {
country = "${var.ovpn_country}"
province = "${var.ovpn_province}"
city = "${var.ovpn_city}"
company = "${var.ovpn_company}"
section = "${var.ovpn_section}"
email = "${var.ovpn_email}"
domain = "${var.ovpn_domain}"
volume = "${var.ecs_volume_data}"
client_conf = "${var.ecs_volume_conf}/client.ovpn"
clients_txt = "${var.ecs_volume_conf}/clients.txt"
}
}