You are on page 1of 71

INTRODUCTION

Introduction of Amazon VPC


• Amazon Virtual Private Cloud (Amazon VPC) là “Virtual Private Cloud” là một mạng ảo tùy chỉnh nằm bên trong Đám mây AWS và được cách
ly với toàn bộ thế giới bên ngoài. Khái niệm này tương tự như việc thiết kế và triển khai một môi trường mạng độc lập riêng biệt trong một
trung tâm dữ liệu tại chỗ.
• Bên trong VPC ta có thể tùy chỉnh, toàn quyền thiết lập môi trường mạng ảo của mình, nghĩa là cả khả năng khởi tạo và chạy tài nguyên AWS
cũng như khả năng chọn dải địa chỉ IP, tạo mạng con và định cấu hình bảng định tuyến cũng như cổng mạng. (routing tables and network
gateways). Bạn có thể sử dụng cả IPv4 và IPv6 để truy cập an toàn và dễ dàng vào các tài nguyên và ứng dụng trong VPC.
• Region là khái niệm mô tả nhiều cụm trung tâm dữ liệu AWS cực lớn nằm trên một lãnh thổ nhất định. Trong một Region, bạn có thể tạo nhiều
VPC và mỗi VPC được phân biệt bằng các dải không gian địa chỉ IP khác nhau. Bạn chỉ định dải địa chỉ IPv4 bằng cách chọn Classless Inter-
Domain Routing (CIDR), chẳng hạn như 10.0.0.0/16.
• Không thể thay đổi dải địa chỉ Amazon VPC sau khi đã tạo.
• Phạm vi địa chỉ Amazon VPC có thể lớn bằng /16 (tức là 65536 địa chỉ khả dụng) hoặc nhỏ bằng /28 (tức là 16 địa chỉ khả dụng) và chúng
không được trùng lặp với bất kỳ mạng nào khác mà chúng sẽ được kết nối.
• Subnets
• Subnet là một phân đoạn của dải địa chỉ IP mà bạn sử dụng khi cung cấp Amazon VPC, mạng này trực tiếp cung cấp dải mạng đang hoạt động cho các tài nguyên
AWS có thể chạy trong đó, chẳng hạn như Amazon EC2, Amazon RDS (Cơ sở dữ liệu mối quan hệ của Amazon). ),…
• Các mạng con cũng được xác định thông qua các khối CIDR (ví dụ: 10.0.1.0/24 và 192.168.0.0/24) và CIDR của mạng con phải nằm trong CIDR của VPC.
• Mạng con nhỏ nhất có thể được tạo là /28 (16 địa chỉ IP). AWS lưu trữ 4 địa chỉ IP đầu tiên và 1 địa chỉ IP cuối cùng của mỗi mạng con cho mục đích kết nối mạng
nội bộ. Ví dụ: mạng con /28 có 16 địa chỉ IP khả dụng, nhưng loại bỏ 5 IP dành riêng cho AWS, để lại 11 địa chỉ IP khả dụng cho các tài nguyên hoạt động trong
mạng con này.
• Availability Zone hay viết tắt là AZ là một hoặc nhiều trung tâm dữ liệu, nằm trong Khu vực và được xác định dựa trên vị trí địa lý. Trong một Availability Zone có
thể có một hoặc nhiều subnet, nhưng một subnet chỉ có thể nằm trong một Availability Zone duy nhất và không thể mở rộng sang các Availability Zone .
• Subnets được chia thành các danh mục như Public, Private, or VPN-only. Public subnet là một subnet với 1 bảng định tuyến (route table) hướng lưu lượng truy
cập trong subnet đến VPC IGW (VPC Internet gateway).
• Private subnet thì ngược lại với Public subnet, không có bảng định tuyến hướng lưu lượng truy cập đến VPC IGW. VPN-only subnet là mạng con có bảng định
tuyến hướng lưu lượng truy cập đến VPG của Amazon VPC.
• Route Table
• The Route Table, còn được gọi là bảng định tuyến, cung cấp hướng dẫn định tuyến và được gán cho các Subnets. Ví dụ khi bạn tạo VPC có lớp mạng 10.10.0.0/16,
với 2 mạng con 10.10.1.0/24,10.10.2.0/24 thì mỗi mạng con mặc định sẽ được gán một route table mặc định.
• Bên trong route table sẽ có mục đích của tuyến đường:10.10.0.0/16 target:local.
• Internet Gateway
• The Internet Gateway (IGW) là một thành phần của Amazon VPC cho phép các tài nguyên trong VPC, cụ thể là EC2, giao tiếp với Internet.
• IGW có tính dự phòng và tính sẵn sàng cao.
• Nó hoạt động như một mục tiêu trong bảng định tuyến của Amazon VPC, giúp lưu lượng được định tuyến bên ngoài Internet bằng cách dịch địa chỉ
mạng của EC2 thành địa chỉ IP Công cộng mà nó đã được chỉ định.
• Cụ thể hơn, EC2 Instances bên trong VPC chỉ biết các địa chỉ IP Private được gán cho nó, nhưng khi có lưu lượng được gửi từ EC2 ra ngoài Internet, IGW
sẽ dịch địa chỉ IP Private đó thành địa chỉ IP Public (hoặc địa chỉ EIP) được gán cho EC2 và duy trì ánh xạ 1-1 cho đến khi địa chỉ Public IP address được
giải phóng.
• Khi EC2 nhận được lưu lượng truy cập từ bên ngoài Internet, IGW sẽ dịch Target address (Public IP address) thành địa chỉ Private IP của phiên bản EC2
và chuyển tiếp lưu lượng đến Amazon VPC.
• NAT Gateway
• Theo mặc định, bất kỳ EC2 nào chạy bên trong Private subnet sẽ không thể giao tiếp với Internet thông qua IGW. Từ đó, vấn đề phát sinh khi EC2 đó cần
ra ngoài Internet để áp dụng các bản cập nhật bảo mật, tải xuống các bản vá hoặc cập nhật phần mềm ứng dụng. Hiểu được nhu cầu đó, AWS cung cấp
2 phương thức cho phép các EC2 bên trong Private subnet có quyền truy cập Internet, đó là NAT Instance và NAT Gateway.
• Đối với các trường hợp phổ biến, chúng ta nên sử dụng NAT Gateway thay vì NAT Instance. NAT Gateway đảm bảo tính khả dụng và băng thông cao hơn,
đồng thời yêu cầu ít nỗ lực quản trị hơn so với NAT Instance.
• Để tạo một cổng NAT, bạn phải chỉ định một subnet (public) và một Elastic IP address. Đảm bảo rằng Elastic IP address không được liên kết với bất kỳ
giao diện Mạng hoặc Trường hợp nào khác.
• Network ACLs cho VPC
• Danh sách kiểm soát truy cập mạng (ACL) là lớp bảo mật tùy chọn cho VPC, nó hoạt động như một tường lửa để kiểm soát lưu lượng ra và vào
cho một hoặc nhiều subnet. Ta có thể thiết lập network ACL với các rule tương tự như security groups, nhằm bổ sung thêm một lớp bảo mật
nữa cho VPC.
• Cơ bản về Network ACL
• VPC sau khi khởi tạo sẽ có sẵn một network ACL mặc định và có thể được sửa đổi. Mặc định, nó cấp phép truy cập cho tất cả lưu lượng truy
cập IPv4 hoặc IPv6 có thể đi ra hoặc đi vào VPC.
• Có thể tạo một network ACL tùy chỉnh và liên kết nó với một subnet. Mặc định các network ACL tùy chỉnh từ chối tất cả lưu lượng truy cập đi
vào và đi ra, cho đến khi ta bổ sung rule cấp phép truy cập.
• Từng subnet trong VPC phải được liên kết với một network ACL. Nếu subnet không được liên kết với một network ACL cụ thể thì subnet sẽ
được tự động liên kết với network ACL mặc định.
• Một network ACL có thể liên kết với nhiều subnet. Tuy nhiên, một subnet chỉ có thể liên kết với một network ACL tại một thời điểm. Khi liên kết
network ACL mới với subnet,thì liên kết trước đó sẽ bị xóa.
• Một network ACL chứa một danh sách các rule được đánh số khác nhau. AWS đánh giá các rule dựa trên số thứ tự được gán, bắt đầu với rule
được đánh số thấp nhất, để xác định xem lưu lượng có được phép đi vào hay đi ra khỏi bất kỳ subnet nào được liên kết với network ACL hay
không. Số thứ tự lớn nhất có thể được gán cho rule là 32766 (tương đương số lượng rule lớn nhất của một network ACL là 32766).
• Network ACL có các rule cấp phép đi vào hoặc đi ra tách biệt và rule có thể là cho phép hoặc từ chối lưu lượng.
• Network ACL là dịch vụ Stateless, nghĩa là phản hồi đối với lưu lượng truy cập được phép đi vào, phải tuân theo rule đối với lưu lượng truy cập
đi ra (và ngược lại).
• Security group
Nó giống như một firewall ảo cho instance. Khi traffic vào instance thì sẽ được kiểm tra xem có nằm trong những rule cho phép của Security
group không để cho phép đi qua.
• Network ACL
Hoạt động như một firewall cho subnet. Khi trafic đi ra và vào subnet thì sẽ được kiểm tra theo rule của Network ACL. Rule được đánh số và ưu
tiên từ thấp đến cao.
• Create VPC
• Access the AWS Management Console interface
• Find VPC
• Select VPC
• In the VPC interface
• Select Yours VPC
• Select Create VPC
3.Follow the steps to create a VPC
• Resource, select VPC only
• Name tag, enter ASG
• IPv4 CIDR, enter 10.10.0.0/16
•Select Create VPC
5.Finish creating VPC
• View the details of the newly created VPC. Check if not Enable DNS resolution and DNS Hostname
• Go to Edit VPC settings
• Select DNS settings
• Select and Save.
• Create subnet
• In the VPC interface
• Select Subnets
• Select Create subnet
• In the Create subnet interface
Implement subnet settings
• Select ASG VPC Subnet name, enter Public Subnet 1
Select AZ ap-southeast-1a
IPv4 CIDR block, import 10.10.1.0/24 according to architecture description
Select Create subnet
• Finish creating subnet
• Follow the same steps to create more subnets
• Public subnet 2 with CIDR of 10.10.2.0/24 located in Availability Zone ap-southeast-1b.
• Private subnet 1 with CIDR of 10.10.3.0/24 located in Availability Zone ap-southeast-1a.
• Private subnet 2 with CIDR of 10.10.4.0/24 located in Availability Zone ap-southeast-1b
• Bạn có thể thấy có 2 cột Availability Zone và Availability Zone ID. Để tránh tài nguyên EC2 được sử dụng không đồng đều, (ví dụ: chúng ta có xu hướng sử dụng AZ
1a để chạy chính và AZ 1b để dự phòng) AWS sẽ chỉ định ngẫu nhiên * Availability Zone into Availability Zone ID*.
• Ta có thể hiểu Availability Zone là bí danh, còn Availability Zone ID là mã định danh. Ví dụ: trong hình, Availability Zone ap-southeast-1a được chỉ định Availability
Zone ID apse1-az2. Trong một tài khoản AWS khác, Availability Zone ap-southeast-1a có thể có Availability Zone ID là apse1-az1.

