
CloudFront-Server



cloudfront-lab and select 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 |
The app listens on port 80, so the HTTP rule is required for the browser test and for CloudFront to reach the origin.
13. Copy the Public IPv4 DNS
14. Open http://<PUBLIC-DNS>/api in a browser, replacing <PUBLIC-DNS> with the value you copied.