วิธีการใช้งาน AWS Client VPN

AWS Client VPN ให้บริการ Client แบบไม่คิดค่าใช้จ่าย คุณสามารถเชื่อมต่ออุปกรณ์คอมพิวเตอร์ของคุณเข้ากับ AWS Client VPN โดยตรงเพื่อสัมผัสประสบการณ์การใช้งาน VPN แบบ end-to-end

สวัสดีครับ POP จากบริษัท Classmethod (Thailand) ครับ

ครั้งนี้จะมาแนะนำเกี่ยวกับการใช้งาน AWS Client VPN สำหรับ Desktop

AWS Client VPN คืออะไร

AWS Client VPN ให้บริการ Client แบบไม่คิดค่าใช้จ่าย คุณสามารถเชื่อมต่ออุปกรณ์คอมพิวเตอร์ของคุณเข้ากับ AWS Client VPN โดยตรงเพื่อสัมผัสประสบการณ์การใช้งาน VPN แบบ end-to-end

สิ่งที่ต้องมี

สร้าง VPC

ครั้งนี้เราจะสร้าง VPC ตามแผนภาพด้านล่างนี้โดยไม่มี Public IP ใน Region: Singapore

ดูตัวอย่างการสร้าง VPC ได้ที่ลิงก์บทความด้านล่างนี้ (ลิงก์บทความด้านล่างนี้เป็นแค่ตัวอย่าง ซึ่งมีการตั้งค่าบางส่วนที่แตกต่างกัน แนะนำให้ดู ตัวอย่างตั้งค่าการสร้าง VPC ของบทความครั้งนี้ ประกอบด้วย)

ตัวอย่างตั้งค่าการสร้าง VPC ของบทความครั้งนี้

Create VPC
VPC settings
Resources to create: ◎ VPC and more

Name tag auto-generation
✅ Auto-generate: tinnakorn-cvpn-test (เมื่อป้อนชื่อแล้วให้สังเกตการเปลี่ยนแปลงด้านขวา)
IPv4 CIDR block: 10.0.0.0/16 (Default)

Number of Availability Zones (AZs): 1
Number of public subnets: 0
Number of private subnets: 1
▼ Customize subnets CIDR blocks (คลิกหัวข้อนี้ก่อนแล้วจะเห็นช่องสำหรับป้อน IP)
Private subnet CIDR block in ap-southeast-1a: 10.0.0.0/24 (ตัวอย่าง)

DNS options
✅ Enable DNS hostnames
✅ Enable DNS resolution

คลิก Create VPC

สร้าง Certificate ด้วย ACM

เราจะสร้าง Certificate ด้วย ACM โดยการ Run Commands ตามแผนภาพด้านล่างนี้

Run Commands ใน AWS CloudShell

AWS CloudShell ไม่มีให้บริการในรีเจี้ยน Singapore แต่มีให้บริการในรีเจี้ยน Sydney ดังนั้น AWS CloudShell ในรีเจี้ยน Sydney จะเริ่มทำงานโดยอัตโนมัติ

ตรวจสอบ Region ให้ตรงกับที่สร้าง VPC ไว้จากขั้นตอนที่แล้ว เช่น Singapore
แล้วคลิกไอคอน AWS CloudShell แล้วรอสักครู่ ก็จะเห็น AWS CloudShell แสดงขึ้นมาจากด้านล่าง

รันคำสั่งนี้เพื่อติดตั้ง OpenSSL

sudo yum -y install openssl


รันคำสั่งนี้เพื่อ Clone easy-rsa.git

git clone https://github.com/OpenVPN/easy-rsa.git


เมื่อดาวน์โหลดเสร็จแล้ว รันคำสั่งนี้เพื่อเข้าไปที่โฟลเดอร์ easyrsa3

cd easy-rsa/easyrsa3


ต่อไปรันคำสั่งนี้เพื่อเริ่มต้น PKI (Public Key Infrastructure)

./easyrsa init-pki


ต่อไปรันคำสั่งนี้เพื่อสร้าง CA

./easyrsa build-ca nopass

Output (example) - 1: แล้วจะมีคำถามแสดงขึ้นมา ให้กดปุ่ม Enter

AWS CloudShell (ap-southeast-2)

[cloudshell-user@ip-xx-xx-xx-xx easyrsa3]$ ./easyrsa build-ca nopass