• NOTE : Một điểm đáng lưu ý nữa là các subnet đều giống nhau, thông qua việc cấu hình bảng định tuyến và cấp phát địa chỉ IP public mà chúng ta mới chia
thành Public và Private Subnet.
• In the VPC interface
• Select Subnets
• Select Public Subnet 1 & 2
• Select Actions
• Select Edit subnet settings
• Create an Internet Gateway
• In the VPC interface
• Select Internet Gateways
• Select Create internet gateway
• Make a configuration
• Name tag, enter Internet Gateway
• Select Create internet gateway
• Implement Attach VPC
• Select Actions
• Select Attach to VPC
• Select ASG, VPC ID will be automatically filled in.
• Select Attach internet gateway
• Create Route Table to route outbound internet through Internet Gateway.
• In the VPC interface
• Select Route Tables
• Select Create route table

Configure Route table VPC, select ASG VPC.


VPC id will be automatically filled in.
Select Create route table
Name, enter Route table-Public
• Execute Edit route
• Select Actions
• Select Edit routes

•Select Add route


•Fill in the Destination CIDR : 0.0.0.0/0 representing the Internet.
•In the Target section select Internet Gateway, then select the Internet Gateway we created.
•Internet Gateway ID will be automatically filled in.
•Select Save changes
• Make sure Route table - Public is selected.
• Select subnet associations
• Select Edit subnet associations
• Make sure Route table - Public is selected.
• Select subnet associations
• Select Edit subnet associations
• Create Security Group for servers located in Public subnet
• In the VPC interface
• Select Security Group
• Select Cretae security group
Configure Security group
Security Group name, enter Public subnet – SG
Description, enter Allow SSH and Ping for servers in public subnet.
Select ASG VPC

