Skip to content

Commit 615ecd4

Browse files
skorfmannSam Goodwin
authored andcommitted
fix(cloudfront): pass viewerProtocolPolicy to the distribution's behaviors (#1932)
1 parent dbd2401 commit 615ecd4

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,13 +608,13 @@ export class CloudFrontWebDistribution extends cdk.Construct implements route53.
608608
if (defaultBehaviors.length !== 1) {
609609
throw new Error("There can only be one default behavior across all sources. [ One default behavior per distribution ].");
610610
}
611-
distributionConfig.defaultCacheBehavior = this.toBehavior(defaultBehaviors[0]);
611+
distributionConfig.defaultCacheBehavior = this.toBehavior(defaultBehaviors[0], props.viewerProtocolPolicy);
612612
const otherBehaviors: CfnDistribution.CacheBehaviorProperty[] = [];
613613
for (const behavior of behaviors.filter(b => !b.isDefaultBehavior)) {
614614
if (!behavior.pathPattern) {
615615
throw new Error("pathPattern is required for all non-default behaviors");
616616
}
617-
otherBehaviors.push(this.toBehavior(behavior) as CfnDistribution.CacheBehaviorProperty);
617+
otherBehaviors.push(this.toBehavior(behavior, props.viewerProtocolPolicy) as CfnDistribution.CacheBehaviorProperty);
618618
}
619619
distributionConfig.cacheBehaviors = otherBehaviors;
620620

packages/@aws-cdk/aws-cloudfront/test/test.basic.ts

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from '@aws-cdk/assert';
22
import s3 = require('@aws-cdk/aws-s3');
33
import cdk = require('@aws-cdk/cdk');
44
import { Test } from 'nodeunit';
5-
import { CloudFrontWebDistribution } from '../lib';
5+
import { CloudFrontWebDistribution, ViewerProtocolPolicy } from '../lib';
66

77
// tslint:disable:object-literal-key-quotes
88

@@ -246,4 +246,80 @@ export = {
246246
});
247247
test.done();
248248
},
249+
250+
'distribution with ViewerProtocolPolicy set to a non-default value'(test: Test) {
251+
const stack = new cdk.Stack();
252+
const sourceBucket = new s3.Bucket(stack, 'Bucket');
253+
254+
new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
255+
viewerProtocolPolicy: ViewerProtocolPolicy.AllowAll,
256+
originConfigs: [
257+
{
258+
s3OriginSource: {
259+
s3BucketSource: sourceBucket
260+
},
261+
behaviors: [
262+
{
263+
isDefaultBehavior: true,
264+
}
265+
]
266+
}
267+
]
268+
});
269+
270+
expect(stack).toMatch({
271+
"Resources": {
272+
"Bucket83908E77": {
273+
"Type": "AWS::S3::Bucket",
274+
"DeletionPolicy": "Retain",
275+
},
276+
"AnAmazingWebsiteProbablyCFDistribution47E3983B": {
277+
"Type": "AWS::CloudFront::Distribution",
278+
"Properties": {
279+
"DistributionConfig": {
280+
"DefaultRootObject": "index.html",
281+
"Origins": [
282+
{
283+
"DomainName": {
284+
"Fn::GetAtt": [
285+
"Bucket83908E77",
286+
"DomainName"
287+
]
288+
},
289+
"Id": "origin1",
290+
"S3OriginConfig": {}
291+
}
292+
],
293+
"ViewerCertificate": {
294+
"CloudFrontDefaultCertificate": true
295+
},
296+
"PriceClass": "PriceClass_100",
297+
"DefaultCacheBehavior": {
298+
"AllowedMethods": [
299+
"GET",
300+
"HEAD"
301+
],
302+
"CachedMethods": [
303+
"GET",
304+
"HEAD"
305+
],
306+
"TargetOriginId": "origin1",
307+
"ViewerProtocolPolicy": "allow-all",
308+
"ForwardedValues": {
309+
"QueryString": false,
310+
"Cookies": { "Forward": "none" }
311+
}
312+
},
313+
"Enabled": true,
314+
"IPV6Enabled": true,
315+
"HttpVersion": "http2",
316+
"CacheBehaviors": []
317+
}
318+
}
319+
}
320+
}
321+
});
322+
test.done();
323+
},
324+
249325
};

0 commit comments

Comments
 (0)