-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patheks-cluster-stack.ts
More file actions
1175 lines (1077 loc) · 55.5 KB
/
Copy patheks-cluster-stack.ts
File metadata and controls
1175 lines (1077 loc) · 55.5 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
import * as cdk from 'aws-cdk-lib';
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
import * as cognito from 'aws-cdk-lib/aws-cognito';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as eks from 'aws-cdk-lib/aws-eks';
import * as events from 'aws-cdk-lib/aws-events';
import * as events_targets from 'aws-cdk-lib/aws-events-targets';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
import * as logs from 'aws-cdk-lib/aws-logs';
import * as cw_actions from 'aws-cdk-lib/aws-cloudwatch-actions';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as wafv2 from 'aws-cdk-lib/aws-wafv2';
import * as route53 from 'aws-cdk-lib/aws-route53';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as cr from 'aws-cdk-lib/custom-resources';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
import * as efs from 'aws-cdk-lib/aws-efs';
import * as kms from 'aws-cdk-lib/aws-kms';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import { KubectlV35Layer } from '@aws-cdk/lambda-layer-kubectl-v35';
import { NagSuppressions } from 'cdk-nag';
import { Construct } from 'constructs';
export class EksClusterStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// ── Validate required CDK context (before any resource creation) ─────
// Skip validation during CI synth (uses placeholder values from cdk.json.example)
if (!this.node.tryGetContext('@ci-synth')) {
const requiredContext: Record<string, string> = {
allowedEmailDomains: 'Email domain allowlist (e.g., your-company.com)',
githubOwner: 'GitHub org for Helm chart repo',
};
const missingCtx = Object.entries(requiredContext)
.filter(([key]) => {
const val = this.node.tryGetContext(key);
if (typeof val !== 'string' || !val) return true;
return val.startsWith('your-') || val === 'us-west-2_XXXXXXXXX' || val === 'xxxxxxxxxxxxxxxxxx';
})
.map(([key, desc]) => ` - ${key}: ${desc}`);
if (missingCtx.length > 0) {
throw new Error(
`Missing required CDK context values in cdk.json:\n${missingCtx.join('\n')}\n\n` +
'Copy cdk/cdk.json.example to cdk/cdk.json and fill in your values.\n' +
'See README.md "Prerequisites" for details.',
);
}
}
// ── VPC ─────────────────────────────────────────────────────────────────
const vpc = new ec2.Vpc(this, 'Vpc', {
maxAzs: 2,
natGateways: 2,
subnetConfiguration: [
{ name: 'public', subnetType: ec2.SubnetType.PUBLIC, cidrMask: 24 },
{ name: 'private', subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS, cidrMask: 24 },
],
});
// Tag private subnets for Karpenter EC2NodeClass subnet discovery.
// Karpenter requires both 'internal-elb' and cluster-owned tags to match.
// CDK adds 'internal-elb' automatically but not the cluster-owned tag.
// Derive unique cluster name from stack name to prevent tag collisions
// across deployments. Context override is available for advanced users.
const stackSuffix = this.stackName.replace(/^OpenClawEksStack-/, '');
const clusterName = (this.node.tryGetContext('clusterName') as string) || `openclaw-${stackSuffix}`;
for (const subnet of vpc.privateSubnets) {
cdk.Tags.of(subnet).add(`kubernetes.io/cluster/${clusterName}`, 'owned');
}
// VPC Endpoints — reduce NAT Gateway costs for AWS service traffic
// S3 Gateway endpoint is free; Interface endpoints ~$7/mo/AZ
vpc.addGatewayEndpoint('S3Endpoint', { service: ec2.GatewayVpcEndpointAwsService.S3 });
vpc.addInterfaceEndpoint('StsEndpoint', { service: ec2.InterfaceVpcEndpointAwsService.STS });
vpc.addInterfaceEndpoint('EcrApiEndpoint', { service: ec2.InterfaceVpcEndpointAwsService.ECR });
vpc.addInterfaceEndpoint('EcrDkrEndpoint', { service: ec2.InterfaceVpcEndpointAwsService.ECR_DOCKER });
// ── EKS Cluster ─────────────────────────────────────────────────────────
// VPC Flow Logs — network forensics for all traffic
new ec2.FlowLog(this, 'VpcFlowLog', {
resourceType: ec2.FlowLogResourceType.fromVpc(vpc),
destination: ec2.FlowLogDestination.toCloudWatchLogs(),
trafficType: ec2.FlowLogTrafficType.ALL,
});
// KMS key for EKS envelope encryption of Kubernetes secrets
const eksSecretsKey = new kms.Key(this, 'EksSecretsKey', {
alias: `openclaw/${this.stackName}/eks-secrets`,
description: 'Envelope encryption for Kubernetes secrets in EKS cluster',
enableKeyRotation: true,
});
const cluster = new eks.Cluster(this, 'Cluster', {
vpc,
version: eks.KubernetesVersion.V1_35,
defaultCapacity: 0,
clusterName,
authenticationMode: eks.AuthenticationMode.API_AND_CONFIG_MAP,
kubectlLayer: new KubectlV35Layer(this, 'KubectlLayer'),
secretsEncryptionKey: eksSecretsKey,
clusterLogging: [
eks.ClusterLoggingTypes.API,
eks.ClusterLoggingTypes.AUDIT,
eks.ClusterLoggingTypes.AUTHENTICATOR,
eks.ClusterLoggingTypes.CONTROLLER_MANAGER,
eks.ClusterLoggingTypes.SCHEDULER,
],
});
// ── Cluster Access: allow deployer to use kubectl ─────────
// Users must set 'deployerPrincipalArn' to their IAM principal ARN.
// This can be any IAM identity: IAM user, IAM role, or SSO/Identity Center role.
// It does NOT require IAM Identity Center — any IAM principal works.
// Example: cdk deploy -c deployerPrincipalArn=arn:aws:iam::123456789012:role/MyRole
const deployerArn = this.node.tryGetContext('deployerPrincipalArn') || this.node.tryGetContext('ssoRoleArn');
if (deployerArn) {
new eks.CfnAccessEntry(this, 'DeployerAccess', {
clusterName: cluster.clusterName,
principalArn: deployerArn,
type: 'STANDARD',
accessPolicies: [{
accessScope: { type: 'cluster' },
policyArn: 'arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy',
}],
});
}
// ── Managed Node Group ──────────────────────────────────────────────────
// 2x t4g.large (4 vCPU, 8 GiB) for system pods (~7.4 vCPU total requests).
cluster.addNodegroupCapacity('SystemNodes', {
instanceTypes: [new ec2.InstanceType('t4g.large')],
amiType: eks.NodegroupAmiType.AL2023_ARM_64_STANDARD,
minSize: 2,
maxSize: 4,
desiredSize: 3,
nodegroupName: 'system-graviton',
labels: { role: 'system' },
});
// ── EBS CSI Driver (with Pod Identity IAM) ──────────────────────────────
const ebsCsiRole = new iam.Role(this, 'EbsCsiRole', {
assumedBy: new iam.ServicePrincipal('pods.eks.amazonaws.com'),
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AmazonEBSCSIDriverPolicy'),
],
});
ebsCsiRole.assumeRolePolicy!.addStatements(new iam.PolicyStatement({
actions: ['sts:TagSession'],
principals: [new iam.ServicePrincipal('pods.eks.amazonaws.com')],
}));
new eks.CfnAddon(this, 'EbsCsiDriver', {
clusterName: cluster.clusterName,
addonName: 'aws-ebs-csi-driver',
podIdentityAssociations: [{
roleArn: ebsCsiRole.roleArn,
serviceAccount: 'ebs-csi-controller-sa',
}],
});
// ── Other EKS Add-ons (no special IAM needed) ───────────────────────────
for (const addonName of ['eks-pod-identity-agent', 'vpc-cni', 'coredns', 'kube-proxy']) {
new eks.CfnAddon(this, addonName.replace(/-/g, ''), {
clusterName: cluster.clusterName,
addonName,
});
}
// ── CloudWatch Container Insights ─────────────────────────────────────
const cwObsRole = new iam.Role(this, 'CwObservabilityRole', {
assumedBy: new iam.ServicePrincipal('pods.eks.amazonaws.com'),
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName('CloudWatchAgentServerPolicy'),
iam.ManagedPolicy.fromAwsManagedPolicyName('AWSXrayWriteOnlyAccess'),
],
});
cwObsRole.assumeRolePolicy!.addStatements(new iam.PolicyStatement({
actions: ['sts:TagSession'],
principals: [new iam.ServicePrincipal('pods.eks.amazonaws.com')],
}));
new eks.CfnAddon(this, 'CwObservability', {
clusterName: cluster.clusterName,
addonName: 'amazon-cloudwatch-observability',
podIdentityAssociations: [{
roleArn: cwObsRole.roleArn,
serviceAccount: 'cloudwatch-agent',
}],
});
// gp3 StorageClass — kept for backward compatibility during EFS migration
new eks.KubernetesManifest(this, 'Gp3StorageClass', {
removalPolicy: cdk.RemovalPolicy.RETAIN,
cluster,
manifest: [{
apiVersion: 'storage.k8s.io/v1',
kind: 'StorageClass',
metadata: { name: 'gp3' },
provisioner: 'ebs.csi.aws.com',
parameters: { type: 'gp3', fsType: 'ext4' },
reclaimPolicy: 'Delete',
volumeBindingMode: 'WaitForFirstConsumer',
allowVolumeExpansion: true,
}],
overwrite: true,
});
// ── EFS FileSystem (multi-AZ, per-tenant access points via CSI dynamic provisioning) ──
const fileSystem = new efs.FileSystem(this, 'TenantEfs', {
vpc,
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
encrypted: true,
performanceMode: efs.PerformanceMode.GENERAL_PURPOSE,
throughputMode: efs.ThroughputMode.ELASTIC,
lifecyclePolicy: efs.LifecyclePolicy.AFTER_30_DAYS,
removalPolicy: cdk.RemovalPolicy.RETAIN,
});
fileSystem.connections.allowFrom(cluster, ec2.Port.tcp(2049), 'EKS nodes to EFS NFS');
// ── EFS CSI Driver (with Pod Identity IAM) ──────────────────────────────
const efsCsiRole = new iam.Role(this, 'EfsCsiRole', {
assumedBy: new iam.ServicePrincipal('pods.eks.amazonaws.com'),
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AmazonEFSCSIDriverPolicy'),
],
});
efsCsiRole.assumeRolePolicy!.addStatements(new iam.PolicyStatement({
actions: ['sts:TagSession'],
principals: [new iam.ServicePrincipal('pods.eks.amazonaws.com')],
}));
const efsCsiAddon = new eks.CfnAddon(this, 'EfsCsiDriver', {
clusterName: cluster.clusterName,
addonName: 'aws-efs-csi-driver',
podIdentityAssociations: [{
roleArn: efsCsiRole.roleArn,
serviceAccount: 'efs-csi-controller-sa',
}],
});
efsCsiAddon.node.addDependency(fileSystem);
// EFS resource policy — allow mount from any principal via mount target.
// The EFS CSI node DaemonSet uses the node instance profile (not the controller's
// Pod Identity role) to mount volumes, so we must allow all principals here.
// Security is enforced by: (1) mount target lives in private subnets only,
// (2) security group restricts NFS port to EKS nodes, (3) access points
// enforce per-tenant POSIX UID/GID isolation.
fileSystem.addToResourcePolicy(new iam.PolicyStatement({
actions: ['elasticfilesystem:ClientMount', 'elasticfilesystem:ClientWrite', 'elasticfilesystem:ClientRootAccess'],
principals: [new iam.AnyPrincipal()],
conditions: {
Bool: { 'elasticfilesystem:AccessedViaMountTarget': 'true' },
},
}));
// efs-sc StorageClass — dynamic provisioning creates per-tenant access points
const efsStorageClass = new eks.KubernetesManifest(this, 'EfsStorageClass', {
removalPolicy: cdk.RemovalPolicy.RETAIN,
cluster,
manifest: [{
apiVersion: 'storage.k8s.io/v1',
kind: 'StorageClass',
metadata: { name: 'efs-sc' },
provisioner: 'efs.csi.aws.com',
parameters: {
provisioningMode: 'efs-ap',
fileSystemId: fileSystem.fileSystemId,
directoryPerms: '0755',
basePath: '/tenants',
subPathPattern: '${.PVC.namespace}',
ensureUniqueDirectory: 'false',
// GID range for EFS access points. Each tenant gets a unique GID.
// Range must be large enough for max expected tenants.
gidRangeStart: '1000',
gidRangeEnd: '2000',
},
reclaimPolicy: 'Delete',
volumeBindingMode: 'Immediate',
}],
overwrite: true,
});
efsStorageClass.node.addDependency(fileSystem);
efsStorageClass.node.addDependency(efsCsiAddon);
new cdk.CfnOutput(this, 'EfsFileSystemId', { value: fileSystem.fileSystemId });
// ── Pod Security Standards ─────────────────────────────────────────────
new eks.KubernetesManifest(this, 'PodSecurityStandards', {
removalPolicy: cdk.RemovalPolicy.RETAIN,
cluster,
manifest: [{
apiVersion: 'v1',
kind: 'Namespace',
metadata: {
name: 'openclaw-system',
labels: {
'pod-security.kubernetes.io/enforce': 'restricted',
'pod-security.kubernetes.io/warn': 'restricted',
'pod-security.kubernetes.io/audit': 'restricted',
},
},
}],
overwrite: true,
});
// ── AWS Load Balancer Controller ────────────────────────────────────────
const lbcSa = cluster.addServiceAccount('LbcSa', {
name: 'aws-load-balancer-controller',
namespace: 'kube-system',
});
lbcSa.role.addToPrincipalPolicy(new iam.PolicyStatement({
actions: [
// EC2 — read-only discovery
'ec2:DescribeAccountAttributes', 'ec2:DescribeAddresses', 'ec2:DescribeAvailabilityZones',
'ec2:DescribeInternetGateways', 'ec2:DescribeVpcs', 'ec2:DescribeVpcPeeringConnections',
'ec2:DescribeSubnets', 'ec2:DescribeSecurityGroups', 'ec2:DescribeInstances',
'ec2:DescribeNetworkInterfaces', 'ec2:DescribeTags', 'ec2:DescribeCoipPools',
'ec2:GetCoipPoolUsage',
// EC2 — security group management
'ec2:CreateSecurityGroup', 'ec2:DeleteSecurityGroup',
'ec2:AuthorizeSecurityGroupIngress', 'ec2:RevokeSecurityGroupIngress',
'ec2:CreateTags', 'ec2:DeleteTags',
// ELB — load balancer lifecycle
'elasticloadbalancing:CreateLoadBalancer', 'elasticloadbalancing:DeleteLoadBalancer',
'elasticloadbalancing:DescribeLoadBalancers', 'elasticloadbalancing:DescribeLoadBalancerAttributes',
'elasticloadbalancing:ModifyLoadBalancerAttributes',
// ELB — target groups
'elasticloadbalancing:CreateTargetGroup', 'elasticloadbalancing:DeleteTargetGroup',
'elasticloadbalancing:DescribeTargetGroups', 'elasticloadbalancing:DescribeTargetGroupAttributes',
'elasticloadbalancing:ModifyTargetGroupAttributes',
'elasticloadbalancing:RegisterTargets', 'elasticloadbalancing:DeregisterTargets',
'elasticloadbalancing:DescribeTargetHealth',
// ELB — listeners & rules
'elasticloadbalancing:CreateListener', 'elasticloadbalancing:DeleteListener',
'elasticloadbalancing:DescribeListeners', 'elasticloadbalancing:DescribeListenerCertificates',
'elasticloadbalancing:DescribeListenerAttributes', 'elasticloadbalancing:ModifyListener',
'elasticloadbalancing:CreateRule', 'elasticloadbalancing:DeleteRule',
'elasticloadbalancing:DescribeRules', 'elasticloadbalancing:ModifyRule', 'elasticloadbalancing:SetRulePriorities',
// ELB — misc
'elasticloadbalancing:AddTags', 'elasticloadbalancing:RemoveTags',
'elasticloadbalancing:SetSecurityGroups', 'elasticloadbalancing:SetSubnets',
'elasticloadbalancing:DescribeSSLPolicies', 'elasticloadbalancing:DescribeTags',
'acm:ListCertificates', 'acm:DescribeCertificate',
'iam:ListServerCertificates', 'iam:GetServerCertificate',
'iam:CreateServiceLinkedRole',
'cognito-idp:DescribeUserPoolClient',
'wafv2:GetWebACL', 'wafv2:GetWebACLForResource',
'wafv2:AssociateWebACL', 'wafv2:DisassociateWebACL',
'shield:GetSubscriptionState', 'shield:DescribeProtection',
'shield:CreateProtection', 'shield:DeleteProtection',
],
// Wildcard required: AWS Load Balancer Controller manages dynamically-created
// ALBs, target groups, security groups, and listeners. Resource ARNs are not
// known at deploy time. See: https://kubernetes-sigs.github.io/aws-load-balancer-controller/latest/deploy/installation/#iam-permissions
resources: ['*'],
}));
NagSuppressions.addResourceSuppressions(lbcSa.role, [{
id: 'AwsSolutions-IAM5',
reason: 'AWS Load Balancer Controller requires wildcard resources to manage dynamically-created ALBs, target groups, and security groups. Per official IAM policy: https://kubernetes-sigs.github.io/aws-load-balancer-controller/latest/deploy/installation/#iam-permissions',
}], true);
cluster.addHelmChart('LbController', {
chart: 'aws-load-balancer-controller',
repository: 'https://aws.github.io/eks-charts',
namespace: 'kube-system',
values: {
clusterName: cluster.clusterName,
serviceAccount: { create: false, name: 'aws-load-balancer-controller' },
region: this.region,
vpcId: vpc.vpcId,
controllerConfig: { featureGates: { ALBGatewayAPI: true } },
},
});
// ── Karpenter ───────────────────────────────────────────────────────────
// Node IAM Role
const karpenterNodeRole = new iam.Role(this, 'KarpenterNodeRole', {
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
managedPolicies: [
iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonEKSWorkerNodePolicy'),
iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonEKS_CNI_Policy'),
iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonEC2ContainerRegistryReadOnly'),
iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonSSMManagedInstanceCore'),
],
});
new iam.CfnInstanceProfile(this, 'KarpenterInstanceProfile', {
roles: [karpenterNodeRole.roleName],
});
cluster.awsAuth.addRoleMapping(karpenterNodeRole, {
groups: ['system:bootstrappers', 'system:nodes'],
username: 'system:node:{{EC2PrivateDNSName}}',
});
// Namespace (must exist before SA)
const karpenterNs = new eks.KubernetesManifest(this, 'KarpenterNs', {
removalPolicy: cdk.RemovalPolicy.RETAIN,
cluster,
manifest: [{
apiVersion: 'v1',
kind: 'Namespace',
metadata: { name: 'karpenter' },
}],
overwrite: true,
});
// Controller SA
const karpenterSa = cluster.addServiceAccount('KarpenterSa', {
name: 'karpenter',
namespace: 'karpenter',
});
karpenterSa.node.addDependency(karpenterNs);
karpenterSa.role.addToPrincipalPolicy(new iam.PolicyStatement({
actions: [
'ec2:CreateLaunchTemplate', 'ec2:CreateFleet', 'ec2:RunInstances',
'ec2:CreateTags', 'ec2:TerminateInstances', 'ec2:DeleteLaunchTemplate',
'ec2:DescribeLaunchTemplates', 'ec2:DescribeInstances',
'ec2:DescribeSecurityGroups', 'ec2:DescribeSubnets',
'ec2:DescribeImages', 'ec2:DescribeInstanceTypes',
'ec2:DescribeInstanceTypeOfferings', 'ec2:DescribeAvailabilityZones',
'ec2:DescribeSpotPriceHistory', 'pricing:GetProducts',
'ssm:GetParameter', 'iam:PassRole',
'iam:CreateInstanceProfile', 'iam:TagInstanceProfile',
'iam:AddRoleToInstanceProfile', 'iam:RemoveRoleFromInstanceProfile',
'iam:DeleteInstanceProfile', 'iam:GetInstanceProfile',
'eks:DescribeCluster',
'sqs:DeleteMessage', 'sqs:GetQueueAttributes',
'sqs:GetQueueUrl', 'sqs:ReceiveMessage',
],
// Wildcard required: Karpenter dynamically provisions and terminates EC2
// instances, launch templates, and instance profiles. Resource ARNs are
// generated at runtime. See: https://karpenter.sh/docs/getting-started/getting-started-with-karpenter/#create-the-karpenter-iam-role
resources: ['*'],
}));
NagSuppressions.addResourceSuppressions(karpenterSa.role, [{
id: 'AwsSolutions-IAM5',
reason: 'Karpenter requires wildcard resources to dynamically provision EC2 instances, launch templates, and instance profiles. Per official IAM policy: https://karpenter.sh/docs/getting-started/',
}], true);
// Helm chart
const karpenterChart = cluster.addHelmChart('Karpenter', {
chart: 'karpenter',
repository: 'oci://public.ecr.aws/karpenter/karpenter',
namespace: 'karpenter',
createNamespace: false,
// Karpenter v1.3.x uses karpenter.sh/v1 and karpenter.k8s.aws/v1 APIs.
// When upgrading, check API version compatibility: https://karpenter.sh/docs/upgrading/
version: '1.3.3',
values: {
serviceAccount: { create: false, name: 'karpenter' },
settings: {
clusterName: cluster.clusterName,
clusterEndpoint: cluster.clusterEndpoint,
interruptionQueue: '',
},
controller: {
resources: {
requests: { cpu: '1', memory: '1Gi' },
limits: { cpu: '1', memory: '1Gi' },
},
},
},
});
karpenterChart.node.addDependency(karpenterSa);
// EC2NodeClass — use internal-elb tag for private subnets, cluster SG tag for security groups
const nodeClass = new eks.KubernetesManifest(this, 'KarpenterNodeClass', {
removalPolicy: cdk.RemovalPolicy.RETAIN,
cluster,
manifest: [{
apiVersion: 'karpenter.k8s.aws/v1',
kind: 'EC2NodeClass',
metadata: { name: 'default' },
spec: {
amiSelectorTerms: [{ alias: 'al2023@latest' }],
role: karpenterNodeRole.roleName,
subnetSelectorTerms: [
{ tags: { 'kubernetes.io/role/internal-elb': '1', [`kubernetes.io/cluster/${cluster.clusterName}`]: 'owned' } },
],
securityGroupSelectorTerms: [
{ tags: { [`kubernetes.io/cluster/${cluster.clusterName}`]: 'owned' } },
],
},
}],
overwrite: true,
});
nodeClass.node.addDependency(karpenterChart);
// NodePool
const nodePool = new eks.KubernetesManifest(this, 'KarpenterNodePool', {
removalPolicy: cdk.RemovalPolicy.RETAIN,
cluster,
manifest: [{
apiVersion: 'karpenter.sh/v1',
kind: 'NodePool',
metadata: { name: 'default' },
spec: {
template: {
spec: {
nodeClassRef: { group: 'karpenter.k8s.aws', kind: 'EC2NodeClass', name: 'default' },
requirements: [
{ key: 'karpenter.sh/capacity-type', operator: 'In', values: ['on-demand', 'spot'] },
{ key: 'kubernetes.io/arch', operator: 'In', values: ['arm64'] },
{ key: 'karpenter.k8s.aws/instance-category', operator: 'In', values: ['c', 'm', 'r', 't'] },
{ key: 'karpenter.k8s.aws/instance-generation', operator: 'Gt', values: ['2'] },
{ key: 'karpenter.k8s.aws/instance-size', operator: 'In', values: ['medium', 'large', 'xlarge'] },
],
},
},
limits: { cpu: '100' },
disruption: {
consolidationPolicy: 'WhenEmptyOrUnderutilized',
consolidateAfter: '1m',
},
},
}],
overwrite: true,
});
nodePool.node.addDependency(nodeClass);
// ── ArgoCD ────────────────────────────────────────────────────────────
// Installed via Helm (scripts/setup-argocd.sh). For production, consider EKS Capability:
// aws eks create-capability --type ARGOCD (requires AWS Identity Center)
// See scripts/setup-argocd.sh for details.
// EKS Capability provides: fully managed ArgoCD, automatic upgrades, Identity Center auth.
// ── Shared Tenant IAM Role ──────────────────────────────────────────────
const tenantRole = new iam.Role(this, 'TenantRole', {
assumedBy: new iam.ServicePrincipal('pods.eks.amazonaws.com'),
description: 'Shared IAM role for all OpenClaw tenant pods (ABAC via EKS Pod Identity)',
});
tenantRole.assumeRolePolicy!.addStatements(new iam.PolicyStatement({
actions: ['sts:TagSession'],
principals: [new iam.ServicePrincipal('pods.eks.amazonaws.com')],
}));
// Bedrock: invoke — us. inference profiles route cross-region, allow all regions
tenantRole.addToPrincipalPolicy(new iam.PolicyStatement({
sid: 'BedrockInvoke',
actions: ['bedrock:InvokeModel', 'bedrock:InvokeModelWithResponseStream'],
resources: [
'arn:aws:bedrock:*::foundation-model/*',
`arn:aws:bedrock:*:${this.account}:inference-profile/*`,
],
}));
// Bedrock: model discovery
tenantRole.addToPrincipalPolicy(new iam.PolicyStatement({
sid: 'BedrockDiscovery',
actions: ['bedrock:ListFoundationModels', 'bedrock:ListInferenceProfiles', 'bedrock:GetInferenceProfile'],
// Wildcard required: ListFoundationModels and ListInferenceProfiles are
// account-level APIs that only support '*' as resource ARN.
// See: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_ListFoundationModels.html
resources: ['*'],
}));
NagSuppressions.addResourceSuppressions(tenantRole, [{
id: 'AwsSolutions-IAM5',
reason: 'Amazon Bedrock ListFoundationModels/ListInferenceProfiles are account-level APIs that only support wildcard resource. See: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_ListFoundationModels.html',
appliesTo: ['Resource::*'],
}], true);
// Secrets Manager: ABAC — tenant can only read secrets tagged with its own namespace
tenantRole.addToPrincipalPolicy(new iam.PolicyStatement({
sid: 'SecretsManagerABAC',
actions: ['secretsmanager:GetSecretValue'],
resources: ['*'],
conditions: {
StringEquals: {
'secretsmanager:ResourceTag/tenant-namespace': '${aws:PrincipalTag/kubernetes-namespace}',
},
},
}));
// AgentCore Browser: session management + automation + profile persistence
tenantRole.addToPrincipalPolicy(new iam.PolicyStatement({
sid: 'AgentCoreBrowser',
actions: [
'bedrock-agentcore:StartBrowserSession',
'bedrock-agentcore:StopBrowserSession',
'bedrock-agentcore:GetBrowserSession',
'bedrock-agentcore:ListBrowserSessions',
'bedrock-agentcore:ConnectBrowserAutomationStream',
'bedrock-agentcore:ConnectBrowserLiveViewStream',
'bedrock-agentcore:GetBrowserProfile',
'bedrock-agentcore:SaveBrowserSessionProfile',
],
resources: [`arn:aws:bedrock-agentcore:${this.region}:${this.account}:browser/*`],
}));
// ── Other imported/optional resources ────────────────────────────────────
const domainName = this.node.tryGetContext('zoneName') || '';
const useCustomDomain = !!domainName && domainName !== 'example.com';
const allowedEmailDomains = this.node.tryGetContext('allowedEmailDomains') || 'example.com';
const githubOwner = this.node.tryGetContext('githubOwner') || '';
const githubRepo = this.node.tryGetContext('githubRepo') || 'openclaw-platform';
const certArn = this.node.tryGetContext('certificateArn') || '';
const certificate = certArn
? acm.Certificate.fromCertificateArn(this, 'Certificate', certArn)
: undefined;
// ── CloudWatch Alerts ──────────────────────────────────────────────────
const alertsTopic = new sns.Topic(this, 'AlertsTopic', {
topicName: `${this.stackName}-Alerts`,
masterKey: eksSecretsKey,
});
const podRestartAlarm = new cloudwatch.Alarm(this, 'PodRestartAlarm', {
alarmName: `${this.stackName}-PodRestartCount`,
metric: new cloudwatch.Metric({
namespace: 'ContainerInsights',
metricName: 'pod_number_of_container_restarts',
dimensionsMap: { ClusterName: cluster.clusterName },
period: cdk.Duration.seconds(300),
statistic: 'Sum',
}),
evaluationPeriods: 1,
threshold: 0,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
});
podRestartAlarm.addAlarmAction(new cw_actions.SnsAction(alertsTopic));
// Cold start alarm — alert when pod startup exceeds 60s
// Create log groups explicitly so MetricFilters don't fail when
// Container Insights hasn't created them yet. retentionDays ensures
// CDK owns the log group; Container Insights will write to it once active.
const perfLogGroup = new logs.LogGroup(this, 'PerfLogGroup', {
logGroupName: `/aws/containerinsights/${cluster.clusterName}/performance`,
retention: logs.RetentionDays.ONE_MONTH,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
const coldStartFilter = new logs.MetricFilter(this, 'ColdStartFilter', {
logGroup: perfLogGroup,
filterPattern: logs.FilterPattern.all(
logs.FilterPattern.stringValue('$.Type', '=', 'Pod'),
logs.FilterPattern.stringValue('$.PodStatus', '=', 'Running'),
logs.FilterPattern.exists('$.pod_startup_duration_seconds'),
),
metricNamespace: 'OpenClaw/ColdStart',
metricName: 'PodStartupDurationSeconds',
metricValue: '$.pod_startup_duration_seconds',
defaultValue: 0,
});
const coldStartAlarm = new cloudwatch.Alarm(this, 'ColdStartAlarm', {
alarmName: `${this.stackName}-PodColdStartSlow`,
metric: coldStartFilter.metric({ statistic: 'Maximum', period: cdk.Duration.seconds(300) }),
evaluationPeriods: 1,
threshold: 60,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
});
coldStartAlarm.addAlarmAction(new cw_actions.SnsAction(alertsTopic));
// Bedrock latency alarm — alert when P95 response time exceeds 10s
const appLogGroup = new logs.LogGroup(this, 'AppLogGroup', {
logGroupName: `/aws/containerinsights/${cluster.clusterName}/application`,
retention: logs.RetentionDays.ONE_MONTH,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
const bedrockLatencyFilter = new logs.MetricFilter(this, 'BedrockLatencyFilter', {
logGroup: appLogGroup,
filterPattern: logs.FilterPattern.literal('{ $.message = "*bedrock*response*" && $.duration = * }'),
metricNamespace: 'OpenClaw/Bedrock',
metricName: 'BedrockResponseTimeMs',
metricValue: '$.duration',
defaultValue: 0,
});
const bedrockLatencyAlarm = new cloudwatch.Alarm(this, 'BedrockLatencyAlarm', {
alarmName: `${this.stackName}-BedrockP95Latency`,
metric: bedrockLatencyFilter.metric({ statistic: 'p95', period: cdk.Duration.seconds(300) }),
evaluationPeriods: 2,
threshold: 10000,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
});
bedrockLatencyAlarm.addAlarmAction(new cw_actions.SnsAction(alertsTopic));
// ── OpenClaw Image ──────────────────────────────────────────────────────
// Default: pull directly from ghcr.io (public, no credentials needed).
// To enable ECR pull-through cache for production:
// 1. Create a GHCR PAT and store in Secrets Manager (name: ecr-pullthroughcache/ghcr)
// 2. Set ghcrCredentialArn in cdk.json to the secret ARN
// 3. cdk deploy — images will be cached in ECR automatically
const ghcrCredentialArn = this.node.tryGetContext('ghcrCredentialArn') as string | undefined;
if (ghcrCredentialArn) {
new ecr.CfnPullThroughCacheRule(this, 'GhcrCache', {
ecrRepositoryPrefix: 'ghcr',
upstreamRegistryUrl: 'ghcr.io',
credentialArn: ghcrCredentialArn,
});
}
const openclawImage = this.node.tryGetContext('openclawImage')
|| (ghcrCredentialArn
? `${this.account}.dkr.ecr.${this.region}.amazonaws.com/ghcr/openclaw/openclaw:latest`
: 'ghcr.io/openclaw/openclaw:latest');
// ── Lambda: Pre-Signup ──────────────────────────────────────────────────
const selfSignupEnabled = this.node.tryGetContext('selfSignupEnabled') !== false;
// Dead-letter queue for Lambda trigger failures
const triggerDlq = new sqs.Queue(this, 'TriggerDLQ', {
encryption: sqs.QueueEncryption.SQS_MANAGED,
retentionPeriod: cdk.Duration.days(14),
});
const preSignupFn = new lambda.Function(this, 'PreSignupFn', {
runtime: lambda.Runtime.PYTHON_3_12,
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda/pre-signup'),
environment: { SNS_TOPIC_ARN: alertsTopic.topicArn, ALLOWED_DOMAINS: allowedEmailDomains, SIGNUP_RATE_LIMIT: '20' },
timeout: cdk.Duration.seconds(10),
deadLetterQueue: triggerDlq,
});
alertsTopic.grantPublish(preSignupFn);
// ── Lambda: Post-Confirmation ───────────────────────────────────────────
const postConfirmFn = new lambda.Function(this, 'PostConfirmFn', {
runtime: lambda.Runtime.PYTHON_3_12,
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda/post-confirmation'),
environment: {
SNS_TOPIC_ARN: alertsTopic.topicArn,
CLUSTER_NAME: cluster.clusterName,
CERTIFICATE_ARN: certificate?.certificateArn || '',
DOMAIN: domainName,
OPENCLAW_IMAGE: openclawImage,
TENANT_ROLE_ARN: tenantRole.roleArn,
},
timeout: cdk.Duration.seconds(60),
deadLetterQueue: triggerDlq,
});
alertsTopic.grantPublish(postConfirmFn);
postConfirmFn.addToRolePolicy(new iam.PolicyStatement({
actions: ['secretsmanager:CreateSecret', 'secretsmanager:TagResource', 'secretsmanager:GetSecretValue', 'secretsmanager:RestoreSecret', 'secretsmanager:UpdateSecret'],
resources: [`arn:aws:secretsmanager:${this.region}:${this.account}:secret:openclaw/*`],
}));
postConfirmFn.addToRolePolicy(new iam.PolicyStatement({
actions: ['eks:CreatePodIdentityAssociation', 'eks:DescribeCluster'],
resources: [`arn:aws:eks:${this.region}:${this.account}:cluster/${cluster.clusterName}`],
}));
postConfirmFn.addToRolePolicy(new iam.PolicyStatement({
actions: ['iam:PassRole', 'iam:GetRole'],
resources: [tenantRole.roleArn],
}));
// ── Cognito: Complete UserPool with Lambda Triggers ──────────────────
// Set triggers in constructor. Use attachInlinePolicy (not addToRolePolicy)
// for Cognito-scoped permissions to avoid circular dependency.
// See: https://github.com/aws/aws-cdk/issues/7016
const cognitoDomainPrefix = this.node.tryGetContext('cognitoDomain')
|| `openclaw-${this.account}-${this.region}`;
const userPool = new cognito.UserPool(this, 'UserPool', {
userPoolName: `openclaw-${this.stackName}`,
selfSignUpEnabled: selfSignupEnabled,
signInAliases: { email: true },
autoVerify: { email: true },
passwordPolicy: {
minLength: 12,
requireUppercase: true,
requireLowercase: true,
requireDigits: true,
requireSymbols: false,
},
customAttributes: {
gateway_token: new cognito.StringAttribute({ mutable: true }),
tenant_name: new cognito.StringAttribute({ mutable: true }),
},
// SYSTEMATIC FIX: Native CDK trigger configuration (eliminates custom resource)
lambdaTriggers: {
preSignUp: preSignupFn,
postConfirmation: postConfirmFn,
},
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
// Explicit L1 override: ensure self-signup is always enabled.
// CDK's selfSignUpEnabled maps to AllowAdminCreateUserOnly=false, but
// CloudFormation has been observed to default to true on fresh deploys.
(userPool.node.defaultChild as cognito.CfnUserPool).addPropertyOverride(
'AdminCreateUserConfig.AllowAdminCreateUserOnly', false);
userPool.addDomain('CognitoDomain', {
cognitoDomain: { domainPrefix: cognitoDomainPrefix },
});
const userPoolClient = userPool.addClient('WebClient', {
generateSecret: false,
authFlows: { userPassword: true, userSrp: true },
readAttributes: new cognito.ClientAttributes()
.withStandardAttributes({ email: true })
.withCustomAttributes('gateway_token', 'tenant_name'),
writeAttributes: new cognito.ClientAttributes()
.withStandardAttributes({ email: true }),
});
// Cognito-scoped IAM — use wildcard to avoid circular dependency
// (attachInlinePolicy with userPool.userPoolArn still creates cycle)
preSignupFn.addToRolePolicy(new iam.PolicyStatement({
actions: ['cognito-idp:ListUsers'],
resources: ['*'],
}));
NagSuppressions.addResourceSuppressions(preSignupFn, [{
id: 'AwsSolutions-IAM5',
reason: 'Wildcard required to avoid circular dependency between UserPool and Lambda trigger. See aws/aws-cdk#7016.',
}], true);
postConfirmFn.addToRolePolicy(new iam.PolicyStatement({
actions: ['cognito-idp:AdminUpdateUserAttributes'],
resources: ['*'],
}));
NagSuppressions.addResourceSuppressions(postConfirmFn, [{
id: 'AwsSolutions-IAM5',
reason: 'Wildcard required to avoid circular dependency between UserPool and Lambda trigger. See aws/aws-cdk#7016.',
}], true);
// USER_POOL_ID is NOT passed as env var to avoid circular dependency
// (lambdaTriggers: UserPool→Lambda, addEnvironment: Lambda→UserPool = cycle).
// Both Lambdas read event['userPoolId'] at runtime instead — Cognito trigger
// events always include the UserPool ID.
// Lambda triggers now handled natively by CDK lambdaTriggers property
// This eliminates configuration drift and removes ~40 lines of complex custom resource code
cluster.awsAuth.addRoleMapping(postConfirmFn.role!, {
groups: ['openclaw:tenant-provisioner'],
username: 'lambda-post-confirm',
});
// RBAC: scoped permissions for PostConfirmation Lambda (not system:masters)
new eks.KubernetesManifest(this, 'TenantProvisionerRbac', {
cluster,
manifest: [
{
apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'ClusterRole',
metadata: { name: 'openclaw-tenant-provisioner' },
rules: [
{ apiGroups: [''], resources: ['namespaces', 'secrets', 'configmaps'], verbs: ['create', 'get', 'list', 'patch', 'update'] },
{ apiGroups: ['argoproj.io'], resources: ['applicationsets', 'applications'], verbs: ['create', 'get', 'list', 'patch', 'update'] },
],
},
{
apiVersion: 'rbac.authorization.k8s.io/v1',
kind: 'ClusterRoleBinding',
metadata: { name: 'openclaw-tenant-provisioner' },
roleRef: { apiGroup: 'rbac.authorization.k8s.io', kind: 'ClusterRole', name: 'openclaw-tenant-provisioner' },
subjects: [{ kind: 'Group', name: 'openclaw:tenant-provisioner', apiGroup: 'rbac.authorization.k8s.io' }],
},
],
overwrite: true,
});
// ── CloudFront WAF (via Custom Resource in us-east-1) ─────────────────
// CloudFront WAF must be in us-east-1 regardless of stack region.
// Uses a custom resource (AwsSdkCall) to create/delete the WAF in us-east-1,
// avoiding cross-region stack dependencies that complicate deployment and deletion.
const enableBotControl = this.node.tryGetContext('enableBotControl') === true;
const wafRules: object[] = [
{
Name: 'AWSManagedRulesCommonRuleSet',
Priority: 1,
OverrideAction: { None: {} },
Statement: {
ManagedRuleGroupStatement: { VendorName: 'AWS', Name: 'AWSManagedRulesCommonRuleSet' },
},
VisibilityConfig: { CloudWatchMetricsEnabled: true, MetricName: 'CommonRuleSet', SampledRequestsEnabled: true },
},
{
Name: 'RateLimit',
Priority: 2,
Action: { Block: {} },
Statement: { RateBasedStatement: { Limit: 2000, AggregateKeyType: 'IP' } },
VisibilityConfig: { CloudWatchMetricsEnabled: true, MetricName: 'RateLimit', SampledRequestsEnabled: true },
},
];
if (enableBotControl) {
wafRules.push({
Name: 'AWSManagedRulesBotControlRuleSet',
Priority: 3,
OverrideAction: { None: {} },
Statement: {
ManagedRuleGroupStatement: {
VendorName: 'AWS', Name: 'AWSManagedRulesBotControlRuleSet',
ManagedRuleGroupConfigs: [{ AWSManagedRulesBotControlRuleSet: { InspectionLevel: 'COMMON' } }],
},
},
VisibilityConfig: { CloudWatchMetricsEnabled: true, MetricName: 'BotControl', SampledRequestsEnabled: true },
});
}
const cfWafName = `${this.stackName}-CF-WAF`;
// CloudFront WAF: if stack is in us-east-1, use native CfnWebACL (simpler, idempotent).
// If not, use AwsCustomResource to create WAF in us-east-1 via SDK call.
let cfWafArn: string;
if (this.region === 'us-east-1') {
const cfWaf = new wafv2.CfnWebACL(this, 'CloudFrontWaf', {
defaultAction: { allow: {} },
scope: 'CLOUDFRONT',
visibilityConfig: {
cloudWatchMetricsEnabled: true,
metricName: 'OpenClawCloudFrontWaf',
sampledRequestsEnabled: true,
},
rules: wafRules.map((r: any) => ({
name: r.Name, priority: r.Priority,
...(r.Action ? { action: { block: {} } } : { overrideAction: { none: {} } }),
statement: r.Statement.RateBasedStatement
? { rateBasedStatement: { limit: r.Statement.RateBasedStatement.Limit, aggregateKeyType: r.Statement.RateBasedStatement.AggregateKeyType } }
: { managedRuleGroupStatement: { vendorName: r.Statement.ManagedRuleGroupStatement.VendorName, name: r.Statement.ManagedRuleGroupStatement.Name } },
visibilityConfig: { cloudWatchMetricsEnabled: r.VisibilityConfig.CloudWatchMetricsEnabled, metricName: r.VisibilityConfig.MetricName, sampledRequestsEnabled: r.VisibilityConfig.SampledRequestsEnabled },
})),
});
cfWafArn = cfWaf.attrArn;
} else {
const createCfWaf = new cr.AwsCustomResource(this, 'CloudFrontWaf', {
onCreate: {
service: 'WAFV2',
action: 'createWebACL',
parameters: {
Name: cfWafName,
Scope: 'CLOUDFRONT',
DefaultAction: { Allow: {} },
VisibilityConfig: { CloudWatchMetricsEnabled: true, MetricName: 'OpenClawCloudFrontWaf', SampledRequestsEnabled: true },
Rules: wafRules,
},
region: 'us-east-1',
physicalResourceId: cr.PhysicalResourceId.fromResponse('Summary.ARN'),
},
// NOTE: No onDelete — WAF deleteWebACL requires LockToken from GetWebACL,
// which AwsCustomResource cannot chain. force-cleanup.sh handles WAF deletion.
policy: cr.AwsCustomResourcePolicy.fromStatements([
new iam.PolicyStatement({
actions: ['wafv2:CreateWebACL', 'wafv2:GetWebACL'],
resources: ['*'],
}),
]),
});
NagSuppressions.addResourceSuppressions(createCfWaf, [{
id: 'AwsSolutions-IAM5',
reason: 'WAF custom resource needs wildcard because WAF ARN is not known at deploy time (created in us-east-1 via SDK call).',
}], true);
cfWafArn = createCfWaf.getResponseField('Summary.ARN');
}
// S3 bucket for auth UI static site
const authUiBucket = new s3.Bucket(this, 'AuthUiBucket', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
encryption: s3.BucketEncryption.S3_MANAGED,
enforceSSL: true,
versioned: true,
// Production: add server access logging to a dedicated log bucket
});
const oai = new cloudfront.OriginAccessIdentity(this, 'AuthUiOAI');
authUiBucket.grantRead(oai);
const distribution = new cloudfront.CloudFrontWebDistribution(this, 'Distribution', {
// CloudFront WAF (CLOUDFRONT scope, created in us-east-1 via custom resource)
webACLId: cfWafArn,
...(useCustomDomain && certificate ? {
viewerCertificate: cloudfront.ViewerCertificate.fromAcmCertificate(
acm.Certificate.fromCertificateArn(this, 'CfCert',
this.node.tryGetContext('cloudfrontCertificateArn') || certificate.certificateArn,
),
{
aliases: [domainName],
securityPolicy: cloudfront.SecurityPolicyProtocol.TLS_V1_2_2021,
},
),
} : {}),
originConfigs: [
{
// Auth UI static site (login, signup, welcome pages)
s3OriginSource: {
s3BucketSource: authUiBucket,
originAccessIdentity: oai,
},
behaviors: [
{
isDefaultBehavior: true,
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
},
],
},
],