Các bước chuẩn bị

Trong phần này, chúng ta sẽ chuẩn bị một EC2 instance chạy Amazon Linux 2023 làm môi trường làm việc, kết nối tới instance bằng VS Code Remote - SSH và dùng AWS CLI để chạy các lệnh CloudFormation trong suốt workshop.

Đăng nhập AWS Management Console bằng IAM user hoặc IAM role có quyền quản trị. Không sử dụng root user để thực hiện workshop.

Tạo IAM role

  1. Truy cập IAM.
    • Chọn Roles và chọn Create role.
    • Tại Trusted entity type chọn AWS service.
    • Tại Use case chọn EC2, sau đó chọn Next.

AWS CloudFormation

  1. Tìm và chọn policy AdministratorAccess. Chọn Next.

AWS CloudFormation

  1. Tại Role name, nhập IaC-Role.

AWS CloudFormation

  1. Kiểm tra lại thông tin và chọn Create role.

AWS CloudFormation

Policy AdministratorAccess chỉ được dùng để đơn giản hóa bài lab. Trong môi trường thật, hãy cấp quyền theo nguyên tắc least privilege.

Khởi tạo EC2 instance

  1. Truy cập EC2.
    • Chọn Instances và chọn Launch instances.

AWS CloudFormation

  1. Tại Name, nhập cfn-workspace-ec2.
    • Tại Application and OS Images, chọn Amazon Linux.
    • Chọn AMI Amazon Linux 2023 AMI.
    • Chọn Architecture 64-bit (x86).

AWS CloudFormation

  1. Tại Instance type, chọn t3.small.
    • Tại Key pair (login), chọn Create new key pair.
    • Nhập Key pair name cfn-workspace-key.
    • Chọn Create key pair.
    • File cfn-workspace-key.pem sẽ được tải về máy tính của bạn.

AWS CloudFormation

  1. Tại Network settings, giữ default VPC.
    • Chọn Create security group.
    • Tích chọn Allow SSH traffic from và chọn My IP.
    • Chọn Launch instance. AWS CloudFormation

Nếu địa chỉ IP public của mạng bạn đang dùng thay đổi, hãy cập nhật inbound rule SSH của security group này theo My IP mới.

  1. Tạo instance thành công
    • Đợi instance chuyển sang trạng thái Running.
    • Ghi lại Public IPv4 address.

AWS CloudFormation

  1. Chọn Actions:

    • Chọn Security
    • Chọn Modify IAM role

AWS CloudFormation

  1. Chọn IAM role IaC-Role và chọn Update IAM role.

AWS CloudFormation

Kết nối VS Code qua Remote - SSH

  1. Trên máy tính cá nhân, di chuyển file cfn-workspace-key.pem vào thư mục ~/.ssh/ (trên Windows là C:\Users\<user>\.ssh\).
    • Trên macOS hoặc Linux, giới hạn quyền của file bằng lệnh:

      chmod 400 ~/.ssh/cfn-workspace-key.pem
      

Không chia sẻ hoặc commit file .pem lên repository. AWS không lưu bản sao của private key, nếu mất file bạn sẽ phải tạo key pair mới.

  1. Mở VS Code.

    • Chọn Extensions (hoặc nhấn Ctrl + Shift + X).
    • Tìm Remote - SSH.
    • Cài đặt extension Remote - SSH của Microsoft.
  2. Mở Command Palette bằng Ctrl + Shift + P.

    • Chọn Remote-SSH: Open SSH Configuration File....

    • Chọn file config của user (ví dụ ~/.ssh/config).

    • Thêm cấu hình sau, thay <EC2_Public_IPv4> bằng Public IPv4 address

      Host cfn-workspace
        HostName <EC2_Public_IPv4>
        User ec2-user
        IdentityFile ~/.ssh/cfn-workspace-key.pem
      

AWS CloudFormation AWS CloudFormation AWS CloudFormation

Public IPv4 address của EC2 instance sẽ thay đổi mỗi khi instance được stop rồi start lại. Khi đó, hãy cập nhật giá trị HostName.

  1. Mở Command Palette.
    • Chọn Remote-SSH: Connect to Host....
    • Chọn cfn-workspace.
    • Chọn Linux.
    • Chọn Continue.
    • Sau vài giây, VS Code sẽ kết nối tới EC2 instance.

AWS CloudFormation 16. Chọn Terminal → New Terminal để mở VS Code terminal. - Kiểm tra AWS CLI đã được cài sẵn trên Amazon Linux 2023 và đang sử dụng IAM role IaC-Role:

  aws --version
  aws sts get-caller-identity

AWS CloudFormation

- Giá trị `Arn` trong kết quả phải chứa `IaC-Role`.
- Đặt region mặc định cho AWS CLI theo region của EC2 instance:

  TOKEN=$(curl -sS -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
  AWS_REGION=$(curl -sS -H "X-aws-ec2-metadata-token: ${TOKEN}" http://169.254.169.254/latest/meta-data/placement/region)
  aws configure set default.region "${AWS_REGION}"
  aws configure get region

AWS CloudFormation

  1. Cài đặt git bằng dnf:

    sudo dnf install -y git
    git --version
    

AWS CloudFormation

  1. Clone repository chứa các CloudFormation template của workshop.
    • Mỗi thư mục day-1, day-2, day-3 chứa template cho phần 3, 4 và 5.

      cd ~
      git clone https://github.com/Hieu192/CloudFormation-series.git
      cd CloudFormation-series
      ls
      

AWS CloudFormation