Tìm hiểu AWS EC2

  • September 10, 2022
  • 471

AWS EC2 là gì?

Amazon Elastic Compute Cloud (AWS EC2) là một cơ sở hạ tầng điện toán đám mây cung cấp tài nguyên máy tính ảo hoá có thể tự mở rộng quy mô để đáp ứng theo yêu cầu.

Các đặc tính của Amazon EC2

1. Scaling

Có thể tăng giảm Ram, CPU... của 1 Instance, hoặc tăng giảm số lượng instance theo nhu cầu.

2. Security
  • Có thể thiết lập rank IP Private dành riêng cho EC2.
  • Sử dụng Security Group và Network ACLS để control inbound/outbound.
  • Có thể thiết lập IPsec VPN giữa Data Center và AWS Clound.
  • Delicated Instance -> Tạo EC2 trên 1 hardware physical dành riêng cho 1 khách hàng duy nhất.
3. Cost

Chi phí của instance tính theo giờ, cấu hình của instance. Khi tạo tài khoản mới sử dụng được tính năng free tier trong vòng 1 năm.

Create AWS instance

Các bước để tạo 1 install, truy cập vào aws console, truy cập vào EC2, chọn launch instance.
Sau đó thiết lập instance name
aws ec2 name
Chọn os của instance
Aws ec2 type
Chọn cấu hình, tạo hoặc chọn key pair có sẵn sau này dùng để ssh vào instance
Aws ec2 instance type
Thiết lập networking
Aws ec2 networking
Thiết lập dung lượng, số lượng instance sẽ tạo, click button Launch instance để tạo instance.
Aws ec2 create instance

Ssh to ec2 instance

1. Connect trực tiếp từ aws console

- Chọn EC2 instance cần ssh
- Chọn Connect
Connect to ec2 install

2. Connect từ máy cá nhân

Để connect được ec2 instance từ máy tính cá nhân chúng ta cần sử dụng file pem khi tạo instance.


# Chmod file pem với giá trị 600
chmod path/file.pem 600

# Ssh command
ssh -i "path/file.pem" ec2-user@{ec2_public_IP}

Sử dụng config ssh


## Move file pem vào folder ~/.ssh
mv {file.pem} ~/.ssh/file.pem

## Chmod 600 cho file pem
chmod 600 ~/.ssh/file.pem

## Thêm config và file ~/.ssh/config
Host instance-name-example
    User ec2-user
    Port 22
    IdentityFile ~/.ssh/file.pem
    HostName {ec2_public_IP}

## Ssh vào instance chạy command
ssh instance-name-example

Install Nginx to ec2 install

Ssh vào server, cài đặt nginx cho ec2 install, loại instance là Amazon Linux 2


// Update yum
sudo yum update

// Install Nginx
sudo amazon-linux-extras list | grep nginx
sudo amazon-linux-extras enable nginx1
sudo yum -y install nginx
nginx -v

// Start nginx
sudo systemctl start nginx

// Check status nginx
sudo systemctl status nginx

Add security group to ec2 install

Mặc định Security Group cho phép mọi truy cập từ trong instance ra ngoài. Thêm security group cho phép sử dụng http truy cập vào instance.
Add security group to ec2
Truy cập vào instance sử dụng http. Truy cập vào instance thông qua public ip của instance.

Access to instance

Add IAM role to instance

Thêm IAM role cho phép instance của bạn truy cập được đến các dịch vụ s3. Tạo 1 IAM role s3FullAccess, sau đó gán IAM role vào instance của bạn.
Modify IAM role
Thêm role s3FullAccess to instance

S3 full access
Thêm IAM role s3FullAccess cho install giúp instance có quyền truy cập đến s3 mà không cần thông qua key.

Remove instance

Để xoá instance bạn chọn instance muốn xoá, sau đó Terminate instance
Remove instance


Như vậy là chúng ta đã tạo được EC2 instance, connect được vào instance, cài đặt nginx, thêm security group cho phép truy cập install thông qua http, gán được IAM role cho instance, Xoá instance. Thanks for reading...