Configure Inbound rules


In Inbound rules, click Add rule.
Select Type: SSH and Source: My IP. My IP represents 1 public IPv4 address you are using (will change when you
change network)
Select Add rule to add a new rule.
Select Type: All ICMP - IPv4 and Source: Anywhere. Allow ping from any IP address.
• Create a Security Group for a server located in a Private subnet
• In the VPC interface
• Select Security Groups
• Select Create security group
• Configure Security group
• In the Security group name field enter Private subnet - SG
• In the Description section enter Allow SSH and Ping for servers in private subnet.
• select VPC, select VPC named ASG.

• Configure Inbound rules


• In Inbound rules, select Add rule.
• Select Type: SSH and leave Source: Custom. Check the search box and select Public subnet SG.
• This option allows all servers assigned Public subnet SG to be SSH to the servers assigned to Private subnet SG. **.
• Create EC2 server
• In this step, we will create 2 EC2 servers ( EC2 instances ) like the architecture below.
• Create EC2 in the Public subnet
1. Access AWS Management Console
- Find EC2
- Select EC2
2. In the EC2 interface
- Select Instances
- Select Launch instances
3. Name and tags of the instance, enter EC2 Public

4. xecuting AMI Selection


- Select Quick Start
- Select Amazon Linux 2
- Select AMI
- Select Instance type and Select Create new key pair : aws-keypair

