-
Notifications
You must be signed in to change notification settings - Fork 70
/
ec2.tf
135 lines (110 loc) · 3.15 KB
/
ec2.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
data "aws_ami" "amazon_linux_2" {
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm*"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "block-device-mapping.volume-type"
values = ["gp2"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["amazon"]
}
resource "aws_key_pair" "openvpn" {
key_name = var.ssh_private_key_file
public_key = file("${path.module}/${var.ssh_public_key_file}")
}
resource "aws_instance" "openvpn" {
ami = data.aws_ami.amazon_linux_2.id
associate_public_ip_address = true
instance_type = var.instance_type
key_name = aws_key_pair.openvpn.key_name
subnet_id = aws_subnet.openvpn.id
vpc_security_group_ids = [
aws_security_group.openvpn.id,
aws_security_group.ssh_from_local.id,
]
root_block_device {
volume_type = "gp2"
volume_size = var.instance_root_block_device_volume_size
delete_on_termination = true
}
tags = {
Name = var.tag_name
Provisioner = "Terraform"
}
}
resource "aws_eip" "openvpn_eip" {
instance = aws_instance.openvpn.id
vpc = true
}
resource "null_resource" "openvpn_bootstrap" {
connection {
type = "ssh"
host = aws_eip.openvpn_eip.public_ip
user = var.ec2_username
port = "22"
private_key = file("${path.module}/${var.ssh_private_key_file}")
agent = false
}
provisioner "remote-exec" {
inline = [
"sudo yum update -y",
"curl -O ${var.openvpn_install_script_location}",
"chmod +x openvpn-install.sh",
<<EOT
sudo AUTO_INSTALL=y \
APPROVE_IP=${aws_eip.openvpn_eip.public_ip} \
ENDPOINT=${aws_eip.openvpn_eip.public_dns} \
./openvpn-install.sh
EOT
,
]
}
}
resource "null_resource" "openvpn_update_users_script" {
depends_on = [null_resource.openvpn_bootstrap]
triggers = {
ovpn_users = join(" ", var.ovpn_users)
}
connection {
type = "ssh"
host = aws_eip.openvpn_eip.public_ip
user = var.ec2_username
port = "22"
private_key = file("${path.module}/${var.ssh_private_key_file}")
agent = false
}
provisioner "file" {
source = "${path.module}/scripts/update_users.sh"
destination = "/home/${var.ec2_username}/update_users.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x ~${var.ec2_username}/update_users.sh",
"sudo ~${var.ec2_username}/update_users.sh ${join(" ", var.ovpn_users)}",
]
}
}
resource "null_resource" "openvpn_download_configurations" {
depends_on = [null_resource.openvpn_update_users_script]
triggers = {
ovpn_users = join(" ", var.ovpn_users)
}
provisioner "local-exec" {
command = <<EOT
mkdir -p ${var.ovpn_config_directory};
scp -o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-i ${var.ssh_private_key_file} ${var.ec2_username}@${aws_eip.openvpn_eip.public_ip}:/home/${var.ec2_username}/*.ovpn ${var.ovpn_config_directory}/
EOT
}
}