Skip to content

Commit cbe2bec

Browse files
authored
feat(cloudfront): add attachWebAclId method for Distribution (#30567)
### Reason for this change I often create a custom construct for a WAF only. I also create resources (such as API Gateway, ALB, etc...) that attach the WAF in separate constructs. Instead of attaching the WAF in the target resource's construct, I create a method for attaching it in the WAF's construct. In this way, the constructs can be loosely coupled, and the target resource's constructs can be more simply. The WAF can also be attached to multiple resources at once later. ```ts export class Waf extends Construct { public readonly webAcl: CfnWebACL; constructor(scope: Construct, id: string, props: WafProps) { super(scope, id); this.webAcl = new CfnWebACL(this, 'WebAcl', { // ... }, }); public attachToRegionalResource(id: string, resourceArn: string) { new CfnWebACLAssociation(this, `${id}Association`, { resourceArn: resourceArn, webAclArn: this.webAcl.attrArn, }); } } const waf = new Waf(this, 'Waf', { /* props */ }); waf.attachToRegionalResource('A', resourceA); waf.attachToRegionalResource('B', resourceB); waf.attachToRegionalResource('C', resourceC); ``` However, when attaching a WAF to a CloudFront, the WAF attaching configuration needs to be defined through CloudFront props, rather than using CfnWebACLAssociation. To do this with the above WAF construct, a method is needed to pass a pre-defined CloudFront and override the properties of that definition with an escape hatch. This is a bit complicated. ```ts public attachToCloudFront(distribution: Distribution) { // override the webAcl using escape hatch } ``` In other words, it would be good if CloudFront also had a mechanism (like CfnWebACLAssociation) to attach WAF after defining resources. This would allow WAF custom constructs to be more generic. ### Description of changes Add `attachWebAclId` method for Distribution. ```ts declare const bucketOrigin: origins.S3Origin; declare const webAcl: wafv2.CfnWebACL; const distribution = new cloudfront.Distribution(stack, 'Distribution', { defaultBehavior: { origin: bucketOrigin }, }); distribution.attachWebAclId(webAcl.attrArn); ``` ### Description of how you validated changes Both of unit and integ tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 5d61a1b commit cbe2bec

13 files changed

+583
-1
lines changed

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.cloudfront-with-webacl.js.snapshot/aws-cdk-cloudfront-with-webacl.assets.json

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"Resources": {
3+
"WebAcl": {
4+
"Type": "AWS::WAFv2::WebACL",
5+
"Properties": {
6+
"DefaultAction": {
7+
"Allow": {}
8+
},
9+
"Scope": "CLOUDFRONT",
10+
"VisibilityConfig": {
11+
"CloudWatchMetricsEnabled": false,
12+
"MetricName": "webAclMetric",
13+
"SampledRequestsEnabled": false
14+
}
15+
}
16+
},
17+
"Distribution830FAC52": {
18+
"Type": "AWS::CloudFront::Distribution",
19+
"Properties": {
20+
"DistributionConfig": {
21+
"DefaultCacheBehavior": {
22+
"CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
23+
"Compress": true,
24+
"TargetOriginId": "awscdkcloudfrontwithwebaclDistributionOrigin11CAC3663",
25+
"ViewerProtocolPolicy": "allow-all"
26+
},
27+
"Enabled": true,
28+
"HttpVersion": "http2",
29+
"IPV6Enabled": true,
30+
"Origins": [
31+
{
32+
"CustomOriginConfig": {
33+
"OriginProtocolPolicy": "https-only"
34+
},
35+
"DomainName": "www.example.com",
36+
"Id": "awscdkcloudfrontwithwebaclDistributionOrigin11CAC3663"
37+
}
38+
],
39+
"WebACLId": {
40+
"Fn::GetAtt": [
41+
"WebAcl",
42+
"Arn"
43+
]
44+
}
45+
}
46+
}
47+
}
48+
},
49+
"Parameters": {
50+
"BootstrapVersion": {
51+
"Type": "AWS::SSM::Parameter::Value<String>",
52+
"Default": "/cdk-bootstrap/hnb659fds/version",
53+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
54+
}
55+
},
56+
"Rules": {
57+
"CheckBootstrapVersion": {
58+
"Assertions": [
59+
{
60+
"Assert": {
61+
"Fn::Not": [
62+
{
63+
"Fn::Contains": [
64+
[
65+
"1",
66+
"2",
67+
"3",
68+
"4",
69+
"5"
70+
],
71+
{
72+
"Ref": "BootstrapVersion"
73+
}
74+
]
75+
}
76+
]
77+
},
78+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
79+
}
80+
]
81+
}
82+
}
83+
}

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.cloudfront-with-webacl.js.snapshot/cdk.out

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.cloudfront-with-webacl.js.snapshot/integ.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.cloudfront-with-webacl.js.snapshot/integcloudfrontwithwebaclDefaultTestDeployAssert166CAFCF.assets.json

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.cloudfront-with-webacl.js.snapshot/integcloudfrontwithwebaclDefaultTestDeployAssert166CAFCF.template.json

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: packages/@aws-cdk-testing/framework-integ/test/aws-cloudfront/test/integ.cloudfront-with-webacl.js.snapshot/manifest.json

+119
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)