15. Amazon RDS for MySQL: Create, Connect, Back Up and Keep It Private
15.2 Planning the Network: Subnet Group and Security Groups
RDS banvnyaadhi network plan kara. Nantar badalne tras aahe.
| Item | Setting for our project |
|---|---|
| VPC | The same VPC as your EC2 web server (default VPC is fine for learning) |
| DB subnet group | At least two subnets in different Availability Zones (private subnets in production) |
| Public access | No |
| Security group for EC2 | web-sg: inbound 80/443 from anywhere, 22 from your IP only |
| Security group for RDS | db-sg: inbound MySQL/Aurora 3306 with source = web-sg (the security group, not an IP) |
Using a security group as the source means "any instance that has web-sg attached may connect" – if the web server's IP changes, the rule still works, and nothing else in the world can reach 3306.
# create db-sg and allow 3306 only from web-sg (IDs are placeholders)
aws ec2 create-security-group --group-name db-sg --description "RDS MySQL" --vpc-id vpc-0abc1234
aws ec2 authorize-security-group-ingress --group-id sg-0db11111 \
--protocol tcp --port 3306 --source-group sg-0web2222
aws ec2 describe-security-groups --group-ids sg-0db11111 --query "SecurityGroups[0].IpPermissions"
Why this matters for security
Port 3306 open to 0.0.0.0/0 is one of the first things scanners like Nmap and Shodan-style search engines find. Attackers then run password brute force against the database directly. With a private DB and a security-group-to-security-group rule, the database simply does not exist for the internet.
Ravindra Bagale's Tip
Because they can't connect, students put source 0.0.0.0/0 in db-sg – "I'll remove it later" – and forget. Never! Choose web-sg as the source. If you really need to look at the database from your laptop, use an SSH tunnel (shown in 15.4).
Ravindra Bagale's Tip – मराठी
Connect होत नाही म्हणून students db-sg मध्ये source 0.0.0.0/0 टाकतात – "नंतर काढू" – आणि विसरतात. कधी नाही! Source मध्ये web-sg निवडा. Laptop वरून database बघायचाच असेल तर SSH tunnel वापरा (15.4 मध्ये दाखवलं आहे).
Ravindra Bagale's Tip – हिंदी
Connect नहीं होता इसलिए students db-sg में source 0.0.0.0/0 डाल देते हैं – "बाद में हटा देंगे" – और भूल जाते हैं. कभी नहीं! Source में web-sg चुनो. Laptop से database देखना ही हो तो SSH tunnel इस्तेमाल करो (15.4 में दिखाया है).
Practice task
Create web-sg and db-sg in your VPC. Add the 3306 rule to db-sg with web-sg as the source and take a screenshot of the inbound rules for your notes.
Extended teaching notes
Why this lesson matters
Use this lesson to strengthen systems you own: understand the risk, practise safely in a lab, then write and apply the fix.
Apply the fix across stacks
Same class of bug, same idea in every language.
Java / Spring Boot
Parameterise queries, encode output, CSRF tokens, limited Actuator, dependency scanning.
ASP.NET Core
EF Core / parameterised SQL, Razor encoding, HTTPS, Data Protection.
Node.js / Express
helmet, parameterised SQL, npm audit, avoid eval and shell concat.
Python / Django / Flask / FastAPI
ORM, autoescape, pip-audit, Bandit; never shell=True with user input.
PHP / Laravel
PDO/Eloquent, Blade escaping, composer audit.
Go / Ruby
database/sql placeholders / ActiveRecord; html/template / ERB escaping; govulncheck / Brakeman.
nginx / Apache / Tomcat
Hide versions, TLS, security headers, least-open ports.
MySQL / PostgreSQL / MongoDB / Redis
Private networks, auth on, least-privilege users, no 0.0.0.0/0.
AWS / Azure / GCP
Roles not long-lived keys, private data stores, cloud audit logs on.
Mini case study
Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.
Defender's worksheet (do this today)
- Write the lesson title, date and the exact lab target you are allowed to use.
- List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
- Run one safe check from the lesson on a lab system only. Paste the command and output.
- Write a two-sentence fix you would give a teammate.
- Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.
Indian workplace (sample data)
Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).
Global practice
Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.
Everyday analogy
Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.
Key terms
- Control: a safeguard (patch, config, process, detection).
- Finding: a weakness backed by evidence.
- Remediation: the fix plus how you verified it.
- Scope: systems you have written permission to touch.
- Least privilege: only the access the task needs.
- Defence in depth: several layers so one failure does not end the story.
Common mistakes
- Skipping written permission "just for a quick scan".
- Practising on hostel, office or public targets without authorisation.
- Collecting tool output without writing the fix.
- Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
- Publishing screenshots that contain secrets, cookies or internal hostnames.
Recap
- Evidence, fix, owner, date.
- Own lab only.
- Fix > spectacle.