Download deployment/aws/README.md from Wizzas/DefectRAG: direct link, hf CLI and curl.
- Browser
- Download file 15.5 kB
-
https://huggingface.co/Wizzas/DefectRAG/resolve/main/deployment/aws/README.md
- Command line
-
hf download hf://Wizzas/DefectRAG/deployment/aws/README.md
-
curl -L -o README.md https://huggingface.co/Wizzas/DefectRAG/resolve/main/deployment/aws/README.md
Host the DefectRAG dashboard on AWS (learning project)
Goal: a public, read-only dashboard (the saved-example showcase) on AWS, built so you practise Budgets, Lightsail, Lambda, SNS, IAM and CloudWatch. An EC2 lab server (Phase 4) is an optional extra. No GPU, no AI model, no uploads. The site is plain static files, so it is cheap and safe. Live inspection runs on the Hugging Face Space; this site only shows saved results.
Status (9 Nov 2026): Phases 1, 2, 3 and the monitoring part of 5 are built and live (HTTPS site, status API, budget guard, CloudWatch alarm and dashboard, Lightsail alarms). Phase 4 (EC2 lab) was skipped on purpose.
Prices and Free Plan rules change. Treat every number below as an estimate and check the AWS Billing console and the AWS Pricing Calculator before you start.
What you will build
Visitor βββΊ Lightsail instance (nginx, static IP, firewall, Let's Encrypt HTTPS) β the website (~$5/month)
β
ββ status.html βββΊ Lambda Function URL (/status) β tiny live API
β
ββ CloudWatch: dashboard + error alarm βββΊ SNS topic βββΊ email
Lightsail alarms (status check, CPU, burst capacity) βββΊ email
Budgets βββΊ SNS βββΊ Lambda "budget guard" (stops tagged EC2 lab instances; none exist unless you do Phase 4)
| Skill on your resume | Where you use it here |
|---|---|
| Budgets | Phase 1: spending alerts, plus an automatic stop-switch |
| Lightsail | Phase 2: the public website with HTTPS and instance alarms |
| Lambda | Phase 3: a status API and the budget guard |
| SNS / IAM | Phase 3: alert topics, one least-privilege role per function |
| CloudWatch | Phase 4: Lambda dashboard and an error alarm that emails you |
| EC2 | Phase 4b (optional, not done here): a lab server with the agent |
Rough cost for 178 days: Lightsail about $30. Lambda, SNS and CloudWatch basics stay near $0 at this size. An optional EC2 lab costs about $0.40 per day while running, so run it only while learning.
Rules that protect your credit (do these first)
- Phase 1 (Budgets) before anything else.
- Never use the root account for daily work. Turn on MFA for root, then create an admin IAM user (or IAM Identity Center user) with MFA and use that.
- Do not create access keys for root. Do not put keys in the website or in Git.
- One AWS Region for everything (for example
us-east-1or the one closest to you). Resources in other regions are easy to forget. - Tag every resource
Project=DefectRAG. Tags make cleanup easy. - Free Plan end date: the plan ends after 6 months or when credits run out, whichever comes first, and the account can close unless you upgrade. Put a calendar reminder at day 150 to decide: upgrade, or run the teardown in Phase 6 and keep screenshots for your portfolio.
Phase 1: Budgets (20 minutes)
- Console β Billing and Cost Management β Budgets β Create budget.
- Choose Customize β Cost budget. Name
defectrag-monthly. Period Monthly. Amount $20. - Add three alerts on Actual cost: 50%, 80%, 100%. Send to your email.
- Add one alert on Forecasted cost at 100%.
- Also open Billing β Credits and note the balance and expiry. Check it every Sunday.
- Optional stop-switch (Phase 3): make the 100% alert publish to an SNS topic so Lambda can stop the lab server.
Budgets data is delayed by several hours. Alerts are a safety net, not a real-time limit. Terminating things you do not use is the real protection.
Phase 2: Lightsail website (45 minutes)
2.1 Build the site on your PC
cd frontend
npm ci
npm run build:showcase # creates frontend/dist-static
Test it locally first: python -m http.server 8771 --directory frontend/dist-static, then open http://127.0.0.1:8771.
2.2 Create the server
- Console β Lightsail β Create instance. Linux/Unix, OS only β Ubuntu 24.04 LTS.
- Plan: the $5/month plan with a public IPv4 address (check that it includes IPv4).
- Under Launch script, paste the content of
server/bootstrap.sh. Include the first line,#!/bin/bash. Without it the script runs undersh, fails atset -o pipefailand nginx is never installed (see Troubleshooting). - Name it
defectrag-web. Add tagProject=DefectRAG. Create. - Networking β Create static IP and attach it to the instance (free while attached; it is billed if left unattached).
- Networking β IPv4 Firewall: keep HTTP 80 open and add HTTPS 443, both to everyone. Change SSH 22 to your own IP only (add your IP, remove "Any IP"). Some internet providers rotate your public address between blocks (check
https://checkip.amazonaws.coma few times). If it changes, allow the whole/24block of each address you see, not a single/32. Keep the Lightsail browser SSH tick, which works without your IP. - Download the default SSH key from Account β SSH keys, or use the browser SSH button.
2.3 Publish
export WEB_HOST=<your static IP>
export WEB_KEY=~/Downloads/LightsailDefaultKey.pem
bash deployment/aws/deploy_site.sh
Open http://<static IP>. You should see the same dashboard as the local preview.
2.4 HTTPS (recommended)
You need a domain name pointing at the static IP.
- Cheap: register a domain in Route 53 (check the price first; domain fees may not be covered by credits) or use any registrar.
- Free option: a free dynamic-DNS subdomain such as DuckDNS pointing at the static IP.
Then on the server (browser SSH, not your own computer). First tell nginx its name, because certbot looks for a matching server_name:
sudo sed -i 's/server_name _;/server_name your.domain.example;/' /etc/nginx/sites-available/defectrag
sudo nginx -t && sudo systemctl reload nginx
sudo certbot --nginx -d your.domain.example --agree-tos -m you@example.com --no-eff-email --redirect
sudo certbot renew --dry-run # proves automatic renewal works
2.5 Lightsail alarms
Lightsail β your instance β Metrics: add alarms for Status check failed (rule greater than or equal to 1; the metric is 0 or 1, so "greater than 1" never fires), CPU utilization > 80% for 15 min, and Burst capacity percentage < 10% (a $5 plan is burstable and is throttled when the capacity reaches 0). Tick Email. Lightsail alarms are Lightsail's own, not CloudWatch alarms.
Phase 3: Lambda (60 minutes)
Two small functions are in lambda/.
3.1 status_api.py: a live "status" endpoint
- Console β Lambda β Create function β Author from scratch β name
defectrag-statusβ Python 3.12 β Create. - Paste
lambda/status_api.pyinto the code editor, and set Runtime settings β Handler tolambda_function.handler(the console names the filelambda_function.py) β Deploy. UpdateSAVED_EXAMPLESwhen the showcase size changes (now 24). - Configuration β Function URL β Create β Auth type NONE β save. (It only returns harmless public data.)
- Configuration β Concurrency β reserve 5 so a flood of requests cannot run up a bill. New accounts often cannot (the total limit is 10 and AWS keeps 100 unreserved), so skip it there; the function is tiny, returns harmless data, and the Budgets alert is the safety net.
- Test:
curl <function-url>should return JSON. - Put the URL in the site:
export STATUS_URL=<function-url>and rundeploy_site.shagain. Openhttp://<ip>/status.html.
3.2 budget_guard.py: the stop-switch
- Console β SNS β Topics β Create topic (Standard)
defectrag-budget-alerts. - In the topic's Access policy, allow
budgets.amazonaws.comto publish (AWS Budgets console shows the exact policy when you add an SNS target). - Create Lambda
defectrag-budget-guard(Python 3.12), pastelambda/budget_guard.py. - Configuration β Permissions β edit the execution role, attach
lambda/budget_guard_policy.jsonas an inline policy. - Add trigger β SNS β
defectrag-budget-alerts. - Edit the
defectrag-monthlybudget β 100% alert β add the SNS topic. - Test safely: in Lambda, run the test event
{"dry_run": true}. It lists what would be stopped and stops nothing.
The guard only stops EC2 instances tagged Project=DefectRAG-lab. It cannot touch Lightsail or anything else.
Phase 4: CloudWatch monitoring (30 minutes)
Built and live. It watches the real Lambda functions, so no EC2 server is needed.
- SNS β Topics β Create topic (Standard)
defectrag-alerts, tagProject=DefectRAG. Add an Email subscription and click Confirm in the message you receive. Do not reusedefectrag-budget-alerts: the budget guard Lambda is subscribed to it, so an alarm posted there would trigger the guard. - CloudWatch β Alarms β Create alarm β metric Lambda β By Function Name β
defectrag-statusβ Errors. Statistic Sum, period 5 minutes, Greater/Equal 1, missing data treated as good (the function rarely runs). Action: notifydefectrag-alerts. Namedefectrag-status-errors. - CloudWatch β Dashboards β Create
defectrag. Add: a line widget fordefectrag-statusInvocations and Errors, a second for Duration (milliseconds, so it needs its own axis), an Alarm status widget fordefectrag-status-errors, and a widget fordefectrag-budget-guardInvocations/Errors (empty until a budget alert fires, which is expected). Click Save dashboard; unsaved widgets disappear. - Open
status.htmla few times so the graphs have data points. The alarm starts as "Insufficient data" and turns OK after a few minutes.
Phase 4b (optional, not done here): EC2 lab with the CloudWatch agent (90 minutes, then terminate)
- EC2 β Launch instance. Name
defectrag-lab. Amazon Linux 2023. Typet3.micro(or the type the Free Plan offers). TagProject=DefectRAG-lab. - Security group: SSH from your IP only, HTTP 80 from anywhere.
- Advanced β IAM instance profile: create a role with the managed policy
CloudWatchAgentServerPolicyand attach it. - Connect (EC2 Instance Connect), then:
sudo dnf install -y nginx amazon-cloudwatch-agent
sudo systemctl enable --now nginx
- Put the config from
cloudwatch/agent-config.jsonat/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json, then:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s \
-c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
- CloudWatch β Alarms β Create alarm:
CPUUtilizationβ₯ 70% for 2 of 3 five-minute periods β email via SNS.StatusCheckFailed_Systemβ₯ 1 β action Recover this instance.mem_used_percent(from the agent) β₯ 85% β email.
- CloudWatch β Dashboards β Create
defectrag: CPU, memory, disk, network, and a Logs Insights widget for nginx 5xx errors. - Practice an incident: run
sudo dnf install -y stress-ng && stress-ng --cpu 2 --timeout 600s, watch the alarm go from OK β ALARM and read the email. Write down what you saw. - Terminate the instance when finished (Instances β Instance state β Terminate). Check log retention is 7 days (Logs β Log groups β Actions β Edit retention).
Phase 5: Evidence for your resume
Take screenshots of: the Budgets page with alerts, the live site on HTTPS (padlock), the Lambda function with its URL test, the CloudWatch dashboard with the alarm showing OK, and the Lightsail firewall with SSH limited to your IP ranges (hide the addresses if you publish it). Suggested bullets (edit to match what you really did):
- Hosted a React/Vite showcase on Amazon Lightsail (Ubuntu, nginx, static IP, HTTPS via Let's Encrypt) with SSH restricted by firewall rule and repeatable deployment through a shell script.
- Built AWS Lambda functions behind a Function URL (status API) and an SNS-triggered cost guard, each with its own least-privilege IAM role, plus AWS Budgets alerts.
- Monitored the stack with a CloudWatch dashboard and an error alarm that emails through SNS, and with Lightsail alarms for status check, CPU and burst capacity.
- Kept the project under a set monthly budget using Budgets alerts, tagging and a documented teardown.
Phase 6: Teardown (do this before the Free Plan ends)
- If you did Phase 4b: terminate EC2 instances. Delete unattached EBS volumes and Elastic IPs.
- Lightsail: delete the instance's alarms with it, and delete the DuckDNS name if you no longer need it.
- Lightsail: delete the instance, then release the static IP (an unattached one is billed).
- Lambda: delete both functions. SNS: delete the topic.
- CloudWatch: delete dashboards, alarms, log groups.
- Budgets: keep one budget until the bill shows $0.00 for a full day.
- Billing β Bills page for the month shows no active charges.
Troubleshooting
| Problem | Likely fix |
|---|---|
| Site shows nginx welcome page | deploy_site.sh did not run, or files are not in /var/www/defectrag |
| Blank page after deploy | Run npm run build:showcase (not build). The normal build expects the Python backend |
403 on the site |
sudo chown -R www-data:www-data /var/www/defectrag |
| Cannot SSH (timeout) | Your IP changed. Some providers rotate between two blocks; allow the /24 of each, see 2.2 |
| nginx missing after first boot | Launch script ran under sh (set: Illegal option -o pipefail in /var/log/cloud-init-output.log). Re-run it with sudo bash |
sudo: disabled / command not found |
You ran the command on your own PC. Run it on the server (prompt ubuntu@ip-...) |
mktemp ... Permission denied in deploy |
Old script version; use the current deploy_site.sh (it uses sudo for /var/www) |
tar: file changed as we read it |
Old script version wrote the archive inside the folder it was packing; use the current one |
| Status alarm "Insufficient data" | Normal for a function that rarely runs; set missing data to "good" and wait a few minutes |
| Budget email never arrives | Confirm the SNS email subscription; billing data can lag several hours |
| Surprise charge | Billing β Cost Explorer, group by Service and Usage type |