You're running a server on Amazon EC2, everything's smooth, and then bam—traffic spikes out of nowhere, your site crawls to a halt, and you're stuck waiting 30 minutes or rebooting just to get back online. Sound familiar? If you're dealing with recurring DDoS attacks every few months, you're not alone, and the good news is there are practical ways to harden your setup beyond Amazon's basic security groups.
DDoS attacks aren't always personal. Sometimes your server just happens to be visible, or attackers are probing for weak spots across cloud infrastructure. EC2 instances are public-facing by default, which makes them easy targets if you haven't layered proper defenses. The attack floods your server with junk requests—either overwhelming your bandwidth, exhausting server resources, or both.
Before diving into advanced protection, make sure you've got these basics covered:
AWS Shield Standard comes free with every EC2 instance. It blocks common network and transport layer attacks automatically, but it won't stop everything. For larger or more sophisticated attacks, you'd need Shield Advanced, which costs money but gives you 24/7 DDoS response team access.
Security groups and NACLs act as your first firewall layer. Tighten these down—only allow necessary ports (like 80, 443 for web traffic) and whitelist trusted IP ranges when possible. Don't leave SSH or database ports open to the world.
CloudFront as a buffer sits between users and your origin server. If you're running a web application, routing traffic through CloudFront means attackers hit AWS's edge locations first, not your EC2 instance directly. This absorbs a lot of the impact before it reaches you.
This is where you get hands-on with your server configuration. iptables or UFW (Uncomplicated Firewall) can throttle requests from individual IPs. For example, you can set rules that drop connections if someone sends more than 20 requests per second. Here's a simple iptables approach:
iptables -A INPUT -p tcp --dport 80 -m state --state NEW -m recent --set
iptables -A INPUT -p tcp --dport 80 -m state --state NEW -m recent --update --seconds 10 --hitcount 20 -j DROP
This limits new connections to 20 per 10 seconds per IP. Adjust the numbers based on your traffic patterns, but don't go too aggressive or you'll block legitimate users.
For web servers like Nginx or Apache, use connection limiting modules. Nginx has limit_req_zone and limit_conn_zone directives that cap requests per IP at the application layer, which catches attacks before they even hit your backend.
If your application can afford to scale horizontally, an Elastic Load Balancer (ELB) paired with an Auto Scaling group is your best friend. When traffic spikes, Auto Scaling automatically spins up additional EC2 instances to distribute the load. The attack still happens, but it's spread across multiple servers instead of crushing a single one.
Set your scaling policies to trigger based on CPU usage, network traffic, or request count. This way, even if attackers flood you with requests, your infrastructure expands to absorb it, then scales back down when things calm down.
MySQL and other databases can choke under DDoS pressure if every incoming request opens a new connection. Use connection pooling to reuse existing database connections instead of creating new ones for every query. This keeps your database from getting overwhelmed.
Also, configure max_connections and wait_timeout in your MySQL config to prevent connection exhaustion. If you're using something like PHP or Node.js, make sure your application isn't creating unbounded connections during traffic spikes.
You won't always catch an attack in real time unless you're watching. CloudWatch is built into AWS and can alert you when traffic or resource usage spikes abnormally. Set up alarms for metrics like network in/out, CPU utilization, and request counts.
For deeper visibility, Nagios works well for monitoring server health and can trigger alerts when something looks off. Pair it with fail2ban, which automatically bans IPs that show malicious patterns (like repeated failed login attempts or excessive requests).
If you want more advanced DDoS detection, consider tools like Cloudflare (which sits in front of your site like CloudFront but with more aggressive filtering) or AWS WAF (Web Application Firewall), which lets you create custom rules to block specific attack patterns.
Here's a quick checklist you can run through today:
Tighten security groups: Only expose necessary ports, restrict SSH to your IP or a VPN.
Enable CloudFront: Route web traffic through a CDN to absorb attack volume.
Set up rate limiting: Use iptables, UFW, or your web server's rate limit features.
Configure Auto Scaling: Build elasticity into your infrastructure so it can handle surges.
Monitor with CloudWatch: Set alarms for unusual traffic or resource spikes.
Install fail2ban: Automatically block repeat offenders at the IP level.
If you're still getting hammered after implementing these measures, it might be time to consider AWS Shield Advanced or a dedicated DDoS mitigation service. Shield Advanced costs around $3,000/month but includes cost protection (so you don't get charged for attack traffic), DDoS response team support, and advanced detection.
Alternatively, third-party services like Cloudflare's DDoS protection or Akamai can sit in front of your entire infrastructure and filter out attacks before they touch AWS at all.
DDoS attacks are frustrating, but they're not unstoppable. The key is layering defenses—start with AWS's built-in tools, add rate limiting and monitoring, then scale up with elastic infrastructure if attacks persist. Most importantly, don't wait for the next attack to hit before you act. Get your defenses in place now, and you'll sleep better knowing your server can handle whatever gets thrown at it.