-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_world_frontend_stack.py
More file actions
1339 lines (1290 loc) · 65.7 KB
/
hello_world_frontend_stack.py
File metadata and controls
1339 lines (1290 loc) · 65.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import Any
from aws_cdk import (
CfnOutput,
CustomResourceProvider,
Duration,
Fn,
RemovalPolicy,
Stack,
)
from aws_cdk import (
aws_athena as athena,
)
from aws_cdk import (
aws_cloudfront as cloudfront,
)
from aws_cdk import (
aws_cloudfront_origins as origins,
)
from aws_cdk import (
aws_cloudtrail as cloudtrail,
)
from aws_cdk import (
aws_cognito as cognito,
)
from aws_cdk import (
aws_glue as glue,
)
from aws_cdk import (
aws_iam as iam,
)
from aws_cdk import (
aws_kms as kms,
)
from aws_cdk import (
aws_logs as logs,
)
from aws_cdk import (
aws_rum as rum,
)
from aws_cdk import (
aws_s3 as s3,
)
from aws_cdk import (
aws_s3_deployment as s3deploy,
)
from aws_cdk import (
custom_resources as cr,
)
from cdk_nag import NagSuppressions
from constructs import Construct
from hello_world.nag_utils import (
CDK_LAMBDA_SUPPRESSIONS,
apply_compliance_aspects,
attach_async_failure_destination,
grant_logs_service_to_key,
suppress_cdk_singletons,
)
class HelloWorldFrontendStack(Stack):
"""CDK stack for the Hello World frontend.
Provisions a private S3 bucket for static assets and a CloudFront
distribution with OAC, HTTPS-only enforcement, and security response
headers. WAF protection is provided by a WebACL ARN passed in from
HelloWorldWafStack, which is always deployed in us-east-1.
This stack can be deployed to any region. When the target region differs
from us-east-1, CDK bridges the WAF ARN cross-region automatically via
SSM Parameter Store (enabled by cross_region_references=True in app.py).
"""
def __init__(self, scope: Construct, construct_id: str, api_url: str, waf_acl_arn: str, **kwargs: Any) -> None:
"""Provision all frontend AWS resources.
Args:
scope: The CDK construct scope.
construct_id: The unique identifier for this stack.
api_url: The backend API Gateway URL, injected into config.json at deploy time.
waf_acl_arn: ARN of the WAF WebACL from HelloWorldWafStack (always in us-east-1).
**kwargs: Additional keyword arguments passed to the parent Stack.
"""
super().__init__(scope, construct_id, **kwargs)
apply_compliance_aspects(self)
# ── KMS key ──────────────────────────────────────────────────────────
# Used to encrypt the frontend S3 bucket and CloudWatch log group.
# CloudWatch Logs requires the Logs service principal in the key policy.
frontend_encryption_key = kms.Key(
self,
"FrontendEncryptionKey",
description=f"KMS key for {self.stack_name} S3 bucket and log groups",
enable_key_rotation=True,
# See HelloWorldApp.encryption_key for the rationale — automated
# rotation, no dependent redeploys, 90-day compliance baseline.
rotation_period=Duration.days(90),
removal_policy=RemovalPolicy.DESTROY,
)
# Confused-deputy guard on the CMK's CloudWatch Logs service grant.
# See ``grant_logs_service_to_key`` in ``nag_utils.py``.
grant_logs_service_to_key(
frontend_encryption_key,
region=self.region,
account=self.account,
partition=self.partition,
)
# ── S3 access logging bucket ─────────────────────────────────────────
# Receives both S3 server access logs and CloudFront standard access
# logs. Must use SSE-S3 (not SSE-KMS) because neither the S3 log
# delivery service nor CloudFront standard logging support KMS-encrypted
# target buckets. This bucket itself does not need access logging (that
# would be circular), versioning, or replication.
access_log_bucket = s3.Bucket(
self,
"FrontendAccessLogBucket",
block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
encryption=s3.BucketEncryption.S3_MANAGED,
enforce_ssl=True,
# CloudFront standard logging requires ACL-based delivery — the bucket owner
# needs FULL_CONTROL on delivered log objects. BUCKET_OWNER_PREFERRED keeps
# Object Ownership set so ACLs remain usable for CloudFront log delivery.
object_ownership=s3.ObjectOwnership.BUCKET_OWNER_PREFERRED,
versioned=False,
# 7-day expiration cap on every prefix in this bucket (S3 access logs,
# CloudFront standard logs, Athena query results). Tunable: extend
# the duration, swap to a tiered transition (Standard-IA at 30d,
# Glacier Instant Retrieval at 90d, Glacier Deep Archive at 180d),
# or layer per-prefix rules if logs and Athena results need
# different retention.
lifecycle_rules=[
s3.LifecycleRule(
id="ExpireAfter7Days",
enabled=True,
expiration=Duration.days(7),
abort_incomplete_multipart_upload_after=Duration.days(1),
),
],
removal_policy=RemovalPolicy.DESTROY,
auto_delete_objects=True,
)
access_log_bucket_suppressions = [
("AwsSolutions-S1", "This IS the access log bucket — logging to itself would be circular"),
(
"NIST.800.53.R5-S3BucketLoggingEnabled",
"This IS the access log bucket — logging to itself would be circular",
),
(
"NIST.800.53.R5-S3DefaultEncryptionKMS",
"S3 log delivery service does not support KMS-encrypted target buckets; SSE-S3 is used instead",
),
(
"HIPAA.Security-S3DefaultEncryptionKMS",
"S3 log delivery service does not support KMS-encrypted target buckets; SSE-S3 is used instead",
),
(
"PCI.DSS.321-S3DefaultEncryptionKMS",
"S3 log delivery service does not support KMS-encrypted target buckets; SSE-S3 is used instead",
),
(
"NIST.800.53.R5-S3BucketVersioningEnabled",
"Versioning not needed for log bucket — logs are append-only and transient",
),
(
"HIPAA.Security-S3BucketVersioningEnabled",
"Versioning not needed for log bucket — logs are append-only and transient",
),
(
"PCI.DSS.321-S3BucketVersioningEnabled",
"Versioning not needed for log bucket — logs are append-only and transient",
),
("NIST.800.53.R5-S3BucketReplicationEnabled", "Replication not needed for log bucket in sample app"),
("HIPAA.Security-S3BucketReplicationEnabled", "Replication not needed for log bucket in sample app"),
("PCI.DSS.321-S3BucketReplicationEnabled", "Replication not needed for log bucket in sample app"),
]
NagSuppressions.add_resource_suppressions(
access_log_bucket,
[{"id": rule, "reason": reason} for rule, reason in access_log_bucket_suppressions],
)
# ── S3 bucket ────────────────────────────────────────────────────────
# Fully private — CloudFront OAC is the only allowed reader.
# KMS-encrypted with server access logging to access_log_bucket.
bucket = s3.Bucket(
self,
"FrontendBucket",
block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
encryption=s3.BucketEncryption.KMS,
encryption_key=frontend_encryption_key,
enforce_ssl=True,
server_access_logs_bucket=access_log_bucket,
server_access_logs_prefix="s3-access-logs/",
versioned=False,
removal_policy=RemovalPolicy.DESTROY,
auto_delete_objects=True,
)
self._create_s3_audit_trail(audited_buckets=[bucket, access_log_bucket], encryption_key=frontend_encryption_key)
# ── CloudFront distribution ──────────────────────────────────────────
distribution = cloudfront.Distribution(
self,
"Distribution",
default_behavior=cloudfront.BehaviorOptions(
origin=origins.S3BucketOrigin.with_origin_access_control(bucket),
viewer_protocol_policy=cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
cache_policy=cloudfront.CachePolicy.CACHING_OPTIMIZED,
response_headers_policy=cloudfront.ResponseHeadersPolicy.SECURITY_HEADERS,
),
default_root_object="index.html",
error_responses=[
# Return index.html for 403/404 so SPA client-side routing works
cloudfront.ErrorResponse(
http_status=403,
response_http_status=200,
response_page_path="/index.html",
),
cloudfront.ErrorResponse(
http_status=404,
response_http_status=200,
response_page_path="/index.html",
),
],
minimum_protocol_version=cloudfront.SecurityPolicyProtocol.TLS_V1_2_2021,
web_acl_id=waf_acl_arn,
enable_logging=True,
log_bucket=access_log_bucket,
log_file_prefix="cloudfront/",
)
# ── CloudWatch RUM + X-Ray ───────────────────────────────────────────
# RUM collects browser telemetry (page loads, JS errors, fetch latency)
# and — with enable_x_ray — emits a client-side trace segment that joins
# the backend Lambda/API Gateway segments into a single X-Ray trace.
# Guest (unauthenticated) browsers authenticate via Cognito Identity
# Pool → STS AssumeRoleWithWebIdentity → scoped rum:PutRumEvents role.
# The monitor ARN is constructed from the known monitor name so the
# IAM role can reference it without a circular dependency on the
# CfnAppMonitor resource.
rum_identity_pool = cognito.CfnIdentityPool(
self,
"RumIdentityPool",
allow_unauthenticated_identities=True,
identity_pool_name=f"{self.stack_name}-rum",
)
rum_monitor_name = f"{self.stack_name}-rum"
rum_monitor_arn = f"arn:{self.partition}:rum:{self.region}:{self.account}:appmonitor/{rum_monitor_name}"
rum_unauth_role = iam.Role(
self,
"RumUnauthenticatedRole",
assumed_by=iam.FederatedPrincipal(
"cognito-identity.amazonaws.com",
conditions={
"StringEquals": {"cognito-identity.amazonaws.com:aud": rum_identity_pool.ref},
"ForAnyValue:StringLike": {"cognito-identity.amazonaws.com:amr": "unauthenticated"},
},
assume_role_action="sts:AssumeRoleWithWebIdentity",
),
description=f"Guest role assumed by browser RUM clients for {rum_monitor_name}",
inline_policies={
"AllowPutRumEvents": iam.PolicyDocument(
statements=[
iam.PolicyStatement(
actions=["rum:PutRumEvents"],
resources=[rum_monitor_arn],
)
]
)
},
)
cognito.CfnIdentityPoolRoleAttachment(
self,
"RumIdentityPoolRoleAttachment",
identity_pool_id=rum_identity_pool.ref,
roles={"unauthenticated": rum_unauth_role.role_arn},
)
rum_app_monitor = rum.CfnAppMonitor(
self,
"RumAppMonitor",
name=rum_monitor_name,
domain=distribution.distribution_domain_name,
cw_log_enabled=True,
# Enable custom events so the frontend can call cwr('recordEvent', ...)
# for domain telemetry. Without this set to ENABLED, custom event
# uploads are silently dropped at the data plane.
custom_events=rum.CfnAppMonitor.CustomEventsProperty(status="ENABLED"),
app_monitor_configuration=rum.CfnAppMonitor.AppMonitorConfigurationProperty(
allow_cookies=True,
enable_x_ray=True,
session_sample_rate=1.0,
# CloudFormation's schema only accepts ["errors", "performance", "http"] here —
# "interaction" is rejected as an invalid enum value despite being a real RUM
# plugin. This server-side list is metadata used by the AWS-generated snippet,
# not the live plugin loader. The actual plugin set is controlled by the
# client-side `telemetries` array in frontend/index.html, which DOES include
# "interaction" alongside the http tuple form. Keep these two lists divergent
# on purpose; do not "sync" them.
telemetries=["errors", "performance", "http"],
identity_pool_id=rum_identity_pool.ref,
guest_role_arn=rum_unauth_role.role_arn,
),
)
# CMK-encrypted log group for the BucketDeployment provider Lambda.
# Passing log_group= here (instead of log_retention=) avoids the legacy
# LogRetention singleton path and keeps every log group encrypted with
# this stack's CMK.
bucket_deployment_log_group = logs.LogGroup(
self,
"BucketDeploymentLogGroup",
encryption_key=frontend_encryption_key,
retention=logs.RetentionDays.ONE_WEEK,
removal_policy=RemovalPolicy.DESTROY,
)
# Shared CMK-encrypted log group for all AwsCustomResource singletons in
# this stack (RumMetricsDestination, RumExtendedMetrics, InvalidateCloudFrontCache).
# CDK reuses one provider Lambda across every AwsCustomResource in a stack,
# so a single log group serves all three.
custom_resource_log_group = logs.LogGroup(
self,
"AwsCustomResourceLogGroup",
encryption_key=frontend_encryption_key,
retention=logs.RetentionDays.ONE_WEEK,
removal_policy=RemovalPolicy.DESTROY,
)
rum_extended_metrics = self._wire_rum_metrics_extras(
rum_app_monitor, rum_monitor_name, rum_monitor_arn, custom_resource_log_group
)
self._wire_rum_log_group_cleanup(rum_app_monitor, rum_monitor_name, custom_resource_log_group)
# ── Deploy frontend assets ───────────────────────────────────────────
# Uploads frontend/ to S3 and generates config.json with the API URL
# and RUM client config injected at deploy time. Cache invalidation is
# handled by a separate AwsCustomResource below — the BucketDeployment's
# built-in `distribution=` parameter is intentionally not used because
# its delete-time invalidation races with CloudFront's own deletion on
# `cdk destroy` (aws/aws-cdk#15891).
bucket_deployment = s3deploy.BucketDeployment(
self,
"DeployFrontend",
sources=[
s3deploy.Source.asset("frontend"),
s3deploy.Source.json_data(
"config.json",
{
"apiUrl": api_url,
"rum": {
"appMonitorId": rum_app_monitor.attr_id,
"identityPoolId": rum_identity_pool.ref,
"region": self.region,
# Session attributes are attached to every RUM event
# in the session. Sourcing them from deploy-time
# config (rather than hardcoding in the HTML) lets
# multiple deploys feed the same dashboard while
# remaining filterable.
"sessionAttributes": {
"applicationName": self.stack_name,
},
},
},
),
],
destination_bucket=bucket,
log_group=bucket_deployment_log_group,
)
# Defer the slow asset deploy until after the RUM custom resources
# have succeeded. If RumExtendedMetrics fails (it depends on IAM
# propagation), the BucketDeployment never starts — saving the most
# expensive single resource from being repeated on every retry until
# the cheaper IAM dance settles.
bucket_deployment.node.add_dependency(rum_extended_metrics)
# CloudFront cache invalidation, decoupled from BucketDeployment.
# Defines on_create and on_update only — no on_delete — so CFN simply
# removes this resource from stack state during teardown without any
# CloudFront API call to race with the distribution's own deletion.
# This is the permanent fix for aws/aws-cdk#15891, replacing the
# BucketDeployment's built-in invalidation hook.
#
# CallerReference is gated on the BucketDeployment's content-hashed S3
# object key. Same assets → same key → CFN sees no change → no
# invalidation fires (correct: nothing to invalidate). Different assets
# → different key → CFN fires on_update → invalidation runs. Prevents
# backend-only deploys from burning the 1000/month free invalidation
# quota. See README "Design decisions" for the longer write-up.
cf_invalidation_call = cr.AwsSdkCall(
service="CloudFront",
action="createInvalidation",
parameters={
"DistributionId": distribution.distribution_id,
"InvalidationBatch": {
"Paths": {"Quantity": 1, "Items": ["/*"]},
# object_keys is a CDK list-token, not a Python list — use Fn.select.
"CallerReference": Fn.select(0, bucket_deployment.object_keys),
},
},
physical_resource_id=cr.PhysicalResourceId.of(f"{self.stack_name}-cf-invalidation"),
)
invalidate_cf_cache = cr.AwsCustomResource(
self,
"InvalidateCloudFrontCache",
on_create=cf_invalidation_call,
on_update=cf_invalidation_call,
policy=cr.AwsCustomResourcePolicy.from_statements(
[
iam.PolicyStatement(
actions=["cloudfront:CreateInvalidation"],
resources=[
f"arn:{Stack.of(self).partition}:cloudfront::{Stack.of(self).account}:distribution/{distribution.distribution_id}"
],
),
]
),
log_group=custom_resource_log_group,
)
invalidate_cf_cache.node.add_dependency(bucket_deployment)
# CDK generates an inline default policy on the AwsCustomResource's
# auto-created role. Same constraint as the RUM custom resources;
# apply the same IAMNoInlinePolicy suppressions.
cf_invalidation_inline_reason = (
"AwsCustomResource policy is a single least-privilege inline statement scoped to "
"cloudfront:CreateInvalidation on this stack's distribution ARN — managed-policy "
"reuse adds nothing"
)
NagSuppressions.add_resource_suppressions(
invalidate_cf_cache,
[
{"id": "NIST.800.53.R5-IAMNoInlinePolicy", "reason": cf_invalidation_inline_reason},
{"id": "HIPAA.Security-IAMNoInlinePolicy", "reason": cf_invalidation_inline_reason},
{"id": "PCI.DSS.321-IAMNoInlinePolicy", "reason": cf_invalidation_inline_reason},
],
apply_to_children=True,
)
CfnOutput(
self,
"CloudFrontDomainName",
description="CloudFront distribution domain name — use this as your frontend URL",
value=f"https://{distribution.distribution_domain_name}",
)
CfnOutput(
self,
"CloudFrontDistributionId",
description="CloudFront distribution ID — needed for manual cache invalidations",
value=distribution.distribution_id,
)
CfnOutput(
self,
"FrontendBucketName",
description="S3 bucket storing the frontend static assets",
value=bucket.bucket_name,
)
CfnOutput(
self,
"RumAppMonitorId",
description="CloudWatch RUM app monitor ID — used by the browser RUM client",
value=rum_app_monitor.attr_id,
)
CfnOutput(
self,
"RumIdentityPoolId",
description="Cognito Identity Pool ID — used by the browser RUM client for guest credentials",
value=rum_identity_pool.ref,
)
# ── RUM / Cognito cdk-nag suppressions ───────────────────────────────
# Unauthenticated identities are intentional — browsers have no prior
# identity and RUM's guest-credentials model is the standard pattern.
# The role's only permission is rum:PutRumEvents on this monitor.
NagSuppressions.add_resource_suppressions(
rum_identity_pool,
[
{
"id": "AwsSolutions-COG7",
"reason": "RUM requires unauthenticated guest credentials for anonymous browser telemetry",
},
],
)
# The guest role has a single least-privilege permission — rum:PutRumEvents
# on exactly one monitor ARN — tightly bound to this role's one purpose.
# A managed policy would add indirection without changing the security
# posture, since the policy is used by nothing else and is scoped to a
# resource that is itself one-to-one with the role.
inline_policy_reason = (
"Single least-privilege inline policy (rum:PutRumEvents on one monitor ARN) "
"is tightly bound to this role's sole purpose — anonymous browser telemetry upload"
)
NagSuppressions.add_resource_suppressions(
rum_unauth_role,
[
{"id": "NIST.800.53.R5-IAMNoInlinePolicy", "reason": inline_policy_reason},
{"id": "HIPAA.Security-IAMNoInlinePolicy", "reason": inline_policy_reason},
{"id": "PCI.DSS.321-IAMNoInlinePolicy", "reason": inline_policy_reason},
],
)
# ── Explicit log group for the CDK auto-delete Lambda ────────────────
# CDK creates a singleton Lambda to empty the bucket before deletion.
# It is a CloudFormation-managed Lambda, but its log group is created
# implicitly by Lambda and has no retention — it would dangle after
# cdk destroy. We find the provider via the construct tree and create
# an explicit log group so CloudFormation owns and deletes it.
# The lookup is type-checked at runtime instead of cast-asserted: if
# CDK ever swaps the provider out for a non-CustomResourceProvider type
# the explicit isinstance() returns None and the log-group block is
# skipped, rather than letting a stale cast() lie its way into a
# service_token attribute access that would crash at synth time.
auto_delete_provider_node = self.node.try_find_child("Custom::S3AutoDeleteObjectsCustomResourceProvider")
auto_delete_provider = (
auto_delete_provider_node if isinstance(auto_delete_provider_node, CustomResourceProvider) else None
)
if auto_delete_provider is not None:
# service_token is the Lambda ARN; index 6 of the colon-split is the function name
fn_name = Fn.select(6, Fn.split(":", auto_delete_provider.service_token))
logs.LogGroup(
self,
"AutoDeleteObjectsLogGroup",
log_group_name=Fn.join("", ["/aws/lambda/", fn_name]),
encryption_key=frontend_encryption_key,
retention=logs.RetentionDays.ONE_WEEK,
removal_policy=RemovalPolicy.DESTROY,
)
self._create_athena_glue_resources(access_log_bucket, frontend_encryption_key)
# ── Per-resource cdk-nag suppressions ──────────────────────────────────
# All Lambdas in this stack are CDK-managed singletons. Their construct
# IDs are stable (hashed from CDK's own source) but they are created as
# stack-level siblings of the construct that requested them, so we look
# them up with ``try_find_child`` rather than absolute path strings —
# this keeps the suppression working regardless of whether the stack is
# at the App root or nested inside a cdk.Stage.
#
# Stable singleton IDs:
# Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C — BucketDeployment provider
# Custom::S3AutoDeleteObjectsCustomResourceProvider — auto-delete provider
# AWS679f53fac002430cb0da5b7982bd2287 — AwsCustomResource provider Lambda
# (used by RumMetricsDestination, RumExtendedMetrics, InvalidateCloudFrontCache)
suppress_cdk_singletons(
self,
(
"Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C",
"AWS679f53fac002430cb0da5b7982bd2287",
),
)
# ── Async failure destination for the AwsCustomResource provider ────────
# See HelloWorldStack for the full rationale — CFN invokes the provider
# async, and without on_failure a crashed provider's payload is lost.
self.cr_provider_dlq = attach_async_failure_destination(
self,
"AWS679f53fac002430cb0da5b7982bd2287",
encryption_key=frontend_encryption_key,
queue_id="AwsCustomResourceProviderDlq",
)
# minimizePolicies restructures the BucketDeployment handler's inline
# policy into a separate resource under DeployFrontend/CustomResourceHandler.
deploy_frontend = self.node.try_find_child("DeployFrontend")
if deploy_frontend is not None:
suppress_cdk_singletons(deploy_frontend, ("CustomResourceHandler",))
if auto_delete_provider is not None:
NagSuppressions.add_resource_suppressions(
auto_delete_provider,
CDK_LAMBDA_SUPPRESSIONS,
apply_to_children=True,
)
# ── Stack-level cdk-nag suppressions (genuinely stack-wide) ─────────────
replication_reason = "S3 replication not needed for sample app — static assets are redeployable"
versioning_reason = "S3 versioning not needed for sample app — static assets are redeployable via cdk deploy"
stack_suppressions = [
("AwsSolutions-CFR1", "Geo restriction not required for sample app"),
("AwsSolutions-CFR4", "Using default CloudFront certificate — no custom domain for sample app"),
("NIST.800.53.R5-S3BucketReplicationEnabled", replication_reason),
("NIST.800.53.R5-S3BucketVersioningEnabled", versioning_reason),
("HIPAA.Security-S3BucketReplicationEnabled", replication_reason),
("HIPAA.Security-S3BucketVersioningEnabled", versioning_reason),
("PCI.DSS.321-S3BucketReplicationEnabled", replication_reason),
("PCI.DSS.321-S3BucketVersioningEnabled", versioning_reason),
]
NagSuppressions.add_stack_suppressions(
self,
[{"id": rule, "reason": reason} for rule, reason in stack_suppressions],
)
def _create_s3_audit_trail(self, audited_buckets: list[s3.Bucket], encryption_key: kms.Key) -> None:
"""Create a CloudTrail Trail recording S3 object-level data events on the given buckets.
Captures every Get/Put/Delete API call against the audited buckets. Object-level
events aren't recorded by the default management-events trail and aren't
reconstructible from S3 server access logs (those only cover successful reads/writes
through the bucket interface, not failed authorization or DeleteObject calls).
Trail logs are stored in a dedicated bucket so the audit destination isn't itself
among the audited resources.
"""
# CloudTrail needs explicit KMS grants on the encryption key to write
# encrypted log files. CDK's auto-grants from passing encryption_key=
# don't always extend to the cloudtrail service principal when the key
# is shared with other services (CloudWatch Logs, CloudFront, etc.),
# so add the principal explicitly here. Mirrors the existing logs/
# CloudFront grants on the same key.
# Confused-deputy guard: scope the CloudTrail principal grant to trails
# in this account. The trail name is generated by CDK so we use a wildcard
# ARN against the account; CloudTrail sets aws:SourceArn to the trail ARN
# on every encrypt call. aws:SourceAccount is checked too as defense in
# depth (some older trail integrations omit aws:SourceArn).
encryption_key.add_to_resource_policy(
iam.PolicyStatement(
actions=["kms:GenerateDataKey*", "kms:DescribeKey"],
principals=[iam.ServicePrincipal("cloudtrail.amazonaws.com")],
resources=["*"],
conditions={
"StringEquals": {"aws:SourceAccount": self.account},
"ArnLike": {
"aws:SourceArn": f"arn:{self.partition}:cloudtrail:{self.region}:{self.account}:trail/*",
},
},
)
)
cloudtrail_log_bucket = s3.Bucket(
self,
"CloudTrailLogsBucket",
block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
encryption=s3.BucketEncryption.S3_MANAGED,
enforce_ssl=True,
versioned=False,
# Bound the audit trail's storage growth. S3 data events fire on
# every Get/Put/Delete against the audited buckets and accumulate
# forever otherwise. 30 days matches a typical short-retention
# forensic window — production forks with compliance scope (HIPAA,
# PCI) should extend or replace this with an AWS Backup plan, and
# forks running CloudTrail Lake can drop the on-bucket trail
# entirely.
lifecycle_rules=[
s3.LifecycleRule(
id="ExpireAfter30Days",
enabled=True,
expiration=Duration.days(30),
abort_incomplete_multipart_upload_after=Duration.days(1),
),
],
removal_policy=RemovalPolicy.DESTROY,
auto_delete_objects=True,
)
# CloudTrail can't write to a bucket that has access logging or KMS-CMK
# encryption enabled (delivery service limitations) — same constraints
# that apply to access_log_bucket. Suppress the corresponding nag rules.
bucket_suppressions = [
("AwsSolutions-S1", "CloudTrail log bucket — server access logging would create circular audit trails"),
(
"NIST.800.53.R5-S3BucketLoggingEnabled",
"CloudTrail log bucket — server access logging would create circular audit trails",
),
(
"HIPAA.Security-S3BucketLoggingEnabled",
"CloudTrail log bucket — server access logging would create circular audit trails",
),
(
"PCI.DSS.321-S3BucketLoggingEnabled",
"CloudTrail log bucket — server access logging would create circular audit trails",
),
(
"NIST.800.53.R5-S3DefaultEncryptionKMS",
"CloudTrail delivery service does not support KMS-CMK encrypted destination buckets",
),
(
"HIPAA.Security-S3DefaultEncryptionKMS",
"CloudTrail delivery service does not support KMS-CMK encrypted destination buckets",
),
(
"PCI.DSS.321-S3DefaultEncryptionKMS",
"CloudTrail delivery service does not support KMS-CMK encrypted destination buckets",
),
(
"NIST.800.53.R5-S3BucketVersioningEnabled",
"Versioning not needed for CloudTrail log bucket — logs are append-only and integrity-validated by CloudTrail",
),
(
"HIPAA.Security-S3BucketVersioningEnabled",
"Versioning not needed for CloudTrail log bucket — logs are append-only and integrity-validated by CloudTrail",
),
(
"PCI.DSS.321-S3BucketVersioningEnabled",
"Versioning not needed for CloudTrail log bucket — logs are append-only and integrity-validated by CloudTrail",
),
(
"NIST.800.53.R5-S3BucketReplicationEnabled",
"Replication not needed for CloudTrail log bucket in sample app",
),
(
"HIPAA.Security-S3BucketReplicationEnabled",
"Replication not needed for CloudTrail log bucket in sample app",
),
(
"PCI.DSS.321-S3BucketReplicationEnabled",
"Replication not needed for CloudTrail log bucket in sample app",
),
]
NagSuppressions.add_resource_suppressions(
cloudtrail_log_bucket,
[{"id": rule, "reason": reason} for rule, reason in bucket_suppressions],
)
cloudtrail_log_group = logs.LogGroup(
self,
"S3DataEventsTrailLogs",
encryption_key=encryption_key,
retention=logs.RetentionDays.ONE_WEEK,
removal_policy=RemovalPolicy.DESTROY,
)
# Pin the trail name so its ARN is known *before* the trail resource is
# created — needed to break the dependency cycle that would otherwise
# form between the trail (which CDK auto-wires to depend on its bucket
# policy) and the confused-deputy Deny statement on the bucket policy
# (which references the trail ARN).
trail_name = f"{self.stack_name}-S3DataEventsTrail"
trail_arn = f"arn:{self.partition}:cloudtrail:{self.region}:{self.account}:trail/{trail_name}"
# Confused-deputy guard on the CloudTrail bucket policy. CDK 2.248's
# cloudtrail.Trail L2 grants the cloudtrail.amazonaws.com principal
# s3:GetBucketAcl + s3:PutObject without an aws:SourceArn condition,
# so any CloudTrail trail in any AWS account that ever discovered this
# bucket name could in principle write to it. Adding two explicit Deny
# statements (one per condition key) closes the gap on either mismatch
# — if both keys lived in one StringNotEquals block IAM would AND them,
# so a malicious trail in the same account with a different name would
# match aws:SourceAccount and slip past. Splitting into two Denies gives
# the OR semantics we actually want.
ct_principals = [iam.ServicePrincipal("cloudtrail.amazonaws.com")]
ct_resources = [cloudtrail_log_bucket.bucket_arn, cloudtrail_log_bucket.arn_for_objects("*")]
cloudtrail_log_bucket.add_to_resource_policy(
iam.PolicyStatement(
effect=iam.Effect.DENY,
actions=["s3:GetBucketAcl", "s3:PutObject"],
principals=ct_principals,
resources=ct_resources,
conditions={"StringNotEquals": {"aws:SourceArn": trail_arn}},
)
)
cloudtrail_log_bucket.add_to_resource_policy(
iam.PolicyStatement(
effect=iam.Effect.DENY,
actions=["s3:GetBucketAcl", "s3:PutObject"],
principals=ct_principals,
resources=ct_resources,
conditions={"StringNotEquals": {"aws:SourceAccount": self.account}},
)
)
s3_data_events_trail = cloudtrail.Trail(
self,
"S3DataEventsTrail",
trail_name=trail_name,
bucket=cloudtrail_log_bucket,
send_to_cloud_watch_logs=True,
cloud_watch_log_group=cloudtrail_log_group,
encryption_key=encryption_key,
enable_file_validation=True,
include_global_service_events=False,
is_multi_region_trail=False,
)
s3_data_events_trail.add_s3_event_selector([cloudtrail.S3EventSelector(bucket=b) for b in audited_buckets])
# CDK creates the trail's CloudWatch Logs delivery role with an inline
# default policy — same pattern as the Lambda service role; not
# directly configurable.
inline_policy_reason = "CDK generates the trail's LogsRole default policy inline — not directly configurable"
NagSuppressions.add_resource_suppressions(
s3_data_events_trail,
[
{"id": "NIST.800.53.R5-IAMNoInlinePolicy", "reason": inline_policy_reason},
{"id": "HIPAA.Security-IAMNoInlinePolicy", "reason": inline_policy_reason},
{"id": "PCI.DSS.321-IAMNoInlinePolicy", "reason": inline_policy_reason},
],
apply_to_children=True,
)
def _wire_rum_metrics_extras(
self,
rum_app_monitor: rum.CfnAppMonitor,
rum_monitor_name: str,
rum_monitor_arn: str,
custom_resource_log_group: logs.LogGroup,
) -> cr.AwsCustomResource:
"""Wire CloudWatch metrics destination and dimensioned metric definitions to the AppMonitor.
Returns the metric-definitions custom resource so callers can wire a dependency on it.
Implementation notes (these are non-obvious — see README "CloudWatch RUM" section):
- Each definition needs an explicit ``EventPattern``; the API rejects vended-metric
submissions with just ``Name`` + ``DimensionKeys`` (returns 200 OK with an Errors[]
body that AwsCustomResource treats as success).
- All three ``rum:*`` actions are bundled on the destination CR's policy so the
BatchCreate call benefits from a full putRumMetricsDestination round-trip of IAM
propagation lead time. Splitting them per-CR loses the IAM race ~100% of the time.
- ``on_update`` mirrors ``on_create``; without it AwsCustomResource no-ops on
CloudFormation UPDATE events and changes to the metric list never reach AWS.
- Http5xx omits the explicit numeric range filter that Http4xx requires (RUM applies
the 5xx filter internally for that vended metric).
"""
rum_metrics_destination = cr.AwsCustomResource(
self,
"RumMetricsDestination",
on_create=cr.AwsSdkCall(
service="rum",
action="putRumMetricsDestination",
parameters={"AppMonitorName": rum_monitor_name, "Destination": "CloudWatch"},
physical_resource_id=cr.PhysicalResourceId.of(f"{rum_monitor_name}/CloudWatch"),
),
on_delete=cr.AwsSdkCall(
service="rum",
action="deleteRumMetricsDestination",
parameters={"AppMonitorName": rum_monitor_name, "Destination": "CloudWatch"},
),
policy=cr.AwsCustomResourcePolicy.from_statements(
[
iam.PolicyStatement(
actions=[
"rum:PutRumMetricsDestination",
"rum:DeleteRumMetricsDestination",
"rum:BatchCreateRumMetricDefinitions",
],
resources=[rum_monitor_arn],
),
]
),
log_group=custom_resource_log_group,
)
rum_metrics_destination.node.add_dependency(rum_app_monitor)
js_pat = '{{"event_type":["com.amazon.rum.js_error_event"],"metadata":{{"{k}":[{{"exists":true}}]}}}}'
http_pat = '{{"event_type":["com.amazon.rum.http_event"],"metadata":{{"browserName":[{{"exists":true}}]}}{s}}}'
http4xx_status = ',"event_details":{"response":{"status":[{"numeric":[">=",400,"<",500]}]}}'
page_pat = '{"event_type":["com.amazon.rum.page_view_event"],"metadata":{"pageId":[{"exists":true}]}}'
defs: list[dict[str, Any]] = [
{
"Name": "JsErrorCount",
"EventPattern": js_pat.format(k="browserName"),
"DimensionKeys": {"metadata.browserName": "BrowserName"},
},
{
"Name": "JsErrorCount",
"EventPattern": js_pat.format(k="deviceType"),
"DimensionKeys": {"metadata.deviceType": "DeviceType"},
},
{
"Name": "JsErrorCount",
"EventPattern": js_pat.format(k="countryCode"),
"DimensionKeys": {"metadata.countryCode": "CountryCode"},
},
{
"Name": "Http4xxCount",
"EventPattern": http_pat.format(s=http4xx_status),
"DimensionKeys": {"metadata.browserName": "BrowserName"},
},
{
"Name": "Http5xxCount",
"EventPattern": http_pat.format(s=""),
"DimensionKeys": {"metadata.browserName": "BrowserName"},
},
{"Name": "PageViewCount", "EventPattern": page_pat, "DimensionKeys": {"metadata.pageId": "PageId"}},
]
batch_create = cr.AwsSdkCall(
service="rum",
action="batchCreateRumMetricDefinitions",
parameters={
"AppMonitorName": rum_monitor_name,
"Destination": "CloudWatch",
"MetricDefinitions": defs,
},
physical_resource_id=cr.PhysicalResourceId.of(f"{rum_monitor_name}/extended-metrics"),
)
rum_extended_metrics = cr.AwsCustomResource(
self,
"RumExtendedMetrics",
on_create=batch_create,
on_update=batch_create,
policy=cr.AwsCustomResourcePolicy.from_sdk_calls(resources=[rum_monitor_arn]),
log_group=custom_resource_log_group,
)
rum_extended_metrics.node.add_dependency(rum_metrics_destination)
# Same single-purpose, monitor-scoped justification as the RumUnauthenticatedRole
# inline policy. Cdk-nag flags both per-construct CustomResourcePolicy resources.
reason = (
"Single least-privilege inline policy attached to the CDK AwsCustomResource handler — "
"scoped to specific rum:* actions on one monitor ARN; managed-policy reuse adds nothing"
)
for construct in (rum_metrics_destination, rum_extended_metrics):
NagSuppressions.add_resource_suppressions(
construct,
[
{"id": "NIST.800.53.R5-IAMNoInlinePolicy", "reason": reason},
{"id": "HIPAA.Security-IAMNoInlinePolicy", "reason": reason},
{"id": "PCI.DSS.321-IAMNoInlinePolicy", "reason": reason},
],
apply_to_children=True,
)
return rum_extended_metrics
def _wire_rum_log_group_cleanup(
self,
rum_app_monitor: rum.CfnAppMonitor,
rum_monitor_name: str,
custom_resource_log_group: logs.LogGroup,
) -> None:
"""Delete the RUM-auto-created CloudWatch Logs group at stack destroy.
CloudWatch RUM creates a log group at
``/aws/vendedlogs/RUMService_{monitor-name}{first-8-hex-of-monitor-id}``
the first time it ingests an event. That log group is owned by this
account but created outside CloudFormation, so ``cdk destroy`` deletes
the AppMonitor without touching the log group — same dangling-resource
shape as the Application Insights dashboard that
``AppInsightsDashboardCleanup`` in the backend stack solves.
``ResourceNotFoundException`` is ignored so destroy succeeds even when
no events were ever ingested (the log group only materializes on the
first event — common in CI / dev / no-traffic deploys).
"""
monitor_id_prefix = Fn.select(0, Fn.split("-", rum_app_monitor.attr_id))
log_group_name = Fn.join("", [f"/aws/vendedlogs/RUMService_{rum_monitor_name}", monitor_id_prefix])
log_group_arn = Fn.join(
"",
[
f"arn:{self.partition}:logs:{self.region}:{self.account}:log-group:/aws/vendedlogs/RUMService_{rum_monitor_name}",
monitor_id_prefix,
":*",
],
)
cleanup = cr.AwsCustomResource(
self,
"RumLogGroupCleanup",
on_delete=cr.AwsSdkCall(
service="CloudWatchLogs",
action="deleteLogGroup",
parameters={"logGroupName": log_group_name},
physical_resource_id=cr.PhysicalResourceId.of("RumLogGroupCleanup"),
ignore_error_codes_matching="ResourceNotFoundException",
),
policy=cr.AwsCustomResourcePolicy.from_sdk_calls(resources=[log_group_arn]),
install_latest_aws_sdk=False,
log_group=custom_resource_log_group,
)
# The implicit attr_id reference already forces this dependency at the
# CFN level; add_dependency makes the intent visible in code.
cleanup.node.add_dependency(rum_app_monitor)
# Matches the IAMNoInlinePolicy suppression pattern on the other RUM CRs
# in this stack — CDK generates the handler's policy inline.
reason = (
"Single least-privilege inline policy attached to the CDK AwsCustomResource handler — "
"scoped to logs:DeleteLogGroup on one log-group ARN; managed-policy reuse adds nothing"
)
# AwsSolutions-IAM5 fires because the log-group ARN ends in `:*`, which
# is the standard CloudWatch Logs log-stream wildcard required by every
# log-group resource ARN per the IAM docs — there is no way to grant
# logs:DeleteLogGroup on a log group without the `:*` suffix. The
# resource is otherwise fully scoped to one specific log group (path
# built from this monitor's runtime-resolved ID prefix), so the
# wildcard portion only authorizes log-stream-scope wildcards within
# that one log group, not across log groups.
iam5_reason = (
"Log-group ARN includes the standard :* log-stream wildcard suffix — required for any "
"CloudWatch Logs resource ARN per the IAM service authorization docs. The resource is "
"otherwise scoped to one specific log group built from the monitor's runtime-resolved ID."
)
NagSuppressions.add_resource_suppressions(
cleanup,
[
{"id": "NIST.800.53.R5-IAMNoInlinePolicy", "reason": reason},
{"id": "HIPAA.Security-IAMNoInlinePolicy", "reason": reason},
{"id": "PCI.DSS.321-IAMNoInlinePolicy", "reason": reason},
{"id": "AwsSolutions-IAM5", "reason": iam5_reason, "applies_to": ["Resource::*"]},
],
apply_to_children=True,
)
def _create_athena_glue_resources(self, access_log_bucket: s3.Bucket, encryption_key: kms.Key) -> None:
"""Create Glue catalog tables and Athena workgroup for CloudFront/S3 access log analytics."""
# ── Glue Database ────────────────────────────────────────────────
# Glue database names: lowercase, alphanumeric + underscores only.
db_name = self.node.id.lower().replace("-", "_") + "_access_logs"
glue_db = glue.CfnDatabase(
self,
"AccessLogsDatabase",
catalog_id=self.account,
database_input=glue.CfnDatabase.DatabaseInputProperty(
name=db_name,
description="Glue catalog for CloudFront and S3 access logs",
),
)
# ── CloudFront Standard Logs Table ───────────────────────────────
# 33-field tab-separated format; 2 header lines (#Version, #Fields).