Rút ngắn thời gian khởi tạo: golden AMI và user data
Khi tải tăng, instance mới phải sẵn sàng trong vài phút; nếu mỗi instance phải cài đặt lại từ đầu thì Auto Scaling group phản ứng quá chậm.
Quá trình cài đặt đầy đủ một EC2 instance của MFTECH mất 45 phút, trong khi Auto Scaling yêu cầu instance mới sẵn sàng trong vài phút.
Golden AMI có thể được tái sử dụng ở phạm vi rộng hơn. Khi MFTECH mở rộng sang Region mới, copy AMI sang Region đó. Khi nhóm khác cần sử dụng, share AMI với account của nhóm đó. AMI tạo từ snapshot đã mã hoá thì bản copy cũng phải được mã hoá; không thể copy thành bản không mã hoá.
Bên trong instance, script khởi động thường cần biết instance ID, AZ hoặc địa chỉ IP của chính nó. Các thông tin này lấy từ instance metadata: dữ liệu về instance, chia theo category (host name, security group, events…), đọc tại địa chỉ 169.254.169.254 (IMDSv2 yêu cầu lấy token trước). Instance metadata cũng trả về user data đã truyền khi launch. Metadata là dữ liệu để đọc, không phải cơ chế chạy script. Metadata và user data chỉ đọc được từ bên trong instance nhưng không được bảo vệ bằng xác thực hay mã hoá, nên không lưu mật khẩu trong đó.
Chọn từng nhu cầu của đội vận hành để xem cơ chế phù hợp:
Theo mặc định, user data script và cloud-init directive chỉ chạy trong chu kỳ boot của lần launch đầu tiên, với quyền root. Không cần cấu hình thêm; chỉ khi muốn chạy ở mọi lần khởi động mới phải đổi cấu hình.
- Rút ngắn thời gian khởi tạo: golden AMI cho phần cố định + user data cho phần thay đổi.
- AMI có thể copy sang Region khác và share với account khác.
- Thông tin về chính instance (ID, IP, AZ): instance metadata, không dùng để chạy script.
- Duy trì trạng thái RAM khi dừng instance: Hibernate (phần 4).
Shortening launch time: golden AMI and user data
When load rises, a new instance must be ready within minutes; if every instance has to be installed from scratch, the Auto Scaling group reacts far too slowly.
A full installation of a MFTECH EC2 instance takes 45 minutes, while Auto Scaling needs a new instance ready within a few minutes.
A golden AMI can be reused more widely. When MFTECH expands to a new Region, copy the AMI to that Region. When another team needs it, share the AMI with that team’s account. An AMI created from encrypted snapshots must also be encrypted when copied; it cannot be copied into an unencrypted copy.
Inside the instance, a startup script often needs to know its own instance ID, AZ or IP address. It reads them from instance metadata: data about the instance, divided into categories (host name, security groups, events…), retrieved at 169.254.169.254 (IMDSv2 requires a session token first). Instance metadata also returns the user data passed at launch. Metadata is data to read, not a way to run scripts. Metadata and user data can only be accessed from within the instance, but they are not protected by authentication or encryption, so do not store passwords in them.
Pick each need of the operations team to see which mechanism fits:
By default, user data scripts and cloud-init directives run only during the boot cycle of the first launch, as root. No extra configuration is needed; you only change the configuration if you want them to run on every boot.
- Shorter launch time: a golden AMI for the fixed part + user data for the changing part.
- An AMI can be copied to another Region and shared with another account.
- Information about the instance itself (ID, IP, AZ): instance metadata, which does not run scripts.
- Keep the RAM state when stopping an instance: Hibernate (part 4).