Thiết lập public endpoint cho backend NestJS service thông qua Amazon API Gateway (HTTP API) với domain https://api.vinashoes.org:
/ → forward tới ECS Service📋 Dependencies: Task 2 (SSL), Task 3 (Frontend), Task 9 (ECS)
Frontend (Next.js) → api.vinashoes.org → API Gateway → ALB → ECS Fargate (NestJS) → MongoDB Atlas
🔗 Integration Flow:
https://api.vinashoes.org/api/products
vinashoes-backend-apiBackend API for VinAShoes ecommerce

✅ Kết quả: API Gateway đã tạo với invoke URL:
https://xxxxxxx.execute-api.ap-southeast-1.amazonaws.com
Bước 1: Tạo Custom Domain

api.vinashoes.org*.vinashoes.org hoặc api.vinashoes.org
Bước 2: API Mappings

vinashoes-backend-api$default
📝 Lưu ý: Copy API Gateway domain name (dạng d-xxxxxxx.execute-api.ap-southeast-1.amazonaws.com) để setup DNS
Tạo HTTP Integration:


Integration details:
ANY (keep default)http://vinashoes-abl-647833895.ap-southeast-1.elb.amazonaws.com/{proxy}Advanced settings:
30000 (milliseconds)Integration to ECS ALB for backend API (optional)Create

🔗 ALB URL: http://vinashoes-abl-647833895.ap-southeast-1.elb.amazonaws.com/
Integration URI: http://vinashoes-abl-647833895.ap-southeast-1.elb.amazonaws.com/{proxy}

/{proxy+}vinashoes-abl-647833895.ap-southeast-1.elb.amazonaws.com/{proxy} (7x7wx1u)
Tạo default route:
/vinashoes-abl-647833895.ap-southeast-1.elb.amazonaws.com/{proxy}

* (hoặc specific domain)Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token*
Bước 1: Lấy API Gateway Domain Name
d-xxxxxxx.execute-api.ap-southeast-1.amazonaws.com
Bước 2: Cấu hình DNS Record

