Skip to content

Commit 2a1319e

Browse files
committed
fix: waf us-east-1
1 parent 6158664 commit 2a1319e

File tree

5 files changed

+92
-47
lines changed

5 files changed

+92
-47
lines changed

backend/cdk/bin/cdk.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import * as cdk from "aws-cdk-lib";
44
import { MainStack } from "../lib/main-stack";
55

66
const app = new cdk.App();
7+
78
new MainStack(app, "ChatModeration", {
8-
/* If you don't specify 'env', this stack will be environment-agnostic.
9-
* Account/Region-dependent features and context lookups will not work,
10-
* but a single synthesized template can be deployed anywhere. */
11-
/* Uncomment the next line to specialize this stack for the AWS Account
12-
* and Region that are implied by the current CLI configuration. */
13-
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
14-
/* Uncomment the next line if you know exactly what Account and Region you
15-
* want to deploy the stack to. */
16-
// env: { account: '123456789012', region: 'us-east-1' },
17-
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
9+
env: {
10+
account: process.env.CDK_DEFAULT_ACCOUNT,
11+
region: process.env.CDK_DEFAULT_REGION,
12+
},
13+
crossRegionReferences: true,
1814
});
15+
16+
app.synth();

backend/cdk/lib/front-end-stack.ts

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ import * as cdk from "aws-cdk-lib";
22
import * as s3 from "aws-cdk-lib/aws-s3";
33
import * as cloudfront from "aws-cdk-lib/aws-cloudfront";
44
import * as iam from "aws-cdk-lib/aws-iam";
5-
import * as waf from "aws-cdk-lib/aws-wafv2";
65
import { Construct } from "constructs";
76

87
interface FrontEndProps extends cdk.NestedStackProps {
98
stackName: string;
9+
webAclArn: string;
10+
crossRegionReferences: boolean;
11+
env?: {
12+
region?: string;
13+
account?: string;
14+
};
1015
}
1116

1217
export class FrontEnd extends cdk.NestedStack {
@@ -56,36 +61,6 @@ export class FrontEnd extends cdk.NestedStack {
5661
removalPolicy: cdk.RemovalPolicy.DESTROY,
5762
});
5863

59-
// CloudFront WAF WebACL
60-
const webAcl = new waf.CfnWebACL(this, "ChatModeration-CloudFrontWAF", {
61-
name: "ChatModeration-CloudFrontWAF",
62-
scope: "CLOUDFRONT",
63-
defaultAction: { allow: {} },
64-
visibilityConfig: {
65-
cloudWatchMetricsEnabled: true,
66-
metricName: "ChatModeration-CloudFrontWAF",
67-
sampledRequestsEnabled: true,
68-
},
69-
rules: [
70-
{
71-
name: "LimitRequests100",
72-
priority: 1,
73-
action: { block: {} },
74-
visibilityConfig: {
75-
cloudWatchMetricsEnabled: true,
76-
metricName: "LimitRequests100",
77-
sampledRequestsEnabled: true,
78-
},
79-
statement: {
80-
rateBasedStatement: {
81-
limit: 100,
82-
aggregateKeyType: "IP",
83-
},
84-
},
85-
},
86-
],
87-
});
88-
8964
// CloudFront Distribution for Front-End Static Assets
9065
const cloudFrontDistribution = new cloudfront.CfnDistribution(
9166
this,
@@ -136,7 +111,7 @@ export class FrontEnd extends cdk.NestedStack {
136111
prefix: "cloudfront-logs/",
137112
includeCookies: false,
138113
},
139-
webAclId: webAcl.attrArn,
114+
webAclId: props.webAclArn,
140115
},
141116
}
142117
);

backend/cdk/lib/main-stack.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import * as cdk from "aws-cdk-lib";
2+
import { Construct } from "constructs";
23
import { Database } from "./database-stack";
34
import { Guardrail } from "./guardrail-stack";
45
import { PromptSwitch } from "./prompt-switch-stack";
56
import { Api } from "./api-stack";
7+
import { Waf } from './waf-stack';
68
import { FrontEnd } from "./front-end-stack";
79
import { Observability } from "./observability-stack";
810

9-
export interface MainStackProps extends cdk.StackProps {}
11+
export interface MainStackProps extends cdk.StackProps {
12+
env?: {
13+
region?: string;
14+
account?: string;
15+
};
16+
}
1017

1118
export class MainStack extends cdk.Stack {
1219
public readonly approvedMessagesTableName: string;
@@ -25,9 +32,11 @@ export class MainStack extends cdk.Stack {
2532
public readonly cloudFrontDistributionId: string;
2633
public readonly cloudFrontDistributionDomain: string;
2734

28-
public constructor(scope: cdk.App, id: string, props: MainStackProps = {}) {
29-
super(scope, id, props);
30-
this.templateOptions.description = 'Live Chat Content Moderation with generative AI on AWS (SO9005)'
35+
constructor(scope: Construct, id: string, props?: MainStackProps) {
36+
super(scope, id, {
37+
...props,
38+
crossRegionReferences: true,
39+
});
3140

3241
// Database Nested Stack
3342
const database = new Database(this, "Database", {
@@ -56,9 +65,20 @@ export class MainStack extends cdk.Stack {
5665
promptSwitchParameterName: promptSwitch.promptSwitchParameterName,
5766
});
5867

68+
// WAF Nested Stack in us-east-1
69+
const wafStack = new Waf(this, 'Waf', {
70+
stackName: "Waf",
71+
crossRegionReferences: true,
72+
env: {
73+
account: this.account,
74+
},
75+
});
76+
5977
// Front-End Nested Stack
6078
const frontEnd = new FrontEnd(this, "FrontEnd", {
6179
stackName: "FrontEnd",
80+
webAclArn: wafStack.webAclArn,
81+
crossRegionReferences: true,
6282
});
6383

6484
// Observability Stack

backend/cdk/lib/waf-stack.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as cdk from "aws-cdk-lib";
2+
import * as waf from "aws-cdk-lib/aws-wafv2";
3+
import { Construct } from "constructs";
4+
5+
interface WafProps extends cdk.StackProps {
6+
crossRegionReferences: boolean;
7+
}
8+
9+
export class Waf extends cdk.Stack {
10+
public readonly webAclArn: string;
11+
12+
constructor(scope: Construct, id: string, props: WafProps) {
13+
super(scope, id, {
14+
...props,
15+
env: {
16+
region: 'us-east-1', // Force WAF to be created in us-east-1
17+
account: props.env?.account,
18+
},
19+
});
20+
21+
const webAcl = new waf.CfnWebACL(this, "ChatModeration-CloudFrontWAF", {
22+
name: "ChatModeration-CloudFrontWAF",
23+
scope: "CLOUDFRONT",
24+
defaultAction: { allow: {} },
25+
visibilityConfig: {
26+
cloudWatchMetricsEnabled: true,
27+
metricName: "ChatModeration-CloudFrontWAF",
28+
sampledRequestsEnabled: true,
29+
},
30+
rules: [
31+
{
32+
name: "LimitRequests100",
33+
priority: 1,
34+
action: { block: {} },
35+
visibilityConfig: {
36+
cloudWatchMetricsEnabled: true,
37+
metricName: "LimitRequests100",
38+
sampledRequestsEnabled: true,
39+
},
40+
statement: {
41+
rateBasedStatement: {
42+
limit: 100,
43+
aggregateKeyType: "IP",
44+
},
45+
},
46+
},
47+
],
48+
});
49+
50+
this.webAclArn = webAcl.attrArn;
51+
}
52+
}

scripts/install.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ deploy_cdk_stack() {
119119

120120
echo -e "\n${BLUE}[INFO] CDK Output:${NC}"
121121
# Run 'cdk deploy' command
122-
cdk deploy --require-approval never --outputs-file "$CDK_OUTPUTS_FILE"
122+
cdk deploy --all --require-approval never --outputs-file "$CDK_OUTPUTS_FILE"
123123
if [ $? -ne 0 ]; then
124124
echo -e "\n${RED}[ERROR] CDK deployment failed. Aborting.${NC}"
125125
exit 1

0 commit comments

Comments
 (0)