diff --git a/packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts b/packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts index 40a01ad6b653f..07f8d6d6cd205 100644 --- a/packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts +++ b/packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts @@ -608,13 +608,13 @@ export class CloudFrontWebDistribution extends cdk.Construct implements route53. if (defaultBehaviors.length !== 1) { throw new Error("There can only be one default behavior across all sources. [ One default behavior per distribution ]."); } - distributionConfig.defaultCacheBehavior = this.toBehavior(defaultBehaviors[0]); + distributionConfig.defaultCacheBehavior = this.toBehavior(defaultBehaviors[0], props.viewerProtocolPolicy); const otherBehaviors: CfnDistribution.CacheBehaviorProperty[] = []; for (const behavior of behaviors.filter(b => !b.isDefaultBehavior)) { if (!behavior.pathPattern) { throw new Error("pathPattern is required for all non-default behaviors"); } - otherBehaviors.push(this.toBehavior(behavior) as CfnDistribution.CacheBehaviorProperty); + otherBehaviors.push(this.toBehavior(behavior, props.viewerProtocolPolicy) as CfnDistribution.CacheBehaviorProperty); } distributionConfig.cacheBehaviors = otherBehaviors; diff --git a/packages/@aws-cdk/aws-cloudfront/test/test.basic.ts b/packages/@aws-cdk/aws-cloudfront/test/test.basic.ts index 2c10214c8a349..5109ae9284cb6 100644 --- a/packages/@aws-cdk/aws-cloudfront/test/test.basic.ts +++ b/packages/@aws-cdk/aws-cloudfront/test/test.basic.ts @@ -2,7 +2,7 @@ import { expect } from '@aws-cdk/assert'; import s3 = require('@aws-cdk/aws-s3'); import cdk = require('@aws-cdk/cdk'); import { Test } from 'nodeunit'; -import { CloudFrontWebDistribution } from '../lib'; +import { CloudFrontWebDistribution, ViewerProtocolPolicy } from '../lib'; // tslint:disable:object-literal-key-quotes @@ -246,4 +246,80 @@ export = { }); test.done(); }, + + 'distribution with ViewerProtocolPolicy set to a non-default value'(test: Test) { + const stack = new cdk.Stack(); + const sourceBucket = new s3.Bucket(stack, 'Bucket'); + + new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', { + viewerProtocolPolicy: ViewerProtocolPolicy.AllowAll, + originConfigs: [ + { + s3OriginSource: { + s3BucketSource: sourceBucket + }, + behaviors: [ + { + isDefaultBehavior: true, + } + ] + } + ] + }); + + expect(stack).toMatch({ + "Resources": { + "Bucket83908E77": { + "Type": "AWS::S3::Bucket", + "DeletionPolicy": "Retain", + }, + "AnAmazingWebsiteProbablyCFDistribution47E3983B": { + "Type": "AWS::CloudFront::Distribution", + "Properties": { + "DistributionConfig": { + "DefaultRootObject": "index.html", + "Origins": [ + { + "DomainName": { + "Fn::GetAtt": [ + "Bucket83908E77", + "DomainName" + ] + }, + "Id": "origin1", + "S3OriginConfig": {} + } + ], + "ViewerCertificate": { + "CloudFrontDefaultCertificate": true + }, + "PriceClass": "PriceClass_100", + "DefaultCacheBehavior": { + "AllowedMethods": [ + "GET", + "HEAD" + ], + "CachedMethods": [ + "GET", + "HEAD" + ], + "TargetOriginId": "origin1", + "ViewerProtocolPolicy": "allow-all", + "ForwardedValues": { + "QueryString": false, + "Cookies": { "Forward": "none" } + } + }, + "Enabled": true, + "IPV6Enabled": true, + "HttpVersion": "http2", + "CacheBehaviors": [] + } + } + } + } + }); + test.done(); + }, + };