* Using Easy-RSA configuration:
  /home/cloudshell-user/easy-rsa/easyrsa3/pki/vars

* Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017

.........................................................................................................................................................+++
.......................................................+++
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:

Output (example) - 2: เมื่อแสดงหน้าจอแบบนี้ถือว่าการสร้าง CA สำเร็จ

AWS CloudShell (ap-southeast-2)

Notice
------
CA creation complete. Your new CA certificate is at:
* /home/cloudshell-user/easy-rsa/easyrsa3/pki/ca.crt

[cloudshell-user@ip-xx-xx-xx-xx easyrsa3]$


ต่อไปรันคำสั่งนี้เพื่อสร้าง Certificates สำหรับ OpenVPN server

./easyrsa build-server-full server nopass

Output (example) - 1: แล้วจะมีคำถามแสดงขึ้นมา ให้พิมพ์ yes แล้วกดปุ่ม Enter

AWS CloudShell (ap-southeast-2)

[cloudshell-user@ip-xx-xx-xx-xx easyrsa3]$ ./easyrsa build-server-full server nopass

* Using Easy-RSA configuration:
  /home/cloudshell-user/easy-rsa/easyrsa3/pki/vars

* Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017

Generating a 2048 bit RSA private key
........................................................................................................................+++
...............................+++
writing new private key to '/home/cloudshell-user/easy-rsa/easyrsa3/pki/0054a8a9/temp.1.1'
-----

Notice
------
Keypair and certificate request completed. Your files are:
* req: /home/cloudshell-user/easy-rsa/easyrsa3/pki/reqs/server.req
* key: /home/cloudshell-user/easy-rsa/easyrsa3/pki/private/server.key 

You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a server certificate 
for '825' days:

subject=
    commonName                = server


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes

Output (example) - 2: เมื่อแสดงหน้าจอแบบนี้ถือว่าการสร้าง Certificates สำหรับ OpenVPN server สำเร็จ

AWS CloudShell (ap-southeast-2)

Using configuration from /home/cloudshell-user/easy-rsa/easyrsa3/pki/0054a8a9/temp.3.1
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'server'
Certificate is to be certified until Aug  4 09:53:53 2025 GMT (825 days)

Write out database with 1 new entries
Data Base Updated

Notice
------
Certificate created at:
* /home/cloudshell-user/easy-rsa/easyrsa3/pki/issued/server.crt

Notice
------
Inline file created:
* /home/cloudshell-user/easy-rsa/easyrsa3/pki/inline/server.inline

[cloudshell-user@ip-xx-xx-xx-xx easyrsa3]$


ต่อไปรันคำสั่งนี้เพื่อสร้าง Certificates สำหรับแต่ละ OpenVPN client

./easyrsa build-client-full client1.domain.tld nopass

Output (example) - 1: แล้วจะมีคำถามแสดงขึ้นมา ให้พิมพ์ yes แล้วกดปุ่ม Enter

AWS CloudShell (ap-southeast-2)

[cloudshell-user@ip-xx-xx-xx-xx easyrsa3]$ ./easyrsa build-client-full client1.domain.tld nopass

* Using Easy-RSA configuration:
  /home/cloudshell-user/easy-rsa/easyrsa3/pki/vars

* Using SSL: openssl OpenSSL 1.0.2k-fips  26 Jan 2017

Generating a 2048 bit RSA private key
....................+++
......................+++
writing new private key to '/home/cloudshell-user/easy-rsa/easyrsa3/pki/85245d01/temp.1.1'
-----

Notice
------
Keypair and certificate request completed. Your files are:
* req: /home/cloudshell-user/easy-rsa/easyrsa3/pki/reqs/client1.domain.tld.req
* key: /home/cloudshell-user/easy-rsa/easyrsa3/pki/private/client1.domain.tld.key 

You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a client certificate 
for '825' days:

subject=
    commonName                = client1.domain.tld


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes

Output (example) - 2: เมื่อแสดงหน้าจอแบบนี้ถือว่าการสร้าง Certificates สำหรับแต่ละ OpenVPN client สำเร็จ

AWS CloudShell (ap-southeast-2)

Using configuration from /home/cloudshell-user/easy-rsa/easyrsa3/pki/85245d01/temp.3.1
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'client1.domain.tld'
Certificate is to be certified until Aug  4 10:55:55 2025 GMT (825 days)