5. Configure Network
- VPC, select ASG
- Subnet, select Public Subnet 1
- Auto-assign public IP, select Enable
- Firewall (Security Group), select Select existing security group
- Select Public subnet -SG
- Select Launch instance
Create EC2 in a Private subnet
1. In the EC2 interface
- Select Instances
- Select Launch instances
2 .Name and tags, enter EC2 Private
3. Make instance type selection. Key pair name, select aws-keypair

4. Configure Network
- VPC, select ASG vpc
- Subnet, select Private subnet 2, SG choose Private subnet - SG
- Auto-assign public IP, select Disable. If not Disable, you need to check the configuration automatically allocate public IP for the subnet.
• TEST CONNECTION
• Go to the EC2 page
• Select Instances
• Select EC2 Public
• Select Details
• View Public IPv4 address
• Using Mobaxterm, putty… to connect EC2

• Check the connection to the internet of EC2 Public, we execute the command: ping amazon.com -c5
• Make a ping to EC2 private : ping <IP Private EC2 Private>

• Connect to the EC2 Private server and check the internet connection.

• Copy aws-keypair.pem to EC2 Public


Run this command, if necessary, to ensure your key is not
publicly viewable.
chmod 400 aws-keypair.pem
Connect to your instance using its Private IP:
10.10.4.135

ssh -i "aws-keypair.pem" ec2-user@10.10.4.135


• CREATE NAT GATEWAY
• Create NAT Gateway
• Generate an Elastic IP address
1. Access EC2
1. Select Elastic IPs
2. Select Allocate Elastic IP address
2. In the Allocate Elastic IP address interface
2. Public IPv4 address pool, select Amazon’s pool of IPv4 addresses
3. Select Allocate
3. We have successfully created a Public IP Address

4. Access to VPC
5. Select NAT Gateways
6. Create NAT gateway
7. In NAT gateway interface

8. Name : NAT gateway


9. Subnet, select Public subnet 2

10. Connectivity type, select Public

11. Elastic IP allocation ID, select Elastic IP just created.


• Create Route table - Private and assign to private subnets.
In the VPC interface
Select Route Tables
Select Create route table
In the Route table interface
Name : Route table – Private
VPC, select ASG vpc
Select Create route table
In the Route table - Private interface
Select Subnet Associations
Select Edit subnet associations

