Deploy Frontend VinaShoes và Edge Layer với 4 service chính:
📋 Prerequisites từ Task 2:
vinashoes.org đã đăng ký và DNS readyvinashoes.org, www.vinashoes.org, img.vinashoes.orgTarget Deliverables:
img.vinashoes.org)vinashoes.org)🎯 Objective: Verify domain setup và quản lý DNS records trên Cloudflare
Cloudflare Console:

vinashoes.org
vinashoes.org để access DNS settings
img.vinashoes.org
🎯 Deliverable 3.0: Domain ready cho infrastructure setup
🎯 Objective: Secure image storage chỉ accessible qua CloudFront
AWS Console:

vinashoes-images-prod




products/, categories/, banners/products/: nike/, adidas/, puma/
AWS Console:




🎯 Deliverable 3.1: S3 bucket ready với secure access configuration
🎯 Objective: Setup CDN cho image caching với performance <200ms
AWS Console:






🎯 Deliverable 3.2: Image CDN qua CloudFront nhanh <200ms
🎯 Objective: Protect CloudFront khỏi các attack patterns phổ biến

🎯 Deliverable 3.3: WAF protection active cho CloudFront
🎯 Objective: Deploy Next.js Frontend với GitHub CI/CD tự động

🎯 Deliverable 3.4: Production-ready Next.js website với custom domain
🗒️ Prerequisites:
aws CLI configured with appropriate profile and permissionsjq installed (for JSON parsing) — optional but helpfulAPP_NAME="vinashoes-frontend-prod"
REGION="us-east-1"
APP_ID=$(aws amplify list-apps --region $REGION \
--query "apps[?contains(name, '$APP_NAME')].appId" --output text)
if [ -n "$APP_ID" ]; then
echo "Deleting Amplify app: $APP_NAME ($APP_ID)"
aws amplify delete-app --app-id $APP_ID --region $REGION
fi
REGION="us-east-1"
WAF_NAME="VinaShoesImagesWAF"
WAF_INFO=$(aws wafv2 list-web-acls --scope CLOUDFRONT --region $REGION \
--query "WebACLs[?Name=='$WAF_NAME']" --output json)
WAF_ID=$(echo "$WAF_INFO" | jq -r '.[0].Id')
if [ "$WAF_ID" != "null" ]; then
LOCK_TOKEN=$(aws wafv2 get-web-acl --name $WAF_NAME --scope CLOUDFRONT \
--region $REGION --query LockToken --output text)
aws wafv2 delete-web-acl --name $WAF_NAME --scope CLOUDFRONT \
--id $WAF_ID --lock-token $LOCK_TOKEN --region $REGION
fi
ORIGIN_DOMAIN="vinashoes-images-prod.s3.amazonaws.com"
DIST_ID=$(aws cloudfront list-distributions \
--query "DistributionList.Items[?Origins.Items[0].DomainName=='$ORIGIN_DOMAIN'].Id" --output text)
if [ -n "$DIST_ID" ]; then
# Disable first
aws cloudfront get-distribution-config --id $DIST_ID > /tmp/dist-config.json
ETAG=$(jq -r '.ETag' /tmp/dist-config.json)
jq '.DistributionConfig.Enabled=false' /tmp/dist-config.json | jq 'del(.ETag)' > /tmp/disabled-config.json
aws cloudfront update-distribution --id $DIST_ID \
--distribution-config file:///tmp/disabled-config.json --if-match $ETAG
# Wait and delete
aws cloudfront wait distribution-deployed --id $DIST_ID
NEW_ETAG=$(aws cloudfront get-distribution-config --id $DIST_ID --query ETag --output text)
aws cloudfront delete-distribution --id $DIST_ID --if-match $NEW_ETAG
fi
BUCKET="vinashoes-images-prod"
REGION="ap-southeast-1"
aws s3 rm s3://$BUCKET --recursive
aws s3api delete-bucket --bucket $BUCKET --region $REGION
💰 Objective: Cost breakdown và optimization cho Task 3
| Service | Minimal (Dev) | Production (Small) | Enterprise (High Traffic) |
|---|---|---|---|
| S3 Storage | $0.02 (1GB) | $1.15 (50GB) | $11.50 (500GB) |
| S3 Requests & Transfer | <$0.10 | $0.50 | $10.00 |
| CloudFront Data Transfer | $0.85 (10GB) | $42.50 (500GB) | $425.00 (5TB) |
| CloudFront Requests | $0.08 (100K) | $3.75 (5M) | $37.50 (50M) |
| WAF (Web ACL + Rules) | $3.00 | $6.00 | $20.00 |
| Amplify Hosting | $0.00 (Free tier) | $5.00 | $20.00 |
| Monitoring & Logging | <$1.00 | $5.00 | $10.00 |
| Tổng Chi Phí/tháng | $3-5 | $15-52 | $500+ |
Minimal Setup (Development/Testing):
Production Setup (Small Business):
Enterprise Setup (High Traffic):
Business Value:
💡 Cost Optimization Summary:

