|
| 1 | +resource "aws_eip" "lab_relay" { |
| 2 | + instance = aws_instance.lab_relay.id |
| 3 | + domain = "vpc" |
| 4 | + depends_on = [aws_internet_gateway.default] |
| 5 | + |
| 6 | + tags = { |
| 7 | + Name = "${local.name}-lab-relay" |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +resource "aws_instance" "lab_relay" { |
| 12 | + ami = data.aws_ami.minimal-arm64.id |
| 13 | + availability_zone = local.availability_zone |
| 14 | + instance_type = "t4g.nano" |
| 15 | + subnet_id = aws_subnet.public.id |
| 16 | + iam_instance_profile = aws_iam_instance_profile.ecs_instance.name |
| 17 | + vpc_security_group_ids = [aws_security_group.lab_relay.id] |
| 18 | + source_dest_check = false |
| 19 | + associate_public_ip_address = true |
| 20 | + user_data_replace_on_change = true |
| 21 | + |
| 22 | + root_block_device { |
| 23 | + volume_type = "gp3" |
| 24 | + volume_size = 4 |
| 25 | + } |
| 26 | + |
| 27 | + user_data = base64encode(<<-INIT |
| 28 | + #!/bin/bash |
| 29 | + yum install -y amazon-ssm-agent |
| 30 | + systemctl enable amazon-ssm-agent |
| 31 | + systemctl start amazon-ssm-agent |
| 32 | +
|
| 33 | + amazon-linux-extras install epel -y |
| 34 | + yum install -y openvpn easy-rsa |
| 35 | +
|
| 36 | + echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf |
| 37 | + sysctl -p |
| 38 | +
|
| 39 | + # Set up PKI using elliptic curve (avoids slow DH param generation) |
| 40 | + EASYRSA_SRC=$(find /usr/share/easy-rsa -name "easyrsa" | head -1 | xargs dirname) |
| 41 | + mkdir -p /etc/openvpn/easy-rsa |
| 42 | + cp -r $EASYRSA_SRC/* /etc/openvpn/easy-rsa/ |
| 43 | + cd /etc/openvpn/easy-rsa |
| 44 | +
|
| 45 | + export EASYRSA_BATCH=1 |
| 46 | + export EASYRSA_ALGO=ec |
| 47 | + export EASYRSA_CURVE=prime256v1 |
| 48 | +
|
| 49 | + ./easyrsa init-pki |
| 50 | + ./easyrsa build-ca nopass |
| 51 | + ./easyrsa build-server-full server nopass |
| 52 | + ./easyrsa build-client-full client nopass |
| 53 | + ./easyrsa build-client-full chr nopass |
| 54 | +
|
| 55 | + # CCD for CHR: route lab subnets through CHR |
| 56 | + mkdir -p /etc/openvpn/ccd |
| 57 | + cat > /etc/openvpn/ccd/chr <<'CCD' |
| 58 | + iroute 192.168.88.0 255.255.255.0 |
| 59 | + iroute 192.168.1.0 255.255.255.0 |
| 60 | + CCD |
| 61 | +
|
| 62 | + # Server config |
| 63 | + cat > /etc/openvpn/server/server.conf <<'CONF' |
| 64 | + port 1194 |
| 65 | + proto udp |
| 66 | + dev tun |
| 67 | + ca /etc/openvpn/easy-rsa/pki/ca.crt |
| 68 | + cert /etc/openvpn/easy-rsa/pki/issued/server.crt |
| 69 | + key /etc/openvpn/easy-rsa/pki/private/server.key |
| 70 | + dh none |
| 71 | + cipher AES-256-GCM |
| 72 | + auth SHA256 |
| 73 | + server 10.8.0.0 255.255.255.0 |
| 74 | + client-config-dir /etc/openvpn/ccd |
| 75 | + route 192.168.88.0 255.255.255.0 |
| 76 | + route 192.168.1.0 255.255.255.0 |
| 77 | + push "route 192.168.88.0 255.255.255.0" |
| 78 | + push "route 192.168.1.0 255.255.255.0" |
| 79 | + keepalive 10 120 |
| 80 | + persist-key |
| 81 | + persist-tun |
| 82 | + verb 3 |
| 83 | + CONF |
| 84 | +
|
| 85 | + # NAT for VPN clients |
| 86 | + cat <<EOF > /etc/systemd/system/openvpn-nat.service |
| 87 | + [Unit] |
| 88 | + Description=OpenVPN NAT |
| 89 | + After=network.target |
| 90 | +
|
| 91 | + [Service] |
| 92 | + Type=oneshot |
| 93 | + ExecStart=/usr/sbin/iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE |
| 94 | + ExecStop=/usr/sbin/iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE |
| 95 | + RemainAfterExit=yes |
| 96 | +
|
| 97 | + [Install] |
| 98 | + WantedBy=multi-user.target |
| 99 | + EOF |
| 100 | +
|
| 101 | + systemctl daemon-reload |
| 102 | + systemctl enable openvpn-nat |
| 103 | + systemctl start openvpn-nat |
| 104 | +
|
| 105 | + systemctl enable openvpn-server@server |
| 106 | + systemctl start openvpn-server@server |
| 107 | +
|
| 108 | + # Build the shareable client config with all certs embedded |
| 109 | + cat > /etc/openvpn/client.ovpn <<'OVPN' |
| 110 | + client |
| 111 | + dev tun |
| 112 | + proto udp |
| 113 | + remote vpn.${local.domain} 1194 |
| 114 | + resolv-retry infinite |
| 115 | + nobind |
| 116 | + persist-key |
| 117 | + persist-tun |
| 118 | + remote-cert-tls server |
| 119 | + cipher AES-256-GCM |
| 120 | + auth SHA256 |
| 121 | + verb 3 |
| 122 | + <ca> |
| 123 | + OVPN |
| 124 | + cat /etc/openvpn/easy-rsa/pki/ca.crt >> /etc/openvpn/client.ovpn |
| 125 | + echo "</ca>" >> /etc/openvpn/client.ovpn |
| 126 | + echo "<cert>" >> /etc/openvpn/client.ovpn |
| 127 | + sed -n '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/p' /etc/openvpn/easy-rsa/pki/issued/client.crt >> /etc/openvpn/client.ovpn |
| 128 | + echo "</cert>" >> /etc/openvpn/client.ovpn |
| 129 | + echo "<key>" >> /etc/openvpn/client.ovpn |
| 130 | + cat /etc/openvpn/easy-rsa/pki/private/client.key >> /etc/openvpn/client.ovpn |
| 131 | + echo "</key>" >> /etc/openvpn/client.ovpn |
| 132 | + INIT |
| 133 | + ) |
| 134 | + |
| 135 | + tags = { |
| 136 | + Name = "${local.name}-lab-relay" |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +resource "aws_security_group" "lab_relay" { |
| 141 | + name = "${local.name}-lab-relay" |
| 142 | + vpc_id = aws_vpc.default.id |
| 143 | + |
| 144 | + ingress { |
| 145 | + from_port = 1194 |
| 146 | + to_port = 1194 |
| 147 | + protocol = "udp" |
| 148 | + cidr_blocks = ["0.0.0.0/0"] |
| 149 | + ipv6_cidr_blocks = ["::/0"] |
| 150 | + } |
| 151 | + |
| 152 | + egress { |
| 153 | + from_port = 0 |
| 154 | + to_port = 0 |
| 155 | + protocol = "-1" |
| 156 | + cidr_blocks = ["0.0.0.0/0"] |
| 157 | + ipv6_cidr_blocks = ["::/0"] |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +resource "aws_route53_record" "wireguard" { |
| 162 | + zone_id = local.domain_zone_id |
| 163 | + name = "vpn.${local.domain}" |
| 164 | + type = "A" |
| 165 | + ttl = 300 |
| 166 | + records = [aws_eip.lab_relay.public_ip] |
| 167 | +} |
0 commit comments