Edit routes
• Test ping amazon.com successfully from EC2 Private.
• ping amazon.com -c5
• Check Resource MAP
• USING REACHABILITY ANALYZER
• Using Reachability Analyzer
Access to VPC interface
Search Reachability Analyzer
Select Create and analyze path
• Implement Path Configuration
• Name tag, enter EC2 private with EC2 Public
• For Source type, select Instance
• Select source as EC2 Public
• For Destination type, select Instance
• For Destination, select EC2 Private
• The remaining parameters are left to default.
• Select Create and analyze path
• CONFIGURING SITE TO SITE VPN
• Chúng ta có thể kết nối trung tâm dữ liệu On-premise với Amazon VPC bằng VPN cứng hoặc mềm tùy theo mục đích và nhu cầu sử dụng thực tế. Để thiết lập kết
nối VPN Site-to-Site, chúng tôi sẽ cần tạo và định cấu hình VPG Cổng riêng ảo và Cổng khách hàng CGW.
• Virtual Private Gateway (VPG) là trung tâm điều khiển kết nối virtual private network (VPN) được cài đặt trên AWS.
• A Customer Gateway (CGW) là thành phần đại diện cho thiết bị VPN cứng hoặc mềm được cài đặt ở đầu Client end.
• VPN tunnel sẽ được thiết lập ngay khi lưu lượng dữ liệu được truyền giữa AWS và mạng của khách hàng.
• Trong kết nối đó, bạn phải chỉ định loại định tuyến sẽ được sử dụng để đảm bảo an toàn và chất lượng truyền dữ liệu.
• Nếu CGW ở phía Client end hỗ trợ Border Gateway Protocol (BGP), thì trong cấu hình kết nối VPN, chúng tôi được yêu cầu đặt định tuyến thành định tuyến động.
Nếu không, chúng ta phải định cấu hình định tuyến kết nối là định tuyến tĩnh. Trong trường hợp sử dụng định tuyến tĩnh, bạn phải nhập chính xác các tuyến cần
thiết cho kết nối từ phía Máy khách đến VPG được thiết lập ở đầu AWS.
• (Muốn tìm hiểu BGP là gì thì xem link https://quantrimang.com/cong-nghe/border-gateway-protocol-bgp-178019)
• Amazon VPC cung cấp nhiều loại CGW và mỗi CGW được gán cho một VPG, nhưng 1 VPG có thể được liên kết với nhiều CGW (thiết kế nhiều-một). Để hỗ trợ mô
hình này, địa chỉ IP của CGW phải là duy nhất trong một khu vực. Amazon VPC cũng cung cấp thông tin cần thiết để Quản trị viên mạng có thể định cấu hình CGW
và thiết lập kết nối VPN với VPG trên AWS. Kết nối VPN luôn bao gồm 2 Internet Protocol Security (IPSec) để đảm bảo tính sẵn sàng cao của kết nối. Dưới đây là các
tính năng quan trọng mà chúng ta cần biết về VPG, CGW và VPN:
• VPG là thành phần đầu cuối của đường hầm VPN nằm trên AWS.
• CGW có thể là một thiết bị phần cứng hoặc một ứng dụng phần mềm nằm ở đầu Client end trong kết nối VPN tunnel.
CREATE VPC FOR VPN
Create a VPN environment
Access VPC interface
Select Yours VPC
Select Create VPC
In the Create VPC interface

Resource, select VPC only

Name : ASG VPN

IPv4 CIDR block, enter : 10.11.0.0/16


• Create Subnets
• VPC : ASG VPN
• Name : VPN Public
• AZ : ap-southeast-1a
• IPv4 CIDR : 10.11.1.0/24
• Actions
• Edit subnet settings
• Enable auto-assign public IPv4 address

• Save
In the VPC interface
Select Internet Gateway
Select Create internet gateway
Create internet gateway
Name : Internet Gateway VPN
Actions
Attact to VPC
Select VPC ASG VPN
• Create Route Table
• Name : Route table VPN – Public
• VPC : ASG VPN
• Edit route
• Add route :
- Fill in the Destination CIDR : 0.0.0.0/0
representing the Internet.
- In the Target section select Internet Gateway,
then select the Internet Gateway VPN we created
• Select Subnet associations
• Select Edit subnet associations
• Select subnet VPN Public.
• Select Save associations
• Create SG in VPC interface
• Name : VPN Public –SG
• Description : Allow IPSec, SSH, and Ping for servers in public subnet.
• VPC, select ASG VPN vpc
• Create EC2
• Name : Customer Gateway instance
• AMI : same every EC2 are created before
• Network
• CONFIGURING VPN CONNECTION
• In this step we will proceed to create Virtual Private Gateway, Customer
• Gateway and VPN Site to Site connection.
• Create Virtual Private Gateway
• Access to VPC
• Select Virtual Private Gateway
• Select Create Virtual Private Gateway
• Name : VPN Gateway
• Actions
• Attach to VPC
• Select VPC ASG
• CREATE CUSTOMER GATEWAY
• Select Customer Gateways
• Select Create Customer Gateway
• Name : Customer Gateway
• IP address, enter public IP address of the server EC2 Customer Gateway.
• CREATE VPN CONNECTION
• Select Site-to-Site VPN Connections
• Select Create VPN Connection

• Name : VPN Connection


• Target Gateway Type: Select Virtual Private Gateway
• Virtual Private Gateway: Select VPN Gateway
• Customer Gateway: Existing
• Customer Gateway ID: Select Customer Gateway

• Routing options : Static


• Static IP Prefixes: 10.11.0.0/16
• Configure propagation for route tables
• In the VPC interface, select Route Tables
• Select Route table - Public
• Select Route Propagation
• Select Edit route propagation
• Similar Route Propagation for Private subnet.
• Configure Customer Gateway
• Select Site-to-Site VPN Connection
• Select VPN Connection created
• Select Download Configuration
• In the Download Configuration dialog, choose appliance that is right for you: In this exercise, we will use OpenSwan.
• Vendor: Select OpenSwan

• Platform: Select OpenSwan

• Software: Select OpenSwan 2.6.38+

• IKE version: Select ikev1

• Select Download.

• Save file config into the same folder


with keypair file
• Connect ssh to EC2 Customer Gateway.

• Install OpenSwan

• sudo su
• yum install openswan -y
• Check the configuration file /etc/ipsec.conf
• vi /etc/ipsec.conf
• Configuration file /etc/sysctl.conf
• vi /etc/sysctl.conf
• Move down to the last position in the configuration file. Press the i key to proceed with editing the file.
• Add the following configuration at the end of the configuration file.

net.ipv4.ip_forward = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0

Then to apply this configuration, run the command:

sysctl -p
• Next we will configure the file /etc/ipsec.d/aws.conf
• vi /etc/ipsec.d/aws.conf
• Since we only have 1 public IP addres for Customer Gateway, we will need to configure overlapip=yes.

• leftid: IP Public Address on the Onprem side. (Here is public IP of EC2 Customer Gateway in ASG VPN VPC) .

• right: IP Public Address on AWS VPN Tunnel side.

• leftsubnet: CIDR of Local Side Network (If there are multiple network layers, you can leave it as 0.0.0.0/0).

• rightsubnet: CIDR of Private Subnet on AWS.


• Create and configure the file etc/ipsec.d/aws.secrets Create a new file with the following configuration to set up authentication for the 2 Tunnels.
• touch /etc/ipsec.d/aws.secrets

• vi /etc/ipsec.d/aws.secrets
• Add the following configuration to the end of the configuration file (this configuration is in step 5 of IPSEC Tunnel #1 and IPSEC Tunnel #2)

• Restart Network service & IPSEC service

• service network restart


• chkconfig ipsec on
• service ipsec start
• service ipsec status
After completing the configuration.Try to ping from the Customer Gateway server side to
the EC2 Private server. If the VPN configuration is successful you will get the result as below.

After completing the configuration.Try to ping from the EC2 Private server side to
the Customer Gateway server. If the VPN configuration is successful you will get the result as
below.
• Modify AWS VPN Tunnel
Access to VPC interface
Select Site-to-Site VPN connections
Select VPN just created.
Select Actions
Select Modify VPN tunnel options
• Go to Cloudwatch to view event logs

You might also like