apid-xxxxxxx.execute-api.ap-southeast-1.amazonaws.com
⏰ DNS Propagation: Đợi 5-10 phút để DNS record propagate
Step 1: Test ALB Direct
# Test Load Balancer trực tiếp
curl -i http://vinashoes-alb-xxx.ap-southeast-1.elb.amazonaws.com/health
# Expected: HTTP/1.1 200 OK
# {"status":"ok"}
Step 2: Test API Gateway Invoke URL
# Test API Gateway invoke URL
curl -i https://xxxxxxx.execute-api.ap-southeast-1.amazonaws.com/health
# Expected: Same response as ALB
Step 3: Test Custom Domain
# Test custom domain (đợi DNS propagate)
curl -i https://api.vinashoes.org/health
# Expected: Same response
Step 4: Test API Endpoints
# Test API endpoints qua custom domain
curl https://api.vinashoes.org/api/products
curl https://api.vinashoes.org/api/categories
🔍 Nếu gặp lỗi 502 Bad Gateway:
# Check target group health
aws elbv2 describe-target-health --target-group-arn arn:aws:elasticloadbalancing:...
# Check ECS service
aws ecs describe-services --cluster vinashoes-cluster --services vinashoes-backend-service
🔍 Nếu DNS không resolve:
# Check DNS propagation
nslookup api.vinashoes.org 8.8.8.8
dig api.vinashoes.org CNAME
🔍 Nếu SSL certificate lỗi:
api.vinashoes.org🎯 API Gateway setup thành công:
https://api.vinashoes.org/{proxy+} và /🔗 Production Architecture:
Frontend (Next.js)
↓
https://api.vinashoes.org
↓
API Gateway (Custom Domain + SSL)
↓
Application Load Balancer
↓
ECS Fargate (NestJS containers)
↓
MongoDB Atlas
🚀 Frontend Integration:
// Frontend Next.js có thể gọi API
const apiUrl = "https://api.vinashoes.org";
// Get products
const products = await fetch(`${apiUrl}/api/products`);
// Create order
const order = await fetch(`${apiUrl}/api/orders`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(orderData),
});
📋 Health Check URLs:
http://vinashoes-alb-xxx.ap-southeast-1.elb.amazonaws.com/healthhttps://xxxxxxx.execute-api.ap-southeast-1.amazonaws.com/healthhttps://api.vinashoes.org/health🎉 Task 10 hoàn thành! Backend NestJS service đã có public endpoint với SSL và custom domain!
Create API (CLI):
# Create HTTP API
aws apigatewayv2 create-api \
--name vinashoes-backend-api \
--protocol-type HTTP \
--description "Backend API for VinAShoes ecommerce"
# Create custom domain
aws apigatewayv2 create-domain-name \
--domain-name api.vinashoes.org \
--domain-name-configurations CertificateArn=arn:aws:acm:...
# Create integration
aws apigatewayv2 create-integration \
--api-id <api-id> \
--integration-type HTTP_PROXY \
--integration-uri http://vinashoes-alb-xxx.ap-southeast-1.elb.amazonaws.com/{proxy}
# Create route
aws apigatewayv2 create-route \
--api-id <api-id> \
--route-key "ANY /{proxy+}" \
--target integrations/<integration-id>
Xóa API Gateway và các thành phần liên quan:
# Xóa API mappings từ custom domain
aws apigatewayv2 delete-api-mapping \
--domain-name api.vinashoes.org \
--api-mapping-id <mapping-id>
# Xóa custom domain
aws apigatewayv2 delete-domain-name \
--domain-name api.vinashoes.org
# Xóa routes
aws apigatewayv2 delete-route \
--api-id <api-id> \
--route-id <route-id>
# Xóa integrations
aws apigatewayv2 delete-integration \
--api-id <api-id> \
--integration-id <integration-id>
# Xóa API
aws apigatewayv2 delete-api \
--api-id <api-id>
Xóa CNAME record từ Cloudflare:
apiNếu certificate chỉ dùng cho API Gateway:
# Liệt kê certificates
aws acm list-certificates
# Xóa certificate (chỉ nếu không dùng cho domain khác)
aws acm delete-certificate \
--certificate-arn arn:aws:acm:region:account:certificate/certificate-id
⚠️ Thứ Tự Dọn Dẹp API Gateway:
Cấu trúc giá Amazon API Gateway:
| Thành Phần Dịch Vụ | Miễn Phí | Trả Phí | Ước Tính Chi Phí |
|---|---|---|---|
| HTTP API Requests | 1 triệu requests/tháng | $1.00/1 triệu requests | $1-10/tháng |
| Custom Domain | - | $0.10/tháng | $0.10/tháng |
| Data Transfer Out | 1GB/tháng | $0.09/GB | $1-5/tháng |
| SSL Certificate (ACM) | Miễn phí | - | $0/tháng |
Ước tính chi phí cho API Gateway:
Chi Phí Cơ Bản API Gateway:
HTTP API Requests: $3/tháng (3 triệu requests)
Custom Domain: $0.10/tháng (1 domain)
Data Transfer: $2/tháng (20GB out)
SSL Certificate: $0/tháng (miễn phí)
Tổng Chi Phí Hàng Tháng: $5.10/tháng
Giảm chi phí API Gateway:
Chiến Thuật Tối Ưu:
1. Request Optimization:
- Implement caching để giảm số requests
- Use API Gateway throttling để control traffic
- Optimize client-side để giảm unnecessary calls
2. Data Transfer:
- Enable compression cho responses
- Use CloudFront để cache static content
- Minimize response size
3. Domain Management:
- Share certificate across multiple APIs
- Use regional endpoints thay vì edge-optimized nếu có thể
Lợi Ích API Gateway vs Chi Phí:
| Loại Lợi Ích | Giá Trị | Tác Động Chi Phí |
|---|---|---|
| Security & SSL | Endpoint bảo mật với custom domain | Vô giá trị |
| Scalability | Auto-scaling với ALB integration | $50K+ infrastructure savings |
| Monitoring | Built-in logging và metrics | $10K+ observability value |
| Developer Experience | CORS, throttling, validation | 20% faster development |
| CORS Management | Automatic CORS handling | $5K+ frontend integration |
Tính Toán ROI:
Theo dõi chi tiêu API Gateway:
# Kiểm tra chi phí API Gateway
aws ce get-cost-and-usage \
--time-period Start=2024-01-01,End=2024-01-31 \
--granularity MONTHLY \
--metrics BlendedCost \
--group-by Type=DIMENSION,Key=SERVICE \
--filter '{
"Dimensions": {
"Key": "SERVICE",
"Values": ["Amazon API Gateway"]
}
}'
# Giám sát API usage
aws apigatewayv2 get-api \
--api-id <api-id>
# Check request metrics
aws cloudwatch get-metric-statistics \
--namespace AWS/ApiGateway \
--metric-name Count \
--dimensions Name=ApiName,Value=vinashoes-backend-api \
--start-time 2024-01-01T00:00:00Z \
--end-time 2024-01-31T23:59:59Z \
--period 86400 \
--statistics Sum
💡 Thực Tiễn Quản Lý Chi Phí Tốt Nhất
Request Optimization:
Monitoring:
Cost Control:
Scaling Considerations:
🚀 Production-Ready AWS Microservices Platform with Complete API Gateway! 🚀