Capstone (1): Dựng Hạ Tầng Đích Bằng CLI
Mười bốn bài qua đã dựng từng dịch vụ riêng. Capstone ghép tất cả thành một thứ hoàn chỉnh: một pipeline đưa code từ commit tới production trên một cụm co giãn, bằng blue/green, có phê duyệt và rollback. Capstone chia hai bài — bài này (15) dựng hạ tầng đích, bài sau (16) nối pipeline chạy vào nó. Tách vậy để thấy rõ hai phần: nơi ứng dụng chạy, và dây chuyền đưa nó tới đó.
💰 Chi phí
Hạ tầng này gồm một ALB và một ASG hai instance t3.micro; lúc blue/green deploy (bài 16) sẽ tạm có bốn instance. Đây là phần tốn tiền — bài 16 sẽ teardown sạch ngay sau khi chạy xong. Đừng để qua đêm.
Mục tiêu
Ráp launch template + ASG + ALB + deployment group blue/green thành môi trường đích sẵn sàng cho pipeline, hiểu các mảnh khớp nhau ra sao.
Kiến trúc đích
Internet
│ HTTP :80
┌──────▼───────┐
│ ALB │ awscicd-cap-alb (2 AZ)
└──────┬───────┘
│ target group awscicd-cap-tg
┌──────▼───────────────────┐
│ ASG awscicd-cap-asg │ 2× t3.micro, mỗi máy: httpd + CodeDeploy agent
│ (launch template) │
└───────────────────────────┘
▲
CodeDeploy DG awscicd-cap-dg (BLUE_GREEN, rollback bật)
→ khi deploy: copy ASG thành green, shift traffic qua ALB
ALB nhận traffic, phân tới các instance khỏe trong target group; ASG giữ đủ máy và thay máy chết; deployment group blue/green sẽ (ở bài 16) dựng fleet green song song và chuyển traffic qua ALB.
Launch template và ASG
Launch template là khuôn tạo instance: AMI Amazon Linux 2023, t3.micro, instance profile (đọc S3 — bài 8), security group mở HTTP, và user-data cài httpd + CodeDeploy agent:
#!/bin/bash
dnf install -y ruby wget httpd
systemctl enable --now httpd
cd /tmp && wget -q https://aws-codedeploy-ap-southeast-1.s3.ap-southeast-1.amazonaws.com/latest/install
chmod +x ./install && ./install auto
systemctl enable --now codedeploy-agent
$ aws ec2 create-launch-template --launch-template-name awscicd-cap-lt \
--launch-template-data file://lt.json
$ aws autoscaling create-auto-scaling-group --auto-scaling-group-name awscicd-cap-asg \
--launch-template "LaunchTemplateName=awscicd-cap-lt,Version=1" \
--min-size 2 --max-size 2 --desired-capacity 2 \
--vpc-zone-identifier "subnet-aaa,subnet-bbb"
ALB, target group, listener
$ aws elbv2 create-load-balancer --name awscicd-cap-alb --type application \
--subnets subnet-aaa subnet-bbb --security-groups <sg>
$ aws elbv2 create-target-group --name awscicd-cap-tg --protocol HTTP --port 80 \
--vpc-id <vpc> --health-check-path / --target-type instance
$ aws elbv2 create-listener --load-balancer-arn <alb> --protocol HTTP --port 80 \
--default-actions Type=forward,TargetGroupArn=<tg>
$ aws autoscaling attach-load-balancer-target-groups \
--auto-scaling-group-name awscicd-cap-asg --target-group-arns <tg>
Deployment group blue/green
Deployment group dưới application awscicd-demo (đã có), nhắm ASG và target group, kiểu blue/green, bật rollback khi deploy fail:
$ aws deploy create-deployment-group --application-name awscicd-demo \
--deployment-group-name awscicd-cap-dg \
--service-role-arn arn:aws:iam::111122223333:role/awscicd-codedeploy-role \
--auto-scaling-groups awscicd-cap-asg \
--deployment-style deploymentType=BLUE_GREEN,deploymentOption=WITH_TRAFFIC_CONTROL \
--load-balancer-info '{"targetGroupInfoList":[{"name":"awscicd-cap-tg"}]}' \
--blue-green-deployment-configuration '{"terminateBlueInstancesOnDeploymentSuccess":{"action":"TERMINATE","terminationWaitTimeInMinutes":1},"deploymentReadyOption":{"actionOnTimeout":"CONTINUE_DEPLOYMENT"},"greenFleetProvisioningOption":{"action":"COPY_AUTO_SCALING_GROUP"}}' \
--auto-rollback-configuration enabled=true,events=DEPLOYMENT_FAILURE
(Nhớ: service role CodeDeploy cần quyền ec2:RunInstances/ec2:CreateTags/iam:PassRole cho blue/green với launch template — đã thêm ở bài 11.)
Xác nhận hạ tầng
$ aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names awscicd-cap-asg \
--query "length(AutoScalingGroups[0].Instances[?LifecycleState=='InService'])"
2
$ aws elbv2 describe-load-balancers --names awscicd-cap-alb --query 'LoadBalancers[0].DNSName'
"awscicd-cap-alb-1647114948.ap-southeast-1.elb.amazonaws.com"
Hai instance InService, ALB có DNS. Lúc này các instance chạy httpd với trang mặc định — chưa có ứng dụng của ta. Pipeline ở bài 16 sẽ deploy app lên qua blue/green. Hạ tầng đích đã sẵn sàng làm "production" cho dây chuyền.
🧹 Dọn dẹp
Hạ tầng này dùng ở bài 16, nên giữ tới hết capstone. Bài 16 sẽ teardown toàn bộ (ASG, ALB, TG, launch template, deployment group). Vì có ALB + nhiều EC2, đừng để chạy qua đêm — nếu tạm dừng giữa bài 15 và 16, xóa ASG (delete-auto-scaling-group --force-delete) và ALB trước.
Tổng kết
Hạ tầng đích của capstone gói lại đúng những gì Part IV dạy: launch template làm khuôn, ASG giữ cụm co giãn sau ALB, và deployment group blue/green với rollback. Mọi thứ dựng bằng CLI, mỗi mảnh khớp với mảnh kia — ALB trỏ target group, ASG đăng ký vào target group, deployment group nhắm cả ASG lẫn target group để điều phối blue/green. Hai instance đang chạy chờ ứng dụng.
Bài cuối nối phần còn lại: một CodePipeline đưa code từ CodeCommit, qua build và test, qua một cổng phê duyệt, rồi blue/green deploy lên chính hạ tầng này — chạy end-to-end thật, rồi dọn sạch toàn bộ và tổng kết cả series.