はじめに
AWS上にVPNサーバーを構築する機会があったので、構築方法を紹介します。
この記事を見ると自分だけのVPNサーバーを構築することができます!
インフラはAWS CDKで構築しました。
今回紹介する構成は以下の図のとおりです。
ここでは、EC2インスタンスにOpenVPNとWireGuardをインストールしてVPNサーバーを構築する方法を紹介します。
二つのライブラリについて比較すると、OpenVPNは互換性が高いが低速、WireGuardはOpenVPNより互換性が低いが高速という特徴があります。
WireGuardが使える環境であればWireGuardを使うのが良さそうです。
両方インストールしておくことも可能ですので、お好みの方をお使いください。
1. VPC, EC2サーバーのデプロイ
AWS CDKのプロジェクトを以下のコマンドで作成しましょう。
mkdir vpn-server-on-aws-stack
cd vpn-server-on-aws-stack
npx cdk init app --language typescript
作成されたvpn-server-on-aws-stack.tsを以下のように編集します。
今回はus-east-1(バージニア北部)で構築しましたが、他のリージョンを使用する際にはインスタンスのmachineImageを修正してください。(ここではubuntuイメージを使用しています。)
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';
export class VpnServerOnAwsStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// VPC
const vpc = new ec2.Vpc(this, 'VpnVpc', {
ipProtocol: ec2.IpProtocol.IPV4_ONLY,
maxAzs: 2,
subnetConfiguration: [
{
cidrMask: 24,
name: 'PublicSubnet',
subnetType: ec2.SubnetType.PUBLIC,
},
],
natGateways: 0,
});
// Security Group for VPN Server
const vpnSecurityGroup = new ec2.SecurityGroup(this, 'VpnSecurityGroup', {
vpc,
description: 'Security group for VPN server',
allowAllOutbound: true,
});
vpnSecurityGroup.addIngressRule(
ec2.Peer.anyIpv4(),
ec2.Port.udp(1194),
'Allow OpenVPN UDP traffic on port 1194'
);
vpnSecurityGroup.addIngressRule(
ec2.Peer.anyIpv4(),
ec2.Port.udp(51820),
'Allow WireGuard UDP traffic on port 51820'
);
vpnSecurityGroup.addIngressRule(
ec2.Peer.anyIpv4(),
ec2.Port.SSH,
'Allow SSH access on port 22'
);
// EC2 Instance
const vpnInstance = new ec2.Instance(this, 'VpnInstance', {
vpc,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T4G, ec2.InstanceSize.MICRO),
machineImage: ec2.MachineImage.genericLinux({
'us-east-1': 'ami-026fccd88446aa0bf',
}),
securityGroup: vpnSecurityGroup,
role: vpnRole,
vpcSubnets: {
subnetType: ec2.SubnetType.PUBLIC,
},
});
// Elastic IP
new ec2.CfnEIP(this, 'VpnEIP', {
domain: 'vpc',
instanceId: vpnInstance.instanceId,
});
}
}
bin/vpn-server-on-aws.tsも以下のように変更し、us-east-1にデプロイされるように変更しましょう。
#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { VpnServerOnAwsStack } from '../lib/vpn-server-on-aws-stack';
const app = new cdk.App();
new VpnServerOnAwsStack(app, 'VpnServerOnAwsStack', {
env: {
region: 'us-east-1'
},
});
これでインフラの構築は完了です。
手動で作成する場合は、Publicサブネット上にEC2インスタンスを構築して、以下のようなセキュリティグループを設定してください。
具体的な作成方法は調べると参考記事がたくさん出てくると思います!
2. EC2インスタンスへの接続
VPNサーバーのインストールはEC2インスタンスへ接続してコマンド実行することで行います。
EC2 Instance Connectを使ってEC2インスタンスに接続しましょう。
作成したインスタンスの詳細画面にある「接続」ボタンを教えてください。
すると以下のような画面になると思うので、そのまま接続を押しましょう。
以下のような画面がでれば接続成功です。
接続できない場合は、セキュリティグループのポート22番を許可しているか、EC2インスタンスがPublicサブネット上に配置されているか確認しましょう。
3. OpenVPNのインストールと接続
OpenVPNのインストールを実施する場合はこの章の作業を実施してください。
WireGuardのみインストールする場合はスキップしてください。
OpenVPNのインストールを簡単に実施できるスクリプトが公開されているのでこちらを使用します。
以下のコマンドを実行するとインストラーが立ち上がるので、全てデフォルトで進めてください。
wget https://git.io/vpn -O openvpn-install.sh
sudo bash openvpn-install.sh
出力結果に記載されているように、設定ファイルは/home/ubuntu/client.ovpnに出力されます。
これをVPNクライアント側で読み込めば接続できます。
Welcome to this OpenVPN road warrior installer!
Which IPv4 address should be used?
1) 10.0.0.51
2) 10.7.0.1
IPv4 address [1]:
This server is behind NAT. What is the public IPv4 address or hostname?
Public IPv4 address / hostname [98.94.171.49]:
Which protocol should OpenVPN use?
1) UDP (recommended)
2) TCP
Protocol [1]:
What port should OpenVPN listen on?
Port [1194]:
Select a DNS server for the clients:
1) Default system resolvers
2) Google
3) 1.1.1.1
4) OpenDNS
5) Quad9
6) Gcore
7) AdGuard
8) Specify custom resolvers
DNS server [1]:
Enter a name for the first client:
Name [client]:
OpenVPN installation is ready to begin.
Press any key to continue...
Hit:1 http://ports.ubuntu.com/ubuntu-ports noble-security InRelease
Hit:2 http://us-east-1.ec2.ports.ubuntu.com/ubuntu-ports noble InRelease
Hit:3 http://us-east-1.ec2.ports.ubuntu.com/ubuntu-ports noble-updates InRelease
...
...
...
Finished!
The client configuration is available in: /home/ubuntu/client.ovpn
New clients can be added by running this script again.
例えば、iPhoneであれば以下のクライアントです。
他にもWindows、Mac、Androidも用意されているので必要なクライアントをインストールしてください。
4. WireGuardのインストールと接続
WireGuardについても同じ方がスクリプトを提供しているので使用させていただきます。
こちらも以下のコマンドを実行し、全てデフォルトで実行してください。
wget https://git.io/wireguard -O wireguard-install.sh
sudo bash wireguard-install.sh
こちらは出力結果にQRコードが表示されます。
このQRコードを読み取るか、/home/ubuntu/client.confに出力された設定ファイルを読み込んで接続します。
Welcome to this WireGuard road warrior installer!
This server is behind NAT. What is the public IPv4 address or hostname?
Public IPv4 address / hostname [98.94.171.49]:
What port should WireGuard listen on?
Port [51820]:
Enter a name for the first client:
Name [client]:
Select a DNS server for the client:
1) Default system resolvers
2) Google
3) 1.1.1.1
4) OpenDNS
5) Quad9
6) Gcore
7) AdGuard
8) Specify custom resolvers
DNS server [1]:
WireGuard installation is ready to begin.
Press any key to continue...
Hit:1 http://us-east-1.ec2.ports.ubuntu.com/ubuntu-ports noble InRelease
Get:2 http://us-east-1.ec2.ports.ubuntu.com/ubuntu-ports noble-updates InRelease [126 kB]
Get:3 http://us-east-1.ec2.ports.ubuntu.com/ubuntu-ports noble-backports InRelease [126 kB]
...
...
...
====
ここにQUコードが表示される。
====
↑ That is a QR code containing the client configuration.
Finished!
The client configuration is available in: /home/ubuntu/client.conf
New clients can be added by running this script again.
WireGuardも様々なプラットフォームで用意されているので、任意のクライアントをインストールしてください。
4. 動作確認
以下のようなIPアドレスがわかるサイトにアクセスして、VPNに接続してIPアドレスが変わっていれば成功です。
IPアドレスだけではなく、住所やインターネットサービスプロバイダーも変わっていますね!
| VPN接続前 | VPN接続後 |
|---|---|
![]() |
![]() |
まとめ
AWS上にVPNサーバーを構築する方法について紹介しました。
VPNサーバーを作るのは大変そうなイメージがありましたが、意外と簡単に作れるものだなと感じました。
様々なVPNサーバーのサービスが提供されていますが、セキュリティ的な不安やコスト面で課題を感じている方は自作してみるのは良いかもしれません。