Write out database with 1 new entries
Data Base Updated

Notice
------
Certificate created at:
* /home/cloudshell-user/easy-rsa/easyrsa3/pki/issued/client1.domain.tld.crt

Notice
------
Inline file created:
* /home/cloudshell-user/easy-rsa/easyrsa3/pki/inline/client1.domain.tld.inline

[cloudshell-user@ip-xx-xx-xx-xx easyrsa3]$


รันคำสั่งนี้เพื่อสร้างโฟลเดอร์ custom_folder

mkdir ~/custom_folder/


รันคำสั่งตามนี้เพื่อย้ายไฟล์ที่ได้จากการรันคำก่อนหน้านี้ไปยังโฟลเดอร์ custom_folder (สามารถรันคำสั่งพร้อมกันได้เลย)

cp pki/ca.crt ~/custom_folder/
cp pki/issued/server.crt ~/custom_folder/
cp pki/private/server.key ~/custom_folder/
cp pki/issued/client1.domain.tld.crt ~/custom_folder/
cp pki/private/client1.domain.tld.key ~/custom_folder/
cd ~/custom_folder/


รันคำสั่งนี้เพื่อดูข้อมูล

ll

Output (example): แล้วจะเห็นไฟล์ที่ย้ายเข้ามา โดยไฟล์เหล่านี้เป็น Key สำหรับการใช้งาน VPN

AWS CloudShell (ap-southeast-2)

-rw------- 1 cloudshell-user cloudshell-user 1172 May  2 11:04 ca.crt    // ไฟล์ Certificate
-rw------- 1 cloudshell-user cloudshell-user 4460 May  2 11:04 client1.domain.tld.crt    // ไฟล์ Certificate
-rw------- 1 cloudshell-user cloudshell-user 1704 May  2 11:04 client1.domain.tld.key    // ไฟล์ Private Key
-rw------- 1 cloudshell-user cloudshell-user 4547 May  2 11:04 server.crt    // ไฟล์ Certificate
-rw------- 1 cloudshell-user cloudshell-user 1704 May  2 11:04 server.key    // ไฟล์ Private Key


รันคำสั่งตามนี้เพื่อสร้าง ACM สำหรับ Server โดยระบุ [Region] ที่เรากำลังเข้าใช้งานอยู่ เช่น ap-southeast-1 (Singapore) ก็จะเป็น --region ap-southeast-1 แบบนี้
(ตอนนี้ AWS CloudShell กำลังทำงานในรีเจี้ยน Sydney เราจึงต้องระบุเป็น Region: Singapore)

aws acm import-certificate --region [Region] --certificate fileb://server.crt --private-key fileb://server.key --certificate-chain fileb://ca.crt

Output (example): เมื่อแสดงหน้าจอแบบนี้ถือว่าการสร้าง ACM สำหรับ Server สำเร็จ

AWS CloudShell (ap-southeast-2)