📊 Monitoring Best Practices:
Step 5: Review and create web ACL
Final Review Checklist:
Click “Create web ACL” để hoàn tất

🎉 WAF Creation Success:
Web ACL Configuration Summary:
VinaShoesImagesWAFVerify WAF Creation:
VinaShoesImagesWAF
🚨 Default Action Strategy:
⚠️ Rate Limiting Considerations:
Create Custom Rule - Block Suspicious User Agents:
{
"Name": "BlockSuspiciousUserAgents",
"Priority": 500,
"Statement": {
"ByteMatchStatement": {
"SearchString": "bot|crawler|scanner|scraper",
"FieldToMatch": {
"SingleHeader": {
"Name": "user-agent"
}
}
}
},
"Action": {
"Block": {}
}
}
🎯 Custom Rules Ideas:
🎯 Objective: Deploy Next.js Frontend với GitHub CI/CD tự động, auto-scaling, và production-ready hosting
⚠️ Amplify Prerequisites:
🚀 Project Structure Best Practices:
1. Create amplify.yml (Build Configuration):
version: 1
applications:
- frontend:
phases:
preBuild:
commands:
# Node version management
- nvm use 18
- node --version
- npm --version
# Clean install với cache optimization
- npm ci --prefer-offline --no-audit
# Environment-specific setup
- echo "NEXT_PUBLIC_CDN_URL=https://img.vinashoes.org" >> .env.production
- echo "NEXT_PUBLIC_API_URL=https://api.vinashoes.org" >> .env.production
build:
commands:
# TypeScript type checking
- npm run type-check
# Linting và code quality
- npm run lint
# Build production bundle
- npm run build
# Post-build optimizations
- echo "Build completed successfully"
- du -sh out/
postBuild:
commands:
# Generate sitemap
- npm run generate-sitemap
# Optimize images (if needed)
- npm run optimize-images
artifacts:
baseDirectory: out
files:
- '**/*'
cache:
paths:
- node_modules/**/*
- .next/cache/**/*
# Custom headers for security và performance
customHeaders:
- pattern: '**/*'
headers:
- key: 'X-Frame-Options'
value: 'DENY'
- key: 'X-Content-Type-Options'
value: 'nosniff'
- key: 'Strict-Transport-Security'
value: 'max-age=31536000; includeSubDomains'
- key: 'Cache-Control'
value: 'public, max-age=31536000, immutable'
- pattern: '*.html'
headers:
- key: 'Cache-Control'
value: 'public, max-age=300'

⚠️ Build Configuration Tips:
2. Package.json Scripts Optimization:
{
"name": "vinashoes-frontend",
"version": "1.0.0",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint --fix",
"type-check": "tsc --noEmit",
"export": "next export",
"analyze": "ANALYZE=true npm run build",
"generate-sitemap": "node scripts/generate-sitemap.js",
"optimize-images": "node scripts/optimize-images.js",
"test": "jest",
"test:e2e": "playwright test"
},
"dependencies": {
"next": "^14.0.0",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"@types/react": "^18.0.0",
"typescript": "^5.0.0",
"eslint": "^8.0.0",
"eslint-config-next": "^14.0.0"
}
}
3. Update next.config.ts với Advanced Settings:
import type { NextConfig } from 'next'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
const nextConfig: NextConfig = {
// Static export cho Amplify
output: 'export',
// Image optimization settings
images: {
unoptimized: true,
domains: ['img.vinashoes.org', 'cdn.vinashoes.org'],
formats: ['image/webp', 'image/avif'],
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384]
},
// Performance optimizations
compress: true,
poweredByHeader: false,
// Trailing slash consistency
trailingSlash: true,
// Environment variables
env: {
NEXT_PUBLIC_CDN_URL: process.env.NEXT_PUBLIC_CDN_URL,
NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,
NEXT_PUBLIC_GA_ID: process.env.NEXT_PUBLIC_GA_ID
},
// Webpack customization
webpack: (config, { dev, isServer }) => {
// Bundle analyzer
if (process.env.ANALYZE === 'true') {
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
})
)
}
// Optimize chunks
if (!dev && !isServer) {
config.optimization.splitChunks = {
chunks: 'all',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
}
}
return config
},
// Redirects và rewrites
async redirects() {
return [
{
source: '/home',
destination: '/',
permanent: true,
},
]
},
// Headers cho security
async headers() {
return [
{
source: '/(.*)',
headers: [
{
key: 'X-Frame-Options',
value: 'DENY',
},
{
key: 'X-Content-Type-Options',
value: 'nosniff',
},
{
key: 'Referrer-Policy',
value: 'origin-when-cross-origin',
},
],
},
]
},
}
export default nextConfig
💡 Next.js Configuration Advanced:
4. Environment Files Setup:
# .env.local (development)
NEXT_PUBLIC_CDN_URL=https://img.vinashoes.org
NEXT_PUBLIC_API_URL=http://localhost:3001
NEXT_PUBLIC_GA_ID=G-XXXXXXXXXX
NEXT_PUBLIC_ENV=development
# .env.production (production)
NEXT_PUBLIC_CDN_URL=https://img.vinashoes.org
NEXT_PUBLIC_API_URL=https://api.vinashoes.org
NEXT_PUBLIC_GA_ID=G-XXXXXXXXXX
NEXT_PUBLIC_ENV=production
# .env.staging (staging)
NEXT_PUBLIC_CDN_URL=https://img-staging.vinashoes.org
NEXT_PUBLIC_API_URL=https://api-staging.vinashoes.org
NEXT_PUBLIC_GA_ID=G-YYYYYYYYYY
NEXT_PUBLIC_ENV=staging
🔗 GitHub Integration Options:
AWS Console:


🔐 Security Best Practices:
vinashoes-frontendmain (hoặc develop cho staging)vinashoes-frontend-prodproduction
1. Auto-detected Build Settings: Amplify sẽ auto-detect Next.js và suggest build commands:

2. Advanced Build Settings:

⚡ Build Performance Optimization:
3. Environment Variables:

# Production Environment Variables
NEXT_PUBLIC_CDN_URL=https://img.vinashoes.org
NEXT_PUBLIC_API_URL=https://api.vinashoes.org
NEXT_PUBLIC_GA_ID=G-XXXXXXXXXX
NEXT_PUBLIC_ENV=production
NODE_ENV=production
1. Initial Deployment:

2. Build Logs Analysis:

3. Deployment Verification:
https://production.d1a2b3c4d5e6f7.amplifyapp.com
🔗 Task 2 Integration:
Sử dụng domain vinashoes.org đã setup từ Task 2 (Cloudflare Registrar + DNS)
vinashoes.org (already registered qua Cloudflare)⚠️ Domain Configuration Strategy: Vì domain đã được setup trong Task 2 với Cloudflare, chúng ta sẽ:
1. Add Custom Domain (Current Step):
www.vinashoes.org (như screenshot)
2. Choose DNS Configuration Method:
Select “Manual configuration” (REQUIRED for Cloudflare DNS):

💡 Why Manual Configuration?
3. Domain Configuration Details:
www.vinashoes.orgmain (production)

4. SSL Certificate Validation: Sau khi clicking “Configure domain”, Amplify sẽ provide:

5. Add DNS Records in Cloudflare:
_amazonawsacm.www)
🚨 Critical Cloudflare Settings:
6. Monitor Domain Status:
Trong Amplify Console, domain status sẽ progress through:

Truy cập https://www.vinashoes.org/ để xem thành quả
⚠️ Quan trọng về Clean Up:
💡 Amplify Cleanup Strategy:
AWS Console:
Navigate to Amplify Console:
vinashoes-frontend-prodDelete Application:
Verify Cleanup:
www.vinashoes.org sẽ không còn accessibleCLI Alternative:
# List all Amplify apps
aws amplify list-apps --region us-east-1
# Delete specific app
aws amplify delete-app --app-id d1a2b3c4d5e6f7 --region us-east-1
🚨 WAF Deletion Dependencies:
AWS Console:
Check Associated Resources:
VinaShoesImagesWAFDisassociate from CloudFront:
Delete Web ACL:
Verify Cleanup:
CLI Alternative:
# List Web ACLs
aws wafv2 list-web-acls --scope CLOUDFRONT --region us-east-1
# Disassociate from resources first
aws wafv2 disassociate-web-acl \
--resource-arn arn:aws:cloudfront::123456789012:distribution/EDFDVBD6EXAMPLE \
--region us-east-1
# Delete Web ACL
aws wafv2 delete-web-acl \
--scope CLOUDFRONT \
--id VinaShoesImagesWAF-id \
--lock-token token-value \
--region us-east-1
⚠️ CloudFront Deletion Process:
AWS Console:
Disable Distribution:
Wait for Disable Completion:
Delete Distribution:
Update Cloudflare DNS:
img.vinashoes.org → CloudFront domainCLI Alternative:
# Get distribution config
aws cloudfront get-distribution --id EDFDVBD6EXAMPLE
# Disable distribution
aws cloudfront update-distribution \
--id EDFDVBD6EXAMPLE \
--distribution-config file://disabled-config.json \
--if-match ETAG_VALUE
# Wait for deployment
aws cloudfront wait distribution-deployed --id EDFDVBD6EXAMPLE
# Delete distribution
aws cloudfront delete-distribution \
--id EDFDVBD6EXAMPLE \
--if-match NEW_ETAG_VALUE
🚨 S3 Deletion Requirements:
AWS Console:
Empty Bucket Contents:
vinashoes-images-proddelete để confirmDelete Bucket:
vinashoes-images-prodVerify Complete Deletion:
CLI Alternative:
# Remove all objects (including versions)
aws s3 rm s3://vinashoes-images-prod --recursive
# Remove delete markers and incomplete multipart uploads
aws s3api delete-bucket --bucket vinashoes-images-prod --region ap-southeast-1
📜 Certificate Cleanup:
AWS Console (us-east-1):
Navigate to ACM Console:
Check Certificate Usage:
*.vinashoes.orgDelete Certificate (Optional):
Clean Cloudflare DNS Records:
Verify Complete Cleanup:
# Check AWS resources
aws amplify list-apps --region us-east-1
# Expected: No apps
aws wafv2 list-web-acls --scope CLOUDFRONT --region us-east-1
# Expected: No Web ACLs
aws cloudfront list-distributions
# Expected: No distributions or empty list
aws s3 ls | grep vinashoes-images-prod
# Expected: No output (bucket deleted)
# Check AWS billing
aws ce get-cost-and-usage \
--time-period Start=2024-12-01,End=2024-12-08 \
--granularity DAILY \
--metrics BlendedCost
✅ Cleanup Complete:
💰 Objective: Detailed cost breakdown và optimization strategies cho Task 3 infrastructure
| Service | Minimal (Dev) | Production (Small) | Enterprise (High Traffic) |
|---|---|---|---|
| S3 Storage | $0.02 (1GB) | $1.15 (50GB) | $11.50 (500GB) |
| S3 Requests & Transfer | <$0.10 | $0.50 | $10.00 |
| CloudFront Data Transfer | $0.85 (10GB) | $42.50 (500GB) | $425.00 (5TB) |
| CloudFront Requests | $0.08 (100K) | $3.75 (5M) | $37.50 (50M) |
| WAF (Web ACL + Rules) | $3.00 | $6.00 | $20.00 |
| Amplify Hosting | $0.00 (Free tier) | $5.00 | $20.00 |
| Monitoring & Logging | <$1.00 | $5.00 | $10.00 |
| Tổng Chi Phí/tháng | $3-5 | $15-52 | $500+ |
| Tier | S3 Storage | S3 Requests | CloudFront Data | CloudFront Requests | WAF | Amplify | Monitoring | Tổng |
|---|---|---|---|---|---|---|---|---|
| Minimal (Dev) | $0.02 | <$0.10 | $0.85 | $0.08 | $3.00 | $0.00 | <$1.00 | $3-5 |
| Production (Small) | $1.15 | $0.50 | $42.50 | $3.75 | $6.00 | $5.00 | $5.00 | $15-52 |
| Enterprise (High) | $11.50 | $10.00 | $425.00 | $37.50 | $20.00 | $20.00 | $10.00 | $500+ |
| Service | Chi Phí Cơ Bản | Chi Phí Thêm | Ví dụ |
|---|---|---|---|
| S3 Storage | $0.023/GB/tháng | $0.0125/GB (IA), $0.004/GB (Glacier) | 10GB = $0.23 |
| S3 Requests | $0.0004/1000 GET | $0.005/1000 PUT | 10K requests = $0.05 |
| CloudFront Data | $0.085/GB (đầu tiên 1TB) | $0.080/GB (tiếp theo) | 100GB = $8.50 |
| CloudFront Requests | $0.0075/10K requests | - | 1M requests = $0.75 |
| WAF Web ACL | $1.00/tháng | $1.00/rule group | Core + Known Bad = $3 |
| WAF Processing | $0.60/million requests | - | 500K requests = $0.30 |
| Amplify Build | 1000 phút free | $0.01/phút | 100 phút = free |
| Amplify Hosting | 5GB free | $0.023/GB | 2GB = free |
| Kịch Bản | Traffic | Storage | Chi Phí Dự Kiến | Notes |
|---|---|---|---|---|
| Development | 100 users/day | 1GB images | $3-5/tháng | Free tier heavy |
| Small Business | 10K visits/tháng | 50GB images | $15-25/tháng | Basic scaling |
| Medium Business | 100K visits/tháng | 200GB images | $50-100/tháng | Moderate traffic |
| Large Business | 1M visits/tháng | 1TB images | $200-400/tháng | High traffic |
| Enterprise | 10M+ visits/tháng | 5TB+ images | $500+/tháng | Heavy usage |
| Chiến Lược | Mô Tả | Tiết Kiệm Dự Kiến |
|---|---|---|
| CloudFront Caching | Cache-Control dài hạn cho images | 80% giảm origin requests |
| S3 Lifecycle | Chuyển assets cũ sang IA/Glacier | 60% giảm storage costs |
| WAF Tuning | Monitor mode trước, block sau | Tránh false positives |
| Amplify Free Tier | Tận dụng build minutes và storage free | $0 cho small projects |
| AWS Budgets | Setup alerts khi vượt threshold | Tránh bill surprises |
| Metric | Giá Trị | Notes |
|---|---|---|
| Performance Gain | 75% faster page loads | 3.2s → 0.8s |
| Cost vs Traditional | 70-80% tiết kiệm | $100-230 → $15-50 |
| Development Time | 40 hours saved | Infrastructure management |
| ROI | 6,300% | 63 hours × $50/hour = $3,150 value |
💡 Cost Optimization Summary:
🎉 Task 3 Complete! Frontend và Edge Layer đã ready với proper sequence: S3 → CloudFront → WAF → Amplify. Next: Task 4 - VPC Infrastructure setup.
Next Step: Task 4: SageMaker Training