|
| 1 | +from aws_cdk import aws_apigateway as apigateway |
| 2 | +from aws_cdk import aws_wafv2 as waf |
| 3 | +from constructs import Construct |
| 4 | + |
| 5 | + |
| 6 | +class WafToApiGatewayConstruct(Construct): |
| 7 | + def __init__(self, scope: Construct, id: str, api: apigateway.RestApi, **kwargs) -> None: |
| 8 | + super().__init__(scope, id, **kwargs) |
| 9 | + |
| 10 | + # Create WAF WebACL with AWS Managed Rules |
| 11 | + web_acl = waf.CfnWebACL( |
| 12 | + self, |
| 13 | + 'ProductApiGatewayWebAcl', |
| 14 | + scope='REGIONAL', # Change to CLOUDFRONT if you're using edge-optimized API |
| 15 | + default_action=waf.CfnWebACL.DefaultActionProperty(allow={}), |
| 16 | + name=f'{id}-Waf', |
| 17 | + visibility_config=waf.CfnWebACL.VisibilityConfigProperty( |
| 18 | + sampled_requests_enabled=True, cloud_watch_metrics_enabled=True, metric_name='ProductApiGatewayWebAcl' |
| 19 | + ), |
| 20 | + rules=[ |
| 21 | + waf.CfnWebACL.RuleProperty( |
| 22 | + name='Product-AWSManagedRulesCommonRuleSet', |
| 23 | + priority=0, |
| 24 | + override_action={'none': {}}, |
| 25 | + statement=waf.CfnWebACL.StatementProperty( |
| 26 | + managed_rule_group_statement=waf.CfnWebACL.ManagedRuleGroupStatementProperty( |
| 27 | + name='AWSManagedRulesCommonRuleSet', vendor_name='AWS' |
| 28 | + ) |
| 29 | + ), |
| 30 | + visibility_config=waf.CfnWebACL.VisibilityConfigProperty( |
| 31 | + sampled_requests_enabled=True, |
| 32 | + cloud_watch_metrics_enabled=True, |
| 33 | + metric_name='Product-AWSManagedRulesCommonRuleSet', |
| 34 | + ), |
| 35 | + ), |
| 36 | + # Block Amazon IP reputation list managed rule group |
| 37 | + waf.CfnWebACL.RuleProperty( |
| 38 | + name='Product-AWSManagedRulesAmazonIpReputationList', |
| 39 | + priority=1, |
| 40 | + override_action={'none': {}}, |
| 41 | + statement=waf.CfnWebACL.StatementProperty( |
| 42 | + managed_rule_group_statement=waf.CfnWebACL.ManagedRuleGroupStatementProperty( |
| 43 | + name='AWSManagedRulesAmazonIpReputationList', vendor_name='AWS' |
| 44 | + ) |
| 45 | + ), |
| 46 | + visibility_config=waf.CfnWebACL.VisibilityConfigProperty( |
| 47 | + sampled_requests_enabled=True, |
| 48 | + cloud_watch_metrics_enabled=True, |
| 49 | + metric_name='Product-AWSManagedRulesAmazonIpReputationList', |
| 50 | + ), |
| 51 | + ), |
| 52 | + # Block Anonymous IP list managed rule group |
| 53 | + waf.CfnWebACL.RuleProperty( |
| 54 | + name='Product-AWSManagedRulesAnonymousIpList', |
| 55 | + priority=2, |
| 56 | + override_action={'none': {}}, |
| 57 | + statement=waf.CfnWebACL.StatementProperty( |
| 58 | + managed_rule_group_statement=waf.CfnWebACL.ManagedRuleGroupStatementProperty( |
| 59 | + name='AWSManagedRulesAnonymousIpList', vendor_name='AWS' |
| 60 | + ) |
| 61 | + ), |
| 62 | + visibility_config=waf.CfnWebACL.VisibilityConfigProperty( |
| 63 | + sampled_requests_enabled=True, |
| 64 | + cloud_watch_metrics_enabled=True, |
| 65 | + metric_name='Product-AWSManagedRulesAnonymousIpList', |
| 66 | + ), |
| 67 | + ), |
| 68 | + # rule for blocking known Bad Inputs |
| 69 | + waf.CfnWebACL.RuleProperty( |
| 70 | + name='Product-AWSManagedRulesKnownBadInputsRuleSet', |
| 71 | + priority=3, |
| 72 | + override_action={'none': {}}, |
| 73 | + statement=waf.CfnWebACL.StatementProperty( |
| 74 | + managed_rule_group_statement=waf.CfnWebACL.ManagedRuleGroupStatementProperty( |
| 75 | + name='AWSManagedRulesKnownBadInputsRuleSet', vendor_name='AWS' |
| 76 | + ) |
| 77 | + ), |
| 78 | + visibility_config=waf.CfnWebACL.VisibilityConfigProperty( |
| 79 | + sampled_requests_enabled=True, |
| 80 | + cloud_watch_metrics_enabled=True, |
| 81 | + metric_name='Product-AWSManagedRulesKnownBadInputsRuleSet', |
| 82 | + ), |
| 83 | + ), |
| 84 | + ], |
| 85 | + ) |
| 86 | + |
| 87 | + # Associate WAF with API Gateway |
| 88 | + waf.CfnWebACLAssociation(self, 'ApiGatewayWafAssociation', resource_arn=api.deployment_stage.stage_arn, web_acl_arn=web_acl.attr_arn) |
0 commit comments