-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathec2.tf
270 lines (246 loc) · 7.85 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
resource "aws_iam_instance_profile" "ec2_vpn_server" {
name_prefix = local.name
role = aws_iam_role.ec2_vpn_server.name
}
data "aws_iam_policy_document" "ec2_vpn_server" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ec2_vpn_server" {
name_prefix = "${local.name}-"
force_detach_policies = true
assume_role_policy = data.aws_iam_policy_document.ec2_vpn_server.json
}
resource "aws_iam_policy" "ec2_vpn_server_ssm" {
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssm:DescribeParameters",
"ssm:GetParameter"
],
"Resource": [
"arn:aws:ssm:${local.region}:${(local.account)}:parameter/${var.prefix}*"
]
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction"
],
"Resource": "${module.wg_manage.lambda_function_arn}"
}
]
}
POLICY
}
resource "aws_iam_role_policy_attachment" "ec2_vpn_server_ssm" {
policy_arn = aws_iam_policy.ec2_vpn_server_ssm.arn
role = aws_iam_role.ec2_vpn_server.name
}
data "aws_ami" "ubuntu" {
owners = ["099720109477"]
most_recent = true
filter {
name = "name"
values = ["ubuntu-minimal/images/hvm-ssd/ubuntu-focal-20.04-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
}
resource "aws_security_group" "ec2_vpn_server" {
name = "${local.name}-ec2_vpn_server"
description = "allow external access"
vpc_id = local.wg_vpc_id
ingress = [
{
description = "VPN traffic"
from_port = var.listen-port
to_port = var.listen-port
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = null
self = true
prefix_list_ids = null
security_groups = null
}
]
egress = [
{
description = "Allow all outgoing traffic"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
prefix_list_ids = null
security_groups = null
self = false
}
]
}
resource "aws_security_group" "ec2_vpn_server_ssh" {
name = "${local.name}-ec2-vpn-server-ssh"
description = "allow external access"
vpc_id = local.wg_vpc_id
count = var.aws_ec2_key != null ? 1 : 0
ingress = [
{
description = "ssh if needed"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = null
self = true
prefix_list_ids = null
security_groups = null
}
]
}
resource "aws_eip" "ec2_vpn_instance" {
vpc = true
tags = merge({
Name = "${local.name}-wireguard"
})
lifecycle {
ignore_changes = [tags]
}
}
module "ec2_vpn_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "~> v3.3.0"
iam_instance_profile = aws_iam_instance_profile.ec2_vpn_server.name
associate_public_ip_address = true
name = "${local.name}-vpn-server"
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
key_name = var.aws_ec2_key
monitoring = false
vpc_security_group_ids = (var.aws_ec2_key == null
? [aws_security_group.ec2_vpn_server.id]
: [ aws_security_group.ec2_vpn_server.id, aws_security_group.ec2_vpn_server_ssh[0].id ])
subnet_id = local.wg_subnet
hibernation = false
cpu_credits = "unlimited"
user_data = <<-EOT
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/usr/bin/bash
set -x
apt-get update
apt-get install -y jq wireguard iptables zip
systemctl stop [email protected]
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
./aws/install
REGION=`curl http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}'`
aws --region=$REGION lambda invoke --function-name ${module.wg_manage.lambda_function_name} invoke-out.txt
aws --region=$REGION ssm get-parameter --with-decryption --name "${local.wg_ssm_config}" | jq -r .Parameter.Value > /etc/wireguard/wg0.conf
sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf
sysctl -p
sleep 20
systemctl enable [email protected]
systemctl start [email protected]
systemctl status [email protected]
#rm /var/lib/cloud/instances/*/sem/config_scripts_user || true
###Setup script to reload WG config
mkdir -p /var/usr/wg-check-reload
cd /var/usr/wg-check-reload
cat > is_reload.sh << 'EOF'
#!/bin/bash
#Get cofig from SSM and compare it with the local config, if SSM has newest version of config - update config and reload service
#Set period of time tolerant to config modification in seconds
GRACE_PERIOD=100
REGION=$(curl http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}')
SSM_LAST_MODIFIED_TIME=$(aws --region=$REGION ssm get-parameter --with-decryption --name "${local.wg_ssm_config}" | jq -r .Parameter.LastModifiedDate | xargs -I {} date --date={} +"%s" )
#echo "Last time config modified in SSM:"$SSM_LAST_MODIFIED_TIME
LOCAL_LAST_MODIFIED_TIME=$(stat -c%Z /etc/wireguard/wg0.conf)
#echo "Last time local config modification:"$LOCAL_LAST_MODIFIED_TIME
TIME_DIFF=$(expr $SSM_LAST_MODIFIED_TIME - $LOCAL_LAST_MODIFIED_TIME )
#echo "Time diff:"$TIME_DIFF
if [[ $TIME_DIFF -ge $GRACE_PERIOD ]]
then
echo "SSM config is newer, reloading WG..."
aws --region=$REGION ssm get-parameter --with-decryption --name "${local.wg_ssm_config}" | jq -r .Parameter.Value > /etc/wireguard/wg0.conf
wg syncconf wg0 <(wg-quick strip /etc/wireguard/wg0.conf)
else
echo "config is pretty new or latest"
fi
EOF
chmod 755 ./is_reload.sh
###Create Systemd Unit to reload wg when config updated
cat > /etc/systemd/system/wg-conf-check-reload.service << 'EOF'
[Unit]
Description=Check WG conf
After=syslog.target network.target
[Service]
Type=oneshot
ExecStart=/var/usr/wg-check-reload/is_reload.sh
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/systemd/system/wg-conf-check-reload.timer << 'EOF'
[Unit]
Description=Logs some system statistics to the systemd journal
Requires=wg-conf-check-reload.service
[Timer]
Unit=wg-conf-check-reload.service
OnCalendar=*:0/1
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable wg-conf-check-reload.timer
systemctl start wg-conf-check-reload.timer
EOT
}
resource "aws_eip_association" "vpn_server_eip" {
instance_id = module.ec2_vpn_instance.id
allocation_id = aws_eip.ec2_vpn_instance.id
}
resource "aws_ssm_parameter" "wg-instance-id" {
name = local.wg_ssm_instance_id
description = "Wireguard server instance id"
type = "String"
value = module.ec2_vpn_instance.id
}
output "vpn_external_address" {
value = aws_eip.ec2_vpn_instance.address
}