[cloudshell-user@ip-xx-xx-xx-xx custom_folder]$ aws acm import-certificate --region ap-southeast-1 --certificate fileb://server.crt --private-key fileb://server.key --certificate-chain fileb://ca.crt
{
    "CertificateArn": "arn:aws:acm:ap-southeast-1:xxxxxxxxxxxx:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
[cloudshell-user@ip-xx-xx-xx-xx custom_folder]$


รันคำสั่งตามนี้เพื่อสร้าง ACM สำหรับ Client โดยระบุ [Region] ที่เรากำลังเข้าใช้งานอยู่ เช่น ap-southeast-1 (Singapore) ก็จะเป็น --region ap-southeast-1 แบบนี้
(ตอนนี้ AWS CloudShell กำลังทำงานในรีเจี้ยน Sydney เราจึงต้องระบุเป็น Region: Singapore)

aws acm import-certificate --region [Region] --certificate fileb://client1.domain.tld.crt --private-key fileb://client1.domain.tld.key --certificate-chain fileb://ca.crt

Output (example): เมื่อแสดงหน้าจอแบบนี้ถือว่าการสร้าง ACM สำหรับ Client สำเร็จ

AWS CloudShell (ap-southeast-2)

[cloudshell-user@ip-xx-xx-xx-xx custom_folder]$ aws acm import-certificate --region ap-southeast-1 --certificate fileb://client1.domain.tld.crt --private-key fileb://client1.domain.tld.key --certificate-chain fileb://ca.crt
{
    "CertificateArn": "arn:aws:acm:ap-southeast-1:xxxxxxxxxxxx:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
[cloudshell-user@ip-xx-xx-xx-xx custom_folder]$

ตรวจสอบ Certificate

ตรวจสอบว่ามี Certificate ที่หน้าจอ AWS Certificate Manager (ACM)

ให้พิมพ์ Certificate Manager ในช่องค้นหา แล้วเลือก Certificate Manager

เลือก Region ตามที่เราระบุไว้ตอนรันคำสั่งสร้าง ACM เช่น ap-southeast-1 = Singapore
แล้วเลือก List certificates ก็จะเห็น Certificates 2 อันตามที่เราสร้างไว้โดยการรันคำสั่งก่อนหน้านี้

Download file

กลับมาที่หน้าจอที่เปิด AWS CloudShell ไว้อยู่ แล้วดาวน์โหลดไฟล์ client1.domain.tld.crt และ client1.domain.tld.key โดยตรงจาก AWS CloudShell ดังนี้เพื่อนำไปใช้ภายหลัง

คลิก Actions จากมุมขวาบน แล้วเลือก Download file

เมื่อแสดง POPUP แบบนี้ ให้ป้อน Path ลงใน Individual file path ตามด้านล่างนี้และคลิก Download

~/custom_folder/client1.domain.tld.crt

จากนั้นให้ดาวน์โหลดไฟล์อีกครั้งโดยป้อน Path ลงใน Individual file path ตามด้านล่างนี้และคลิก Download

~/custom_folder/client1.domain.tld.key

เมื่อดาวน์โหลดทั้ง 2 ไฟล์แล้ว ก็จะได้ไฟล์ client1.domain.tld.crt และ client1.domain.tld.key อยู่ในอุปกรณ์ของเรา

สร้าง AWS Client VPN ใน Amazon VPC

เราจะสร้าง Client VPN endpoints ตามแผนภาพด้านล่างนี้ ใน Region: Singapore

ให้กลับไปที่หน้าจอ VPC แล้วเลือก Client VPN endpoints ในหัวข้อ ▼ Virtual private network (VPN) จากเมนูด้านซ้าย แล้วคลิก Create client VPN endpoint ด้านขวา

เมื่อเข้ามาที่หน้า Create client VPN endpoint แล้วจะทำการตั้งค่าดังนี้

Details
Name: tinnakorn-cvpn-test
Client IPv4 CIDR: 10.1.0.0/22

Authentication information
Server certificate ARN: server (เลือก ACM ของ "Server")
Authentication options: ติ๊ก Use mutual authentication
Client certificate ARN: client1.domain.tld (เลือก ACM ของ "client1.domain.tld")

Other parameters - optional
Enable split-tunnel: เลื่อนมาด้านขวาเพื่อเปิดใช้งาน
หมายเหตุ: การเปิดใช้งาน split-tunnel จะทำให้เราเปิดใช้งาน Client VPN แล้วจะเกิดการแบ่งการเชื่อมต่อดังนี้
AWS Endpoint > ใช้ IP ของ Client VPN
Endpoint อื่นๆ > ใช้ IP ของเครื่องเราเอง
ซึ่งการทำแบบนี้จะช่วยลดการถ่ายโอนข้อมูลของ Client VPN ซึ่งจะมีส่วนในการลดค่าใช้จ่ายที่จะเกิดขึ้นได้

VPC ID: เลือก VPC ที่สร้างไว้ตอนแรก เช่น tinnakorn-cvpn-test

เลื่อนลงมาด้านล่างสุดแล้วคลิก Create client VPN endpoint

เมื่อแสดงหน้าจอแบบนี้แล้ว จะเห็นว่า State เป็น Pending-associate ให้รอการเริ่มต้นระบบสักครู่โดยอาจใช้เวลาประมาณ 10 นาที ในระหว่างที่รอเราสามารถเริ่มทำขั้นตอนถัดไปได้เลย

ให้เลือก Client VPN endpoints ของเรา เช่น tinnakorn-cvpn-test แล้วจะมีหน้าต่างปรากฏขึ้นมาจากด้านล่าง จากนั้นให้เลือกแท็บ Target network associations แล้วคลิก Associate target network

เมื่อเข้ามาหน้า Associate target network ก็ให้เลือก VPC และ Choose a subnet to associate ที่สร้างไว้ก่อนหน้านี้ แล้วคลิก Associate target network

แล้วเลือกแท็บ Target network associations ด้านล่าง จะเห็นว่า State เป็น Associating ไม่ต้องรอให้เสร็จ ให้ทำขั้นตอนถัดไปได้เลย

ต่อไปเลือกแท็บ Authorization rules แล้วคลิก Add authorization rule

เมื่อเข้ามาหน้า Add authorization rule ก็ให้ป้อนหรือเลือก Destination network to enable access ที่ต้องการ
ในตัวอย่างนี้คือ 0.0.0.0/0 แล้วคลิก Add authorization rule

แล้วเลือกแท็บ Authorization rules ด้านล่าง จะเห็นว่า State เป็น Authorizing ไม่ต้องรอให้เสร็จ ให้ทำขั้นตอนถัดไปได้เลย

ต่อไปให้ดูด้านบนที่ Client VPN endpoints แล้วคลิก Download client configuration

แล้วจะมี POPUP แสดงขึ้นมา ให้คลิก Download client configuration อีกครั้งเพื่อดาวน์โหลด
เมื่อดาวน์โหลดเสร็จแล้วจะได้ไฟล์ downloaded-client-config.ovpn อยู่ในอุปกรณ์ของเรา

ทีนี้กลับมาดูที่ Client VPN endpoints อีกครั้ง หากเริ่มต้นระบบเสร็จแล้ว State จะเปลี่ยนเป็น ✅ Available (ครั้งนี้ใช้เวลาประมาณ 10 นาที)
หรือหาก State ยังเป็น Pending-associate อยู่ ไม่จำเป็นต้องรอ ให้ทำขั้นตอนถัดไปได้เลย

การใช้งาน AWS Client VPN for Desktop

ดาวน์โหลด AWS Client VPN for Desktop

ดาวน์โหลด AWS Client VPN Download

ติดตั้ง AWS Client VPN for Desktop

เมื่อดาวน์โหลดเสร็จแล้วจะได้ไฟล์ AWS_VPN_Client.msi ให้ดับเบิ้ลคลิกที่ไฟล์ติดตั้ง แล้วทำการติดตั้งดังนี้

ติ๊ก ✅ I accept the terms in the License Agreement และคลิก Install

คลิก Finish

เตรียมไฟล์

เราต้องเตรียมไฟล์ downloaded-client-config.ovpn ที่ดาวน์โหลดมาก่อนหน้านี้เพื่อจะนำไปใช้กับโปรแกรม AWS Client VPN ภายหลัง
ก่อนอื่นให้นำไฟล์ downloaded-client-config.ovpn ไปเปิดใน Notepad ที่เราใช้งานอยู่เตรียมไว้ แล้วจะแสดงเนื้อหาแบบนี้ (ในตัวอย่างนี้จะใช้ Notepad ของ Windows)

ต่อไปให้เปิดไฟล์ client1.domain.tld.crt ใน Notepad แล้วเลื่อนลงมาด้านล่างสุด ก็จะแสดงเนื้อหาแบบนี้ แล้วให้คัดลอกเนื้อหาในส่วนของ BEGIN CERTIFICATE ตรงที่ครอบเตรียมไว้

กลับมาที่ไฟล์ downloaded-client-config.ovpn แล้วเลื่อนลงมาด้านล่างสุด แล้ว Enter เพื่อเพิ่มไป 3 บรรทัดและพิมพ์ tag code ตามด้านล่างนี้
แล้ววาง BEGIN CERTIFICATE ที่คัดลอกมาลงใน tag code ก็จะได้ตามรูปภาพด้านล่าง แล้วทำการ Save (* เมื่อวางไปแล้วอย่าให้มีบรรทัดว่าง เพราะจะทำให้โปรแกรมไม่ทำงาน)

<cert>
</cert>

ต่อไปให้เปิดไฟล์ client1.domain.tld.key ใน Notepad ก็จะแสดงเนื้อหาแบบนี้ แล้วให้คัดลอกเนื้อหาในส่วนของ BEGIN PRIVATE KEY ตรงที่ครอบเตรียมไว้

กลับมาที่ไฟล์ downloaded-client-config.ovpn อีกครั้ง แล้วเลื่อนลงมาด้านล่างสุด แล้ว Enter เพื่อเพิ่มไป 2 บรรทัดและพิมพ์ tag code ตามด้านล่างนี้
แล้ววาง BEGIN PRIVATE KEY ที่คัดลอกมาลงใน tag code ก็จะได้ตามรูปภาพด้านล่าง แล้วทำการ Save (* เมื่อวางไปแล้วอย่าให้มีบรรทัดว่าง เพราะจะทำให้โปรแกรมไม่ทำงาน)

<key>
</key>

ตรวจสอบ State ใน Client VPN endpoints

กลับไปที่หน้าจอ Client VPN endpoints แล้วตรวจสอบ State ตามนี้

ตรวจสอบ Client VPN endpoint ของเรา เช่น
・Name: tinnakorn-cvpn-test | State: Available
・แท็บ Target network associations | State: Associated

・แท็บ Authorization rules | State: Active

เมื่อ State เป็นเหมือนกับรูปภาพด้านบนทั้งหมดแล้ว ให้เริ่มทำขั้นตอนถัดไป

การใช้งาน AWS Client VPN

โปรแกรม AWS VPN Client ที่ติดตั้งเมื่อสักครู่จะแสดง Icon บนหน้าจอ Desktop แบบนี้ ให้คลิกเปิดโปรแกรมขึ้นมา

แล้วโปรแกรมจะแสดงหน้าจอแบบนี้ ให้เลือก Connection

จะเห็นว่า Connection Details แสดงเป็น Disconnected ซึ่งเราต้องเพิ่ม Profile ก่อนจึงจะสามารถเชื่อมต่อได้

เลือก File เลือก Manage Profiles

คลิก Add Profile

ป้อน Display Name ตามต้องการและ Browse VPN Configuration File (downloaded-client-config.ovpn) ที่เตรียมไว้ก่อนหน้านี้ แล้วคลิก Add Profile

คลิก Done

เลือก Profile ที่เราเพิ่ม แล้วคลิก Connect แล้วรอการเชื่อมต่อสักครู่

เมื่อเชื่อมต่อสำเร็จ ข้อมูลการเชื่อมต่อจะแสดงใน Connection Details แบบนี้

เมื่อทำขั้นตอนในส่วนของ VPC เสร็จทั้งหมดแล้ว เราจะใช้การตั้งค่าของ VPC นี้ไปใช้กับ EC2 Instance ในขั้นตอนถัดไป

การสร้าง Windows Server ใน Amazon EC2

ครั้งนี้จะสร้าง Microsoft Windows Server 2022 Base ขึ้นมาเพื่อทดสอบการใช้งานกับ Client VPN ตามแผนภาพด้านล่างนี้
ซึ่งเราสามารถใช้ EC2 Instance อะไรก็ได้ แต่เนื่องจาก Windows Server เป็นการแนะนำที่ทำให้เข้าใจได้ง่ายกว่า ดังนั้นการแนะนำครั้งนี้จึงเลือกเป็น Windows Server

ดูตัวอย่างการสร้าง Windows Server ได้ที่ลิงก์บทความด้านล่างนี้ (ลิงก์บทความด้านล่างนี้เป็นแค่ตัวอย่าง ซึ่งมีการตั้งค่าบางส่วนที่แตกต่างกัน แนะนำให้ดู ตัวอย่างตั้งค่าการสร้าง Windows Server ของบทความครั้งนี้ ประกอบด้วย)

ตัวอย่างตั้งค่าการสร้าง Windows Server ใน EC2 ของบทความครั้งนี้

※Create Key pairs
Key pairs: tinnakorn-cvpn-test (ตั้งชื่อที่ต้องการ)
Key pair type: RSA
Private key file format: ◉ .pem

※Launch instances
Name and tags
Name: tinnakorn-cvpn-test (ตั้งชื่อที่ต้องการ)

Application and OS Images (Amazon Machine Image)
Amazon Machine Image (AMI): Microsoft Windows Server 2022 Base

Instance type
Instance type: t3a.medium

Key pair (login)
Key pair name - required: tinnakorn-cvpn-test (key pair ที่สร้างขึ้นเมื่อสักครู่นี้)

Network settings
VPC: tinnakorn-cvpn-test-vpc (VPC ที่สร้างขึ้นตอนแรก)
Subnet: tinnakorn-cvpn-test-subnet-private1-ap-southeast-1a (Subnet ที่ได้มาจากการสร้าง VPC ตอนแรก)
Auto-assign public IP: Disable (Default)

Firewall (security groups)
Security group name - required: tinnakorn-cvpn-test (ตั้งชื่อที่ต้องการ)
Description - required: tinnakorn-cvpn-test (ป้อนตามต้องการ)
Inbound security groups rules
▼ Security group rule 1 (TCP, 3389, xxx.xxx.xxx.xxx/32)
Type: rdp | Source type: Custom | Source: 10.0.0.0/16 (การตั้งค่านี้จะทำให้มีแค่การเชื่อมต่อที่มาจากภายใน VPC เท่านั้น ที่สามารถเชื่อมต่อเข้ามาได้ และเนื่องจากการเชื่อมต่อจาก Client VPN จะถูกแปลงเป็นการเชื่อมต่อที่มาจากภายใน VPC ดังนั้นการตั้งค่านี้จึงทำให้สามารถเชื่อมต่อจาก Client VPN ได้ด้วย)

Configure storage
1x: 30 GiB gp2 Root volume (Default)

ตรวจสอบ Private IPv4 ใน EC2 Instance

เข้ามาที่หน้าจอ EC2 Instance (Windows Server) ของเรา จะเห็นว่ามี Private IPv4 addresses: 10.0.0.xxx อยู่ แต่ไม่มี Public IPv4 DNS

ตรวจสอบการเข้าถึง Internet ใน Windows Server

เมื่อเราเปิดใช้งาน Client VPN แล้ว IP เราจะถูกเปลี่ยนเป็น IP ที่อยู่ใน Client CIDR (10.2.0.0/22) ซึ่งจะอยู่ในระยะของการตั้งค่า Inbound ของ Security Group ที่ตั้งค่าไว้ (10.0.0.0/16) ทำให้เราสามารถเข้าเชื่อมต่อ EC2 Instance Windows Server ที่เราสร้างขึ้นใน Private Subnet ได้ และเมื่อเราเข้าไปเปิดเบราว์เซอร์ที่อยู่ใน EC2 Instance Windows Server จะเห็นได้ว่าเราจะไม่สามารถใช้งานเว็บไซต์ต่างๆได้ เนื่องจากไม่มีทางออกไป Internet gateway นั่นเอง

การลบ AWS Resource ที่สร้างขึ้นในบทความนี้

การลบฟังก์ชันใน Service ต่างๆ ถ้าเราไม่รู้ลำดับของการลบฟังก์ชันก็จะทำให้เกิด Error ในขณะลบ หรือไม่สามารถลบได้นั่นเอง เช่น ถ้าทำการลบ Security Group ที่กำลังเชื่อมต่ออยู่กับ EC2 โดยที่ไม่ Terminate EC2 ก่อน ก็จะไม่สามารถลบ Security Group ได้ เป็นต้น ดังนั้นแนะนำให้ลบตามขั้นตอนที่ผมเรียงไว้ได้เลยครับ

การลบฟังก์ชันที่ได้สร้างขึ้นใน Service ต่างๆ

ลบเรียงตามขั้นตอนดังนี้:

  • Amazon EC2
    • Instance
    • Key Pairs
  • Amazon VPC
    • Target network associations
    • Client VPN endpoints
    • Your VPCs
  • AWS Certificate Manager (ACM)
    • Certificates
  • AWS CloudShell
    • ลบโฟลเดอร์ custom_folder ที่สร้างด้วยการ Run Command

การลบ Amazon EC2 (Instance, Key Pair)

เข้ามาที่หน้าจอ Service EC2 แล้วทำการลบ

※Instance
เลือก Instance จากเมนูด้านซ้าย เลือก Instance ที่ต้องการลบ แล้วเลือก Instance state แล้วเลือก Terminate instance
และเมื่อมี POPUP แสดงขึ้นมา ให้ยืนยันการลบตามคำแนะนำ
(การ Terminate Instance ต้องรอจนกว่า Instance state เป็น Terminated จึงจะเสร็จสมบูรณ์)

※Key Pair
เลือก Key Pair จากเมนูด้านซ้าย เลือก Actions แล้วเลือก Delete
และเมื่อมี POPUP แสดงขึ้นมา ให้ยืนยันการลบตามคำแนะนำ

การลบ Amazon VPC

เข้ามาที่หน้าจอ Service VPC » Client VPN endpoints แล้วทำการลบ

※Target network associations
เลือก Client VPN endpoints จากเมนูด้านซ้าย เลือก Client VPN endpoints ที่ต้องการลบ แล้วเลือกแท็บ Target network associations แล้วเลือก Association ID แล้วคลิก Disassociate
และเมื่อมี POPUP แสดงขึ้นมา ให้ยืนยันการลบตามคำแนะนำ

※Client VPN endpoints
เลือก Actions แล้วเลือก Delete client VPN endpoint
และเมื่อมี POPUP แสดงขึ้นมา ให้ยืนยันการลบตามคำแนะนำ

※Your VPCs
เลือก Your VPCs จากเมนูด้านซ้าย เลือก VPC ที่ต้องการลบ แล้วเลือก Actions แล้วเลือก Delete VPC
และเมื่อมี POPUP แสดงขึ้นมา ให้ยืนยันการลบตามคำแนะนำ

การลบ AWS Certificate Manager

เข้ามาที่หน้าจอ Service ACM » List certificates แล้วทำการลบ

※Certificates
เลือก List certificates จากเมนูด้านซ้าย เลือก Certificate ID ที่ต้องการลบ แล้วเลือก Delete
และเมื่อมี POPUP แสดงขึ้นมา ให้ยืนยันการลบตามคำแนะนำ

การลบไฟล์ใน AWS CloudShell

การลบขั้นตอนนี้จะลบโดยการ Run Command
ให้เปิด AWS CloudShell ขึ้นมา แล้วรันคำสั่งตามนี้

※AWS CloudShell
รันคำสั่งนี้เพื่อกลับมาที่ /home/cloudshell-user

cd ~


แล้วรันคำสั่งนี้เพื่อตรวจสอบไฟล์

ll

Output (example): จะเห็นว่ามีโฟลเดอร์ easy-rsa และ custom_folder

AWS CloudShell (ap-southeast-2)

[cloudshell-user@ip-xx-xx-xx-xx ~]$ ll
total 8
drwxrwxr-x  2 cloudshell-user cloudshell-user 4096 May 16 08:01 custom_folder
drwxrwxr-x 10 cloudshell-user cloudshell-user 4096 May 16 07:52 easy-rsa
[cloudshell-user@ip-xx-xx-xx-xx ~]$


แล้วรันคำสั่งตามนี้เพื่อลบโฟลเดอร์ easy-rsa และ custom_folder

rm -rf easy-rsa/ custom_folder/


แล้วตรวจสอบด้วยคำสั่งด้านล่างนี้อีกครั้ง

ll

Output (example): จะเห็นว่าโฟลเดอร์ easy-rsa และ custom_folder ถูกลบไปแล้ว

AWS CloudShell (ap-southeast-2)

[cloudshell-user@ip-xx-xx-xx-xx ~]$ ll
total 0
[cloudshell-user@ip-xx-xx-xx-xx ~]$

การสาธิตการใช้งาน Client VPN ก็จบเพียงเท่านี้ครับ

ราคา AWS VPN

  • AWS Client VPN endpoint association $0.15 per hour
    นี่คือค่าบริการที่ต้องจ่ายเมื่อสร้าง Client VPN และโปรดทราบว่าถึงแม้จะไม่มีการเชื่อมต่อจาก Client VPN แต่ก็ยังมีค่าใช้จ่าย
  • AWS Client VPN connection $0.05 per hour
    นี่คือค่าบริการสำหรับเวลาที่มีการเชื่อมต่อจาก Client VPN ซึ่งเราสามารถลดค่าบริการได้โดยลดระยะเวลาการเชื่อมต่อจาก Client VPN

ตรวจสอบราคา AWS VPN เพิ่มเติมได้ที่ลิงก์ด้านล่างนี้ครับ
ราคา AWS VPN

สรุป

เราสามารถเข้าถึง EC2 Instance ภายใน VPC ผ่าน Client VPN จาก Client PC ของเราได้ แต่เราไม่สามารถเข้าถึงจาก EC2 Instance ภายนอก VPC ได้ ดังนั้นการใช้งาน AWS Client VPN จะทำให้มีความปลอดภัยมากยิ่งขึ้น

ผมหวังว่าบทความนี้จะเป็นประโยชน์ให้กับผู้อ่านได้นะครับ

POP จากบริษัท Classmethod (Thailand) ครับ !

บทความที่เกี่ยวข้อง