Skip to content

How to Block Amazon AWS Traffic

If you need to block traffic originating from Amazon AWS IP ranges, you can use the methods below. Amazon publishes their IP ranges publicly, which makes it possible to generate .htaccess deny rules automatically.

These commands are run via SSH on your hosting account.

Terminal window
curl -sf https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[].ip_prefix'

To output deny statements you can add to your .htaccess file:

Terminal window
curl -sf https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[].ip_prefix' | awk '{print "deny from " $0}'

To add the deny rules directly to your existing .htaccess file:

Terminal window
curl -sf https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[].ip_prefix' | awk '{print "deny from " $0}' >> .htaccess

To create a complete block that allows all traffic except AWS:

Terminal window
echo "order allow,deny" >> .htaccess
echo "allow from all" >> .htaccess
curl -sf https://ip-ranges.amazonaws.com/ip-ranges.json | jq -r '.prefixes[].ip_prefix' | awk '{print "deny from " $0}' >> .htaccess