
CloudFront-Server



cloudfront-lab và chọn Create key pair


cloudfront-sg


#!/bin/bash
# User data for Amazon Linux 2023. Runs once as root on first boot.
# Log output: /var/log/cloud-init-output.log
set -euxo pipefail
export HOME=/root
# Update the system and install Node.js 24 LTS from the Amazon Linux repo
dnf update -y
dnf install -y nodejs24 nodejs24-npm
# Install PM2 globally
npm install -g pm2@latest
# Create the app directory and install Express
mkdir -p /opt/app
cd /opt/app
npm init -y
npm install express
# Create the Express app
cat <<'EOF' > app.js
const express = require('express');
const app = express();
app.get('/api', (req, res) => {
console.log(JSON.stringify(req.headers));
res.json({
timestamp: new Date().toISOString(),
headers: req.headers,
});
});
app.listen(80, () => {
console.log('api is up!');
});
EOF
# Start the app with PM2 and keep it running across reboots
pm2 start /opt/app/app.js --name api
pm2 save
pm2 startup systemd -u root --hp /root



| Type | Protocol | Port range | Source |
|---|---|---|---|
| HTTP | TCP | 80 | 0.0.0.0/0 |
| HTTPS | TCP | 443 | 0.0.0.0/0 |
Ứng dụng lắng nghe ở cổng 80, nên cần rule HTTP để kiểm tra bằng trình duyệt và để CloudFront truy cập origin.
13. Sao chép Public IPv4 DNS
14. Mở http://<PUBLIC-DNS>/api trên trình duyệt, thay <PUBLIC-DNS> bằng giá trị vừa sao chép.