|
| 1 | +# WAF HTTP API Python Example |
| 2 | + |
| 3 | +This is a Python CDK example that demonstrates how to use the `waf-http-api` construct to create a WAF-protected HTTP API. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +This example creates: |
| 8 | + |
| 9 | +- **Lambda Function**: Python 3.12 runtime that returns a greeting with request information |
| 10 | +- **HTTP API Gateway**: RESTful API with multiple routes (`/` and `/hello`) |
| 11 | +- **WafHttpApi Construct**: Automatically creates CloudFront distribution and WAF WebACL |
| 12 | +- **Origin Verification**: Secret header mechanism to ensure requests come through CloudFront |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +- ✅ **Python 3.12 Lambda Runtime**: Latest Python runtime for optimal performance |
| 17 | +- ✅ **WafHttpApi Construct**: Uses the official `waf-http-api` package for simplified setup |
| 18 | +- ✅ **Comprehensive Security**: WAF protection with IP reputation and common rule sets |
| 19 | +- ✅ **Origin Verification**: CloudFront adds secret headers to verify legitimate requests |
| 20 | +- ✅ **Multiple HTTP Methods**: Supports GET and POST requests |
| 21 | +- ✅ **CORS Support**: Configured for cross-origin requests |
| 22 | +- ✅ **Detailed Logging**: Request information and verification status |
| 23 | +- ✅ **Infrastructure as Code**: Fully defined using AWS CDK Python |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +- Python 3.8 or later |
| 28 | +- AWS CDK CLI installed (`npm install -g aws-cdk`) |
| 29 | +- AWS credentials configured |
| 30 | + |
| 31 | +## Setup |
| 32 | + |
| 33 | +1. **Create and activate virtual environment**: |
| 34 | + |
| 35 | + ```bash |
| 36 | + python3 -m venv .venv |
| 37 | + source .venv/bin/activate # On Windows: .venv\Scripts\activate.bat |
| 38 | + ``` |
| 39 | + |
| 40 | +2. **Install dependencies**: |
| 41 | + |
| 42 | + ```bash |
| 43 | + pip install -r requirements.txt |
| 44 | + pip install -r requirements-dev.txt |
| 45 | + ``` |
| 46 | + |
| 47 | + This will install: |
| 48 | + |
| 49 | + - `aws-cdk-lib`: AWS CDK core library |
| 50 | + - `constructs`: CDK constructs framework |
| 51 | + - `waf-http-api`: The WAF HTTP API construct package |
| 52 | + - `pytest`: Testing framework |
| 53 | + |
| 54 | +## Usage |
| 55 | + |
| 56 | +### Build and Test |
| 57 | + |
| 58 | +```bash |
| 59 | +# Run tests |
| 60 | +pytest |
| 61 | + |
| 62 | +# Synthesize CloudFormation template |
| 63 | +cdk synth |
| 64 | + |
| 65 | +# Deploy to AWS |
| 66 | +cdk deploy |
| 67 | + |
| 68 | +# Clean up resources |
| 69 | +cdk destroy |
| 70 | +``` |
| 71 | + |
| 72 | +### Testing the API |
| 73 | + |
| 74 | +After deployment, you'll get several outputs: |
| 75 | + |
| 76 | +- **CloudFrontUrl**: Use this endpoint for production traffic (recommended) |
| 77 | +- **HttpApiUrl**: Direct API Gateway URL (not recommended for production) |
| 78 | + |
| 79 | +```bash |
| 80 | +# Test the API through CloudFront (recommended) |
| 81 | +curl https://d1234567890.cloudfront.net/ |
| 82 | + |
| 83 | +# Test the hello endpoint |
| 84 | +curl https://d1234567890.cloudfront.net/hello |
| 85 | + |
| 86 | +# POST request |
| 87 | +curl -X POST https://d1234567890.cloudfront.net/hello \ |
| 88 | + -H "Content-Type: application/json" \ |
| 89 | + -d '{"test": "data"}' |
| 90 | +``` |
| 91 | + |
| 92 | +## Project Structure |
| 93 | + |
| 94 | +``` |
| 95 | +example/python/ |
| 96 | +├── app.py # CDK app entry point |
| 97 | +├── waf_http_api_example/ |
| 98 | +│ ├── __init__.py |
| 99 | +│ └── waf_http_api_example_stack.py # Main stack definition |
| 100 | +├── tests/ |
| 101 | +│ ├── __init__.py |
| 102 | +│ └── test_waf_http_api_example.py # Comprehensive test suite |
| 103 | +├── requirements.txt # Runtime dependencies |
| 104 | +├── requirements-dev.txt # Development dependencies |
| 105 | +├── cdk.json # CDK configuration |
| 106 | +└── README.md # This file |
| 107 | +``` |
| 108 | + |
| 109 | +## Key Components |
| 110 | + |
| 111 | +### Lambda Function |
| 112 | + |
| 113 | +- **Runtime**: Python 3.12 |
| 114 | +- **Handler**: Inline code that processes HTTP requests |
| 115 | +- **Features**: Origin verification, detailed logging, CORS headers |
| 116 | + |
| 117 | +### HTTP API Gateway |
| 118 | + |
| 119 | +- **Type**: HTTP API (faster and cheaper than REST API) |
| 120 | +- **Routes**: `GET /`, `GET /hello`, `POST /hello` |
| 121 | +- **Integration**: Lambda proxy integration |
| 122 | + |
| 123 | +### WafHttpApi Construct |
| 124 | + |
| 125 | +- **Package**: `waf_http_api` from PyPI |
| 126 | +- **Features**: Automatically creates and configures CloudFront and WAF |
| 127 | +- **Configuration**: Simple API with sensible defaults |
| 128 | +- **Extensibility**: Support for custom domains and WAF rules |
| 129 | + |
| 130 | +### CloudFront Distribution (via WafHttpApi) |
| 131 | + |
| 132 | +- **Caching**: Optimized for API responses |
| 133 | +- **Security**: WAF integration for DDoS and application-layer protection |
| 134 | +- **Origin**: HTTP API Gateway with custom verification headers |
| 135 | + |
| 136 | +### WAF WebACL (via WafHttpApi) |
| 137 | + |
| 138 | +- **Scope**: CloudFront (global) |
| 139 | +- **Rules**: |
| 140 | + - AWS Managed IP Reputation List |
| 141 | + - AWS Managed Common Rule Set |
| 142 | +- **Action**: Allow by default, block malicious traffic |
| 143 | + |
| 144 | +## Security Features |
| 145 | + |
| 146 | +1. **WAF Protection**: Blocks malicious requests before they reach your API |
| 147 | +2. **Origin Verification**: Secret headers ensure requests come through CloudFront |
| 148 | +3. **HTTPS Only**: All traffic redirected to HTTPS |
| 149 | +4. **IP Reputation**: Automatic blocking of known malicious IP addresses |
| 150 | +5. **Common Attack Protection**: OWASP Top 10 and common web vulnerabilities |
| 151 | + |
| 152 | +## Monitoring and Observability |
| 153 | + |
| 154 | +- **CloudWatch Logs**: Lambda function logs for debugging |
| 155 | +- **WAF Metrics**: Request counts, blocked requests, rule matches |
| 156 | +- **CloudFront Metrics**: Cache hit rates, origin response times |
| 157 | +- **API Gateway Metrics**: Request counts, latency, error rates |
| 158 | + |
| 159 | +## Cost Optimization |
| 160 | + |
| 161 | +- **HTTP API**: More cost-effective than REST API |
| 162 | +- **CloudFront**: Reduces origin load and improves performance |
| 163 | +- **Lambda**: Pay-per-request pricing with efficient Python runtime |
| 164 | +- **WAF**: Only pay for requests processed |
| 165 | + |
| 166 | +## Development |
| 167 | + |
| 168 | +### Running Tests |
| 169 | + |
| 170 | +```bash |
| 171 | +# Run all tests |
| 172 | +pytest |
| 173 | + |
| 174 | +# Run with verbose output |
| 175 | +pytest -v |
| 176 | + |
| 177 | +# Run specific test |
| 178 | +pytest tests/test_waf_http_api_example.py::TestWafHttpApiExampleStack::test_lambda_function_created_with_python_312 |
| 179 | +``` |
| 180 | + |
| 181 | +### Adding Custom WAF Rules |
| 182 | + |
| 183 | +You can extend the WAF configuration by adding custom rules to the `web_acl` in the stack: |
| 184 | + |
| 185 | +```python |
| 186 | +# Add rate limiting rule |
| 187 | +rate_limit_rule = wafv2.CfnWebACL.RuleProperty( |
| 188 | + name="RateLimitRule", |
| 189 | + priority=10, |
| 190 | + action=wafv2.CfnWebACL.RuleActionProperty(block={}), |
| 191 | + statement=wafv2.CfnWebACL.StatementProperty( |
| 192 | + rate_based_statement=wafv2.CfnWebACL.RateBasedStatementProperty( |
| 193 | + limit=2000, |
| 194 | + aggregate_key_type="IP" |
| 195 | + ) |
| 196 | + ), |
| 197 | + visibility_config=wafv2.CfnWebACL.VisibilityConfigProperty( |
| 198 | + cloud_watch_metrics_enabled=True, |
| 199 | + metric_name="RateLimitRule", |
| 200 | + sampled_requests_enabled=True |
| 201 | + ) |
| 202 | +) |
| 203 | +``` |
| 204 | + |
| 205 | +## Troubleshooting |
| 206 | + |
| 207 | +### Common Issues |
| 208 | + |
| 209 | +1. **403 Forbidden**: Check that requests are going through CloudFront, not directly to API Gateway |
| 210 | +2. **Origin Verification Failed**: Ensure the secret header is properly configured |
| 211 | +3. **WAF Blocking Legitimate Traffic**: Review WAF logs and adjust rules if needed |
| 212 | + |
| 213 | +### Debugging |
| 214 | + |
| 215 | +1. **Check Lambda Logs**: View CloudWatch logs for the Lambda function |
| 216 | +2. **WAF Logs**: Enable WAF logging to S3 for detailed analysis |
| 217 | +3. **CloudFront Logs**: Enable access logs for request analysis |
| 218 | + |
| 219 | +## License |
| 220 | + |
| 221 | +This example is provided under the same license as the main waf-http-api project. |
0 commit comments