DEV Community

Cover image for Triển Khai nhiều Static Websites trên AWS với nhiều môi trường bằng AWS Amplify
Danh Hoang Hieu Nghi
Danh Hoang Hieu Nghi

Posted on

Triển Khai nhiều Static Websites trên AWS với nhiều môi trường bằng AWS Amplify

Khi một công ty cần triển khai và duy trì nhiều static websites trên AWS, bài toán thường không chỉ là “host website ở đâu”, mà còn là:

  • Source code nằm ở nhiều hệ thống khác nhau như AWS CodeCommit, GitHub, Bitbucket
  • Có nhiều môi trường release: Development, Staging, UAT, Production
  • Mỗi môi trường phải tự động deploy khi merge code vào branch tương ứng
  • Toàn bộ traffic phải dùng HTTPS
  • Giải pháp phải serverless
  • Không được yêu cầu server chạy liên tục
  • Chi phí vận hành phải thấp nhất

Trong trường hợp này, lựa chọn phù hợp nhất là:

AWS Amplify Hosting


Bài toán kiến trúc

Giả sử công ty có nhiều website tĩnh:

Website A
Website B
Website C
Enter fullscreen mode Exit fullscreen mode

Source code của các website này có thể nằm ở nhiều nơi:

AWS CodeCommit
GitHub
Bitbucket
Enter fullscreen mode Exit fullscreen mode

Mỗi repository lại có các branch tương ứng với từng môi trường:

dev          → Development
staging      → Staging
uat          → User Acceptance Testing
production   → Production
Enter fullscreen mode Exit fullscreen mode

Yêu cầu là khi developer merge code vào branch nào, website ở môi trường tương ứng phải được deploy tự động.

Ví dụ:

Merge vào dev branch        → Deploy Development
Merge vào staging branch    → Deploy Staging
Merge vào uat branch        → Deploy UAT
Merge vào production branch → Deploy Production
Enter fullscreen mode Exit fullscreen mode

Kiến trúc tổng quan

Flow triển khai có thể hiểu như sau:

Source Code Repository
        ↓
Git Branch Merge
        ↓
AWS Amplify Hosting
        ↓
Automatic Build & Deploy
        ↓
Environment-specific Static Website
        ↓
HTTPS Access
Enter fullscreen mode Exit fullscreen mode

AWS Amplify đóng vai trò là dịch vụ managed hosting cho static websites và frontend apps. Nó có thể kết nối trực tiếp với Git repositories, theo dõi branch, tự động build và deploy khi có thay đổi.


Vì sao dùng AWS Amplify?

AWS Amplify Hosting phù hợp vì nó giải quyết trực tiếp các yêu cầu chính của bài toán.

1. Hỗ trợ nhiều Git providers

Amplify có thể kết nối với các hệ thống source control phổ biến như:

AWS CodeCommit
GitHub
Bitbucket
Enter fullscreen mode Exit fullscreen mode

Điều này giúp công ty không cần phải chuẩn hóa toàn bộ source code về một nơi duy nhất. Website nào đang ở GitHub thì vẫn giữ ở GitHub. Website nào đang ở Bitbucket thì vẫn có thể deploy được.


2. Branch-based deployment

Đây là điểm quan trọng nhất của bài toán.

Với AWS Amplify, mỗi branch có thể được map với một environment riêng.

Ví dụ:

Git Branch AWS Environment URL
dev Development dev.example.com
staging Staging staging.example.com
uat User Acceptance Testing uat.example.com
production Production www.example.com

Khi code được merge vào branch tương ứng, Amplify sẽ tự động trigger build và deploy.

dev branch merge
        ↓
Development deployment
Enter fullscreen mode Exit fullscreen mode
production branch merge
        ↓
Production deployment
Enter fullscreen mode Exit fullscreen mode

Không cần tự viết pipeline phức tạp cho từng môi trường.


3. Serverless static website hosting

Một yêu cầu quan trọng khác là:

Không được có server chạy liên tục.

Amplify Hosting là managed service. Bạn không cần tạo EC2 instance, không cần patch server, không cần quản lý web server như Nginx hay Apache.

Điều này giúp loại bỏ các thành phần vận hành như:

EC2 instances
Load balancers tự quản lý
Web servers chạy liên tục
Manual deployment scripts
Server patching
Enter fullscreen mode Exit fullscreen mode

Thay vào đó, Amplify quản lý hosting, build, deployment và delivery cho bạn.


4. HTTPS được hỗ trợ sẵn

Bài toán yêu cầu:

All data exchange must use HTTPS.

AWS Amplify Hosting hỗ trợ HTTPS cho website được deploy. Khi gắn custom domain, Amplify cũng có thể xử lý SSL/TLS certificate để website được truy cập an toàn qua HTTPS.

Ví dụ:

https://dev.example.com
https://staging.example.com
https://uat.example.com
https://www.example.com
Enter fullscreen mode Exit fullscreen mode

Người dùng cuối truy cập website qua browser với HTTPS lock icon, đảm bảo traffic được mã hóa.


5. Least operational overhead

Nếu nhìn theo góc độ AWS Solutions Architect exam, cụm từ quan trọng nhất trong đề thường là:

LEAST operational overhead

Điều này nghĩa là giải pháp không chỉ “chạy được”, mà phải là giải pháp cần ít cấu hình, ít vận hành, ít bảo trì nhất.

AWS Amplify phù hợp vì:

Connect trực tiếp với Git repository
Tự detect branch merge
Tự build
Tự deploy
Tự host static website
Có HTTPS
Không cần server chạy liên tục
Enter fullscreen mode Exit fullscreen mode

Đây là một giải pháp managed và serverless, nên operational overhead thấp hơn nhiều so với việc tự lắp ghép nhiều dịch vụ riêng lẻ.


So sánh với các lựa chọn khác

Elastic Beanstalk

Elastic Beanstalk phù hợp hơn cho application backend hoặc web application cần runtime server.

Với static websites đơn giản, dùng Elastic Beanstalk là không tối ưu vì vẫn có nhiều operational overhead hơn.

Bạn sẽ phải quan tâm nhiều hơn đến môi trường chạy ứng dụng, scaling, platform, instance, deployment configuration.

Elastic Beanstalk = chạy được, nhưng không lý tưởng cho static sites đơn giản
Enter fullscreen mode Exit fullscreen mode

S3 + CodePipeline + CodeBuild

S3 static website hosting kết hợp với CodePipeline và CodeBuild cũng có thể triển khai được.

Tuy nhiên, bạn sẽ phải tự cấu hình nhiều thứ hơn:

S3 bucket cho từng environment
CodePipeline cho từng branch
CodeBuild project
IAM roles
Artifact flow
CloudFront nếu cần HTTPS với custom domain
Invalidation logic nếu dùng CDN
Enter fullscreen mode Exit fullscreen mode

Giải pháp này có thể đúng về mặt kỹ thuật, nhưng không phải lựa chọn có least operational overhead.

S3 + CodePipeline + CodeBuild = works, but more setup
Enter fullscreen mode Exit fullscreen mode

EC2

EC2 là lựa chọn kém nhất cho bài toán này.

Vì EC2 yêu cầu server chạy liên tục, trái ngược hoàn toàn với yêu cầu:

No continuously running servers
Enter fullscreen mode Exit fullscreen mode

Ngoài ra, dùng EC2 để host static websites sẽ kéo theo nhiều công việc vận hành:

Provision server
Install web server
Patch OS
Configure HTTPS
Monitor instance
Handle scaling
Manage availability
Enter fullscreen mode Exit fullscreen mode

Đây là operational overhead cao nhất.


Kiến trúc đề xuất

Một kiến trúc hợp lý sẽ như sau:

AWS CodeCommit / GitHub / Bitbucket
        ↓
Branch merge event
        ↓
AWS Amplify Hosting
        ↓
Automatic build and deployment
        ↓
Development / Staging / UAT / Production
        ↓
HTTPS static websites
Enter fullscreen mode Exit fullscreen mode

Mỗi website có thể có các environment riêng:

Website A
├── dev.example.com
├── staging.example.com
├── uat.example.com
└── www.example.com
Enter fullscreen mode Exit fullscreen mode
Website B
├── dev.website-b.com
├── staging.website-b.com
├── uat.website-b.com
└── www.website-b.com
Enter fullscreen mode Exit fullscreen mode

Mapping branch với environment

Một cách triển khai thường gặp:

Branch Environment Purpose
dev Development Developer testing
staging Staging Pre-release validation
uat User Acceptance Testing Business/user approval
production Production Live website

Khi team merge code vào staging, Amplify tự động deploy bản staging.

Khi release đã được approve, team merge vào production, Amplify tự động deploy bản production.


Key exam takeaway

Trong AWS exam, khi thấy các keyword sau:

Static websites
Multiple Git providers
Branch-based deployment
Development / Staging / UAT / Production
HTTPS
Serverless
No continuously running servers
Least operational overhead
Enter fullscreen mode Exit fullscreen mode

Hãy nghĩ ngay đến:

AWS Amplify Hosting
Enter fullscreen mode Exit fullscreen mode

Kết luận

AWS Amplify Hosting là lựa chọn phù hợp nhất cho bài toán deploy nhiều static websites trên AWS với nhiều môi trường release.

Nó hỗ trợ trực tiếp các yêu cầu quan trọng:

✅ Static website hosting
✅ Kết nối với CodeCommit, GitHub, Bitbucket
✅ Branch-based deployments
✅ Tự động build và deploy khi merge code
✅ HTTPS enabled
✅ Serverless
✅ Không cần EC2
✅ Không có always-on servers
✅ Least operational overhead
Enter fullscreen mode Exit fullscreen mode

Với bài toán này, giải pháp tốt nhất là:

Host each website by using AWS Amplify Hosting, connect repository branches to the corresponding environments, and trigger deployments automatically when code is merged into each branch.

Top comments (0)