AWS — WAF
What is WAF?
Protects web applications from common attacks.
Create web ACL
aws wafv2 create-web-acl \
--name my-web-acl \
--scope REGIONAL \
--default-action Allow={} \
--rules '[
{
"Name": "RateLimit",
"Priority": 1,
"Statement": {"RateBasedStatement": {"Limit": 1000, "AggregateKeyType": "IP"}},
"Action": {"Block": {}},
"VisibilityConfig": {"SampledRequestsEnabled": true, "CloudWatchMetricsEnabled": true, "MetricName": "RateLimit"}
}
]' \
--visibility-config 'SampledRequestsEnabled=true,CloudWatchMetricsEnabled=true,MetricName=my-web-acl'
Common rules
| Rule | Protection |
|---|---|
| SQL Injection | SQL attacks |
| XSS | Cross-site scripting |
| IP Reputation | Known bad IPs |
| Rate Limiting | DDoS protection |
Associate with ALB
aws wafv2 associate-web-acl \
--web-acl-arn arn:aws:wafv2:us-east-1:123456789:regional/webacl/my-web-acl/xxx \
--resource-arn arn:aws:elasticloadbalancing:us-east-1:123456789:loadbalancer/app/my-alb/xxx
Custom rules
import boto3
waf = boto3.client('wafv2')
# Create rule
rule = {
'Name': 'CustomRule',
'Priority': 1,
'Statement': {
'ByteMatchStatement': {
'SearchString': 'bad-input',
'FieldToMatch': {'Body': {}},
'TextTransformations': [{'Priority': 0, 'Type': 'LOWERCASE'}]
}
},
'Action': {'Block': {}}
}
Best practices
- Use managed rule groups
- Enable logging
- Test with count mode
- Monitor with CloudWatch
Mini Practice
- Create a web ACL
- Add rate limiting rule
- Associate with ALB
- Test and monitor
Up Next
Continue with Shield - DDoS Protection.
Related Topics
Frequently Asked Questions about WAF
What is WAF in AWS?
WAF is a fundamental concept in AWS. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn WAF?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn WAF.
Why is WAF important in AWS?
WAF is essential for AWS development. Understanding this concept will help you write better code and solve real-world problems more effectively.