Skip to content

Commit a09afc4

Browse files
ZeldoKavirarix0rrr
authored andcommitted
fix(aws-cloudfront): properly support loggingConfig (#809)
feat(aws-s3): Add support for domainName on BucketRef instead of just Bucket LoggingConfiguration now requires bucket instead of it being optional. There is no point to including a LoggingConfiguration without a bucket (no-breaking). Fixes #721
1 parent 86a55a9 commit a09afc4

6 files changed

+285
-7
lines changed

Diff for: packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts

+27-7
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,28 @@ export enum SecurityPolicyProtocol {
101101
}
102102

103103
/**
104-
* CloudFront supports logging of incoming requests and can log details to a given S3 Bucket.
105-
*
106-
* If you wish to configure logging you can configure details about it.
107-
*
108-
* @default bucket: if you do not pass a bucket for logging - we'll create one
109-
* @default includeCookies: false by default
110-
* @default prefix: no prefix is set by default.
104+
* Logging configuration for incoming requests
111105
*/
112106
export interface LoggingConfiguration {
107+
/**
108+
* Bucket to log requests to
109+
*
110+
* @default A logging bucket is automatically created
111+
*/
113112
readonly bucket?: s3.BucketRef,
113+
114+
/**
115+
* Whether to include the cookies in the logs
116+
*
117+
* @default false
118+
*/
114119
readonly includeCookies?: boolean,
120+
121+
/**
122+
* Where in the bucket to store logs
123+
*
124+
* @default No prefix
125+
*/
115126
readonly prefix?: string
116127
}
117128

@@ -622,6 +633,15 @@ export class CloudFrontWebDistribution extends cdk.Construct {
622633
};
623634
}
624635

636+
if (props.loggingConfig) {
637+
this.loggingBucket = props.loggingConfig.bucket || new s3.Bucket(this, `LoggingBucket`);
638+
distributionConfig.logging = {
639+
bucket: this.loggingBucket.domainName,
640+
includeCookies: props.loggingConfig.includeCookies || false,
641+
prefix: props.loggingConfig.prefix
642+
};
643+
}
644+
625645
const distribution = new cloudformation.DistributionResource(this, 'CFDistribution', {distributionConfig});
626646
this.domainName = distribution.distributionDomainName;
627647

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
{
2+
"Resources": {
3+
"Bucket83908E77": {
4+
"Type": "AWS::S3::Bucket"
5+
},
6+
"AnAmazingWebsiteProbablyCFDistribution47E3983B": {
7+
"Type": "AWS::CloudFront::Distribution",
8+
"Properties": {
9+
"DistributionConfig": {
10+
"CacheBehaviors": [],
11+
"DefaultCacheBehavior": {
12+
"AllowedMethods": [
13+
"GET",
14+
"HEAD"
15+
],
16+
"CachedMethods": [
17+
"GET",
18+
"HEAD"
19+
],
20+
"ForwardedValues": {
21+
"Cookies": {
22+
"Forward": "none"
23+
},
24+
"QueryString": false
25+
},
26+
"TargetOriginId": "origin1",
27+
"ViewerProtocolPolicy": "redirect-to-https"
28+
},
29+
"DefaultRootObject": "index.html",
30+
"Enabled": true,
31+
"HttpVersion": "http2",
32+
"IPV6Enabled": true,
33+
"Logging": {
34+
"Bucket": {
35+
"Fn::GetAtt": [
36+
"Bucket83908E77",
37+
"DomainName"
38+
]
39+
},
40+
"IncludeCookies": true,
41+
"Prefix": "test-prefix"
42+
},
43+
"Origins": [
44+
{
45+
"CustomOriginConfig": {
46+
"HTTPPort": 80,
47+
"HTTPSPort": 443,
48+
"OriginKeepaliveTimeout": 5,
49+
"OriginProtocolPolicy": "https-only",
50+
"OriginReadTimeout": 30,
51+
"OriginSSLProtocols": [
52+
"TLSv1.2"
53+
]
54+
},
55+
"DomainName": "brelandm.a2z.com",
56+
"Id": "origin1",
57+
"OriginCustomHeaders": [
58+
{
59+
"HeaderName": "X-Custom-Header",
60+
"HeaderValue": "somevalue"
61+
}
62+
]
63+
}
64+
],
65+
"PriceClass": "PriceClass_100",
66+
"ViewerCertificate": {
67+
"CloudFrontDefaultCertificate": true
68+
}
69+
}
70+
}
71+
},
72+
"AnAmazingWebsiteProbably2LoggingBucket222F7CE9": {
73+
"Type": "AWS::S3::Bucket"
74+
},
75+
"AnAmazingWebsiteProbably2CFDistribution7C1CCD12": {
76+
"Type": "AWS::CloudFront::Distribution",
77+
"Properties": {
78+
"DistributionConfig": {
79+
"CacheBehaviors": [],
80+
"DefaultCacheBehavior": {
81+
"AllowedMethods": [
82+
"GET",
83+
"HEAD"
84+
],
85+
"CachedMethods": [
86+
"GET",
87+
"HEAD"
88+
],
89+
"ForwardedValues": {
90+
"Cookies": {
91+
"Forward": "none"
92+
},
93+
"QueryString": false
94+
},
95+
"TargetOriginId": "origin1",
96+
"ViewerProtocolPolicy": "redirect-to-https"
97+
},
98+
"DefaultRootObject": "index.html",
99+
"Enabled": true,
100+
"HttpVersion": "http2",
101+
"IPV6Enabled": true,
102+
"Logging": {
103+
"Bucket": {
104+
"Fn::GetAtt": [
105+
"AnAmazingWebsiteProbably2LoggingBucket222F7CE9",
106+
"DomainName"
107+
]
108+
},
109+
"IncludeCookies": false
110+
},
111+
"Origins": [
112+
{
113+
"CustomOriginConfig": {
114+
"HTTPPort": 80,
115+
"HTTPSPort": 443,
116+
"OriginKeepaliveTimeout": 5,
117+
"OriginProtocolPolicy": "https-only",
118+
"OriginReadTimeout": 30,
119+
"OriginSSLProtocols": [
120+
"TLSv1.2"
121+
]
122+
},
123+
"DomainName": "brelandm.a2z.com",
124+
"Id": "origin1",
125+
"OriginCustomHeaders": [
126+
{
127+
"HeaderName": "X-Custom-Header",
128+
"HeaderValue": "somevalue"
129+
}
130+
]
131+
}
132+
],
133+
"PriceClass": "PriceClass_100",
134+
"ViewerCertificate": {
135+
"CloudFrontDefaultCertificate": true
136+
}
137+
}
138+
}
139+
}
140+
}
141+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import s3 = require('@aws-cdk/aws-s3');
2+
import cdk = require('@aws-cdk/cdk');
3+
import cloudfront = require('../lib');
4+
5+
const app = new cdk.App(process.argv);
6+
7+
const stack = new cdk.Stack(app, 'aws-cdk-cloudfront-custom');
8+
9+
const loggingBucket = new s3.Bucket(stack, 'Bucket');
10+
11+
new cloudfront.CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
12+
originConfigs: [
13+
{
14+
originHeaders: {
15+
"X-Custom-Header": "somevalue",
16+
},
17+
customOriginSource: {
18+
domainName: "brelandm.a2z.com",
19+
},
20+
behaviors: [
21+
{
22+
isDefaultBehavior: true,
23+
}
24+
]
25+
}
26+
],
27+
loggingConfig: {
28+
bucket: loggingBucket,
29+
includeCookies: true,
30+
prefix: 'test-prefix'
31+
}
32+
});
33+
34+
new cloudfront.CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably2', {
35+
originConfigs: [
36+
{
37+
originHeaders: {
38+
"X-Custom-Header": "somevalue",
39+
},
40+
customOriginSource: {
41+
domainName: "brelandm.a2z.com",
42+
},
43+
behaviors: [
44+
{
45+
isDefaultBehavior: true,
46+
}
47+
]
48+
}
49+
],
50+
loggingConfig: {}
51+
});
52+
53+
process.stdout.write(app.run());

Diff for: packages/@aws-cdk/aws-s3/lib/bucket.ts

+18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export interface BucketRefProps {
3030
* policy, won't work.
3131
*/
3232
bucketName?: string;
33+
34+
/**
35+
* The domain name of the bucket.
36+
*
37+
* @default Inferred from bucket name
38+
*/
39+
bucketDomainName?: string;
3340
}
3441

3542
/**
@@ -72,6 +79,11 @@ export abstract class BucketRef extends cdk.Construct {
7279
*/
7380
public abstract readonly bucketName: string;
7481

82+
/**
83+
* The domain of the bucket.
84+
*/
85+
public abstract readonly domainName: string;
86+
7587
/**
7688
* Optional KMS encryption key associated with this bucket.
7789
*/
@@ -701,6 +713,7 @@ export interface NotificationKeyFilter {
701713
class ImportedBucketRef extends BucketRef {
702714
public readonly bucketArn: string;
703715
public readonly bucketName: string;
716+
public readonly domainName: string;
704717
public readonly encryptionKey?: kms.EncryptionKey;
705718

706719
protected policy?: BucketPolicy;
@@ -716,7 +729,12 @@ class ImportedBucketRef extends BucketRef {
716729

717730
this.bucketArn = parseBucketArn(props);
718731
this.bucketName = bucketName;
732+
this.domainName = props.bucketDomainName || this.generateDomainName();
719733
this.autoCreatePolicy = false;
720734
this.policy = undefined;
721735
}
736+
737+
private generateDomainName() {
738+
return `${this.bucketName}.s3.amazonaws.com`;
739+
}
722740
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"Resources": {
3+
"MyBucketF68F3FF0": {
4+
"Type": "AWS::S3::Bucket"
5+
}
6+
},
7+
"Outputs": {
8+
"RealBucketDomain": {
9+
"Value": {
10+
"Fn::GetAtt":["MyBucketF68F3FF0","DomainName"]
11+
},
12+
"Export": {
13+
"Name": "aws-cdk-s3-urls:RealBucketDomain"
14+
}
15+
},
16+
"ImportedBucketDomain": {
17+
"Value": "my-bucket-test.s3.amazonaws.com",
18+
"Export": {
19+
"Name": "aws-cdk-s3-urls:ImportedBucketDomain"
20+
}
21+
}
22+
}
23+
}
24+
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import cdk = require('@aws-cdk/cdk');
2+
import s3 = require('../lib');
3+
4+
class TestStack extends cdk.Stack {
5+
constructor(parent: cdk.App, id: string) {
6+
super(parent, id);
7+
8+
/// !show
9+
const bucket = new s3.Bucket(this, 'MyBucket');
10+
const bucket2 = s3.Bucket.import(this, "MyBucket2", {
11+
bucketArn: "arn:aws:s3:::my-bucket-test"
12+
});
13+
14+
new cdk.Output(this, 'RealBucketDomain', { value: bucket.domainName });
15+
new cdk.Output(this, 'ImportedBucketDomain', { value: bucket2.domainName });
16+
/// !hide
17+
}
18+
}
19+
20+
const app = new cdk.App(process.argv);
21+
new TestStack(app, 'aws-cdk-s3-urls');
22+
process.stdout.write(app.run());

0 commit comments

Comments
 (0)