Skip to content

Commit cfaeabd

Browse files
ran-isenbergRan Isenberg
andauthored
feature: add waf logging (#895)
--------- Co-authored-by: Ran Isenberg <ran.isenberg@ranthebuilder.cloud>
1 parent 815613b commit cfaeabd

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

cdk/service/api_construct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, scope: Construct, id_: str, appconfig_app_name: str, is_produ
2828
self._build_swagger_endpoints(rest_api=self.rest_api, dest_func=self.create_order_func)
2929
self.monitoring = CrudMonitoring(self, id_, self.rest_api, self.api_db.db, self.api_db.idempotency_db, [self.create_order_func])
3030

31-
if is_production_env:
31+
if not is_production_env:
3232
# add WAF
3333
self.waf = WafToApiGatewayConstruct(self, f'{id_}waf', self.rest_api)
3434

cdk/service/waf_construct.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from aws_cdk import Aws, CfnOutput, RemovalPolicy
12
from aws_cdk import aws_apigateway as apigateway
3+
from aws_cdk import aws_iam as iam
4+
from aws_cdk import aws_logs as logs
25
from aws_cdk import aws_wafv2 as waf
36
from constructs import Construct
47

@@ -86,3 +89,40 @@ def __init__(self, scope: Construct, id: str, api: apigateway.RestApi, **kwargs)
8689

8790
# Associate WAF with API Gateway
8891
waf.CfnWebACLAssociation(self, 'ApiGatewayWafAssociation', resource_arn=api.deployment_stage.stage_arn, web_acl_arn=web_acl.attr_arn)
92+
93+
# Enable logging for WAF, must start with 'aws-waf-logs-' prefix
94+
log_group_name = f'aws-waf-logs-{id}'
95+
# Create CloudWatch Log Group for WAF logging
96+
waf_log_group = logs.LogGroup(
97+
self,
98+
'WafLogGroup',
99+
log_group_name=log_group_name,
100+
retention=logs.RetentionDays.TWO_WEEKS,
101+
removal_policy=RemovalPolicy.DESTROY,
102+
)
103+
104+
# Attach resource policy to allow WAF to write to the log group
105+
waf_log_group.add_to_resource_policy(
106+
iam.PolicyStatement(
107+
effect=iam.Effect.ALLOW,
108+
principals=[iam.AnyPrincipal()],
109+
actions=['logs:PutLogEvents', 'logs:CreateLogStream', 'logs:DescribeLogGroups'],
110+
resources=[f'{waf_log_group.log_group_arn}:*'],
111+
)
112+
)
113+
114+
# Output the Log Group ARN for visibility
115+
CfnOutput(self, id='WafLogGroupArn', value=waf_log_group.log_group_arn).override_logical_id('WafLogGroupArn')
116+
117+
# Construct the Log Group ARN manually as its not available in the CDK
118+
log_group_arn = f'arn:{Aws.PARTITION}:logs:{Aws.REGION}:{Aws.ACCOUNT_ID}:log-group:{log_group_name}:*'
119+
120+
enable_waf_logging = waf.CfnLoggingConfiguration(
121+
self,
122+
'WafLoggingConfiguration',
123+
resource_arn=web_acl.attr_arn,
124+
log_destination_configs=[log_group_arn],
125+
)
126+
127+
web_acl.node.add_dependency(waf_log_group)
128+
enable_waf_logging.node.add_dependency(web_acl) # Ensure WebACL is created first

0 commit comments

Comments
 (0)