Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 1a11594

Browse files
authored
feat(aws-cloudfront, nextjs-component): support setting WAF web ACL id (#724)
1 parent 531622c commit 1a11594

File tree

6 files changed

+122
-31
lines changed

6 files changed

+122
-31
lines changed

README.md

Lines changed: 31 additions & 29 deletions
Large diffs are not rendered by default.

packages/serverless-components/aws-cloudfront/__tests__/general-options.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,71 @@ describe("General options propagation", () => {
176176
})
177177
);
178178
});
179+
180+
it("create distribution with web ACL id and update it", async () => {
181+
// Create
182+
await component.default({
183+
webACLId:
184+
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/ExampleWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a",
185+
origins
186+
});
187+
188+
expect(mockCreateDistribution).toBeCalledWith(
189+
expect.objectContaining({
190+
DistributionConfig: expect.objectContaining({
191+
WebACLId:
192+
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/ExampleWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a"
193+
})
194+
})
195+
);
196+
197+
// Update
198+
await component.default({
199+
webACLId:
200+
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/UpdatedWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a",
201+
origins
202+
});
203+
204+
expect(mockUpdateDistribution).toBeCalledWith(
205+
expect.objectContaining({
206+
DistributionConfig: expect.objectContaining({
207+
WebACLId:
208+
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/UpdatedWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a"
209+
})
210+
})
211+
);
212+
});
213+
214+
it("create distribution with web ACL id and delete it", async () => {
215+
// Create
216+
await component.default({
217+
webACLId:
218+
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/ExampleWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a",
219+
origins
220+
});
221+
222+
expect(mockCreateDistribution).toBeCalledWith(
223+
expect.objectContaining({
224+
DistributionConfig: expect.objectContaining({
225+
WebACLId:
226+
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/ExampleWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a"
227+
})
228+
})
229+
);
230+
231+
// Delete
232+
// Per AWS, providing an empty ACLId will remove the WAF association: https://docs.aws.amazon.com/waf/latest/APIReference/API_DisassociateWebACL.html
233+
await component.default({
234+
webACLId: "",
235+
origins
236+
});
237+
238+
expect(mockUpdateDistribution).toBeCalledWith(
239+
expect.objectContaining({
240+
DistributionConfig: expect.objectContaining({
241+
WebACLId: ""
242+
})
243+
})
244+
);
245+
});
179246
});

packages/serverless-components/aws-cloudfront/lib/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ const createCloudFrontDistribution = async (cf, s3, inputs) => {
9191
const CustomErrorResponses = getCustomErrorResponses(inputs.errorPages);
9292
distributionConfig.CustomErrorResponses = CustomErrorResponses;
9393

94+
// Set WAF web ACL id if defined
95+
if (inputs.webACLId !== undefined && inputs.webACLId !== null) {
96+
distributionConfig.WebACLId = inputs.webACLId;
97+
}
98+
9499
const res = await cf.createDistribution(params).promise();
95100

96101
return {
@@ -136,6 +141,11 @@ const updateCloudFrontDistribution = async (cf, s3, distributionId, inputs) => {
136141
};
137142
}
138143

144+
// When updating, don't override any existing webACLId if not set in inputs
145+
if (inputs.webACLId !== undefined && inputs.webACLId !== null) {
146+
params.DistributionConfig.WebACLId = inputs.webACLId;
147+
}
148+
139149
let s3CanonicalUserId;
140150
let originAccessIdentityId;
141151

packages/serverless-components/aws-cloudfront/serverless.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class CloudFront extends Component {
5959
!equals(this.state.comment, inputs.comment) ||
6060
!equals(this.state.aliases, inputs.aliases) ||
6161
!equals(this.state.priceClass, inputs.priceClass) ||
62-
!equals(this.state.errorPages, inputs.errorPages)
62+
!equals(this.state.errorPages, inputs.errorPages) ||
63+
!equals(this.state.webACLId, inputs.webACLId)
6364
) {
6465
this.context.debug(
6566
`Updating CloudFront distribution of ID ${this.state.id}.`

packages/serverless-components/nextjs-component/__tests__/custom-inputs.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,5 +1075,14 @@ describe("Custom inputs", () => {
10751075
}
10761076
});
10771077
});
1078+
1079+
it("sets web ACL id for AWS WAF", async () => {
1080+
await createNextComponent().default({
1081+
cloudfront: {
1082+
webACLId:
1083+
"arn:aws:wafv2:us-east-1:123456789012:global/webacl/ExampleWebACL/473e64fd-f30b-4765-81a0-62ad96dd167a"
1084+
}
1085+
});
1086+
});
10781087
});
10791088
});

packages/serverless-components/nextjs-component/src/component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ class NextjsComponent extends Component {
239239
errorPages: cloudFrontErrorPagesInputs,
240240
distributionId: cloudFrontDistributionId = null,
241241
comment: cloudFrontComment,
242+
webACLId: cloudFrontWebACLId,
242243
...cloudFrontOtherInputs
243244
} = inputs.cloudfront || {};
244245

@@ -587,7 +588,8 @@ class NextjsComponent extends Component {
587588
...(cloudFrontErrorPagesInputs && {
588589
errorPages: cloudFrontErrorPagesInputs
589590
}),
590-
comment: cloudFrontComment
591+
comment: cloudFrontComment,
592+
webACLId: cloudFrontWebACLId
591593
});
592594

593595
let appUrl = cloudFrontOutputs.url;

0 commit comments

Comments
 (0)