Skip to content

Commit

Permalink
feat(aws-cloudfront): add enabled to web distribution (#15433)
Browse files Browse the repository at this point in the history
Allows cloudfront web distribution to be enabled or disabled. This prop is available in `Distribution` but not `CloudFrontWebDistribution`


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
iRoachie committed Jul 29, 2021
1 parent 3888773 commit 7ad9348
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-cloudfront/lib/web-distribution.ts
Expand Up @@ -557,6 +557,13 @@ export interface CloudFrontWebDistributionProps {
*/
readonly comment?: string;

/**
* Enable or disable the distribution.
*
* @default true
*/
readonly enabled?: boolean;

/**
* The default object to serve.
*
Expand Down Expand Up @@ -785,7 +792,7 @@ export class CloudFrontWebDistribution extends cdk.Resource implements IDistribu

let distributionConfig: CfnDistribution.DistributionConfigProperty = {
comment: trimmedComment,
enabled: true,
enabled: props.enabled ?? true,
defaultRootObject: props.defaultRootObject ?? 'index.html',
httpVersion: props.httpVersion || HttpVersion.HTTP2,
priceClass: props.priceClass || PriceClass.PRICE_CLASS_100,
Expand Down
78 changes: 78 additions & 0 deletions packages/@aws-cdk/aws-cloudfront/test/web-distribution.test.ts
Expand Up @@ -201,6 +201,84 @@ nodeunitShim({
test.done();
},

'can disable distribution'(test: Test) {
const stack = new cdk.Stack();
const sourceBucket = new s3.Bucket(stack, 'Bucket');

new CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
enabled: false,
originConfigs: [
{
s3OriginSource: {
s3BucketSource: sourceBucket,
},
behaviors: [
{
isDefaultBehavior: true,
},
],
},
],
});

expect(stack).toMatch({
'Resources': {
'Bucket83908E77': {
'Type': 'AWS::S3::Bucket',
'DeletionPolicy': 'Retain',
'UpdateReplacePolicy': 'Retain',
},
'AnAmazingWebsiteProbablyCFDistribution47E3983B': {
'Type': 'AWS::CloudFront::Distribution',
'Properties': {
'DistributionConfig': {
'DefaultRootObject': 'index.html',
'Origins': [
{
'ConnectionAttempts': 3,
'ConnectionTimeout': 10,
'DomainName': {
'Fn::GetAtt': [
'Bucket83908E77',
'RegionalDomainName',
],
},
'Id': 'origin1',
'S3OriginConfig': {},
},
],
'ViewerCertificate': {
'CloudFrontDefaultCertificate': true,
},
'PriceClass': 'PriceClass_100',
'DefaultCacheBehavior': {
'AllowedMethods': [
'GET',
'HEAD',
],
'CachedMethods': [
'GET',
'HEAD',
],
'TargetOriginId': 'origin1',
'ViewerProtocolPolicy': 'redirect-to-https',
'ForwardedValues': {
'QueryString': false,
'Cookies': { 'Forward': 'none' },
},
'Compress': true,
},
'Enabled': false,
'IPV6Enabled': true,
'HttpVersion': 'http2',
},
},
},
},
});
test.done();
},

'ensure long comments will not break the distribution'(test: Test) {
const stack = new cdk.Stack();
const sourceBucket = new s3.Bucket(stack, 'Bucket');
Expand Down

0 comments on commit 7ad9348

Please sign in to comment.