Skip to content

Commit a828dd6

Browse files
committed
feat: expose CloudFront distribution as property (#18)
- Add public readonly distribution property to Hosting construct - Expose CloudFront distribution from HostingInfrastructure - Update documentation with new property
1 parent 7794c19 commit a828dd6

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

lib/hosting.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class Hosting extends Construct {
5050
/**
5151
* The S3 Bucket used for hosting the content.
5252
*/
53-
public readonly hostingBucket: s3.Bucket;
53+
public readonly hostingBucket: s3.IBucket;
5454

5555
/**
5656
* The CloudFront Function used for URI manipulation.

lib/hosting_infrastructure.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ interface IConfigProps {
4747

4848

4949
export class HostingInfrastructure extends Construct {
50+
5051
public readonly hostingBucket: IBucket;
52+
public readonly distribution: cloudfront.Distribution;
5153

5254

5355

@@ -77,7 +79,7 @@ export class HostingInfrastructure extends Construct {
7779
},
7880
]);
7981
*/
80-
const hostingBucket = new s3.Bucket(this, "HostingBucket", {
82+
this.hostingBucket = new s3.Bucket(this, "HostingBucket", {
8183
versioned: false,
8284
...(s3Logs ? { serverAccessLogsBucket: s3Logs } : {}),
8385
enforceSSL: true,
@@ -90,9 +92,8 @@ export class HostingInfrastructure extends Construct {
9092
}),
9193
});
9294

93-
this.hostingBucket = hostingBucket;
9495

95-
const s3origin = new origins.S3Origin(hostingBucket);
96+
const s3origin = new origins.S3Origin(this.hostingBucket);
9697

9798
const myResponseHeadersPolicy = new cloudfront.ResponseHeadersPolicy(
9899
this,
@@ -246,7 +247,7 @@ export class HostingInfrastructure extends Construct {
246247

247248

248249

249-
const distribution = new cloudfront.Distribution(this, "Distribution", {
250+
this.distribution = new cloudfront.Distribution(this, "Distribution", {
250251
comment: "Static hosting - " + Aws.STACK_NAME,
251252
defaultRootObject: "index.html",
252253
httpVersion: cloudfront.HttpVersion.HTTP2_AND_3,
@@ -279,7 +280,7 @@ export class HostingInfrastructure extends Construct {
279280
: {}),
280281
});
281282

282-
NagSuppressions.addResourceSuppressions(distribution, [
283+
NagSuppressions.addResourceSuppressions(this.distribution, [
283284
{
284285
id: "AwsSolutions-CFR4",
285286
reason:
@@ -290,7 +291,7 @@ export class HostingInfrastructure extends Construct {
290291
//OAC is not implemented in CDK so tweaking is required:eplace OAI par OAC
291292
//https://github.com/aws/aws-cdk/issues/21771
292293

293-
const cfnDistribution = distribution.node.defaultChild as CfnDistribution;
294+
const cfnDistribution = this.distribution.node.defaultChild as CfnDistribution;
294295
cfnDistribution.addOverride(
295296
"Properties.DistributionConfig.Origins.0.S3OriginConfig.OriginAccessIdentity",
296297
""
@@ -300,7 +301,7 @@ export class HostingInfrastructure extends Construct {
300301
oac.getAtt("Id")
301302
);
302303

303-
const comS3PolicyOverride = hostingBucket.node.findChild("Policy").node
304+
const comS3PolicyOverride = this.hostingBucket.node.findChild("Policy").node
304305
.defaultChild as CfnBucketPolicy;
305306
const statement = comS3PolicyOverride.policyDocument.statements[1];
306307
if (statement["_principal"] && statement["_principal"].CanonicalUser) {
@@ -318,30 +319,30 @@ export class HostingInfrastructure extends Construct {
318319
service: "cloudfront",
319320
region: "",
320321
resource: "distribution",
321-
resourceName: distribution.distributionId,
322+
resourceName: this.distribution.distributionId,
322323
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
323324
}),
324325
},
325326
}
326327
);
327328

328-
const s3OriginNode = distribution.node
329+
const s3OriginNode = this.distribution.node
329330
.findAll()
330331
//.filter((child) => child.node.id === "S3Origin");
331332
.filter((child) => child.node.id === "S3Origin");
332333
s3OriginNode[0].node.tryRemoveChild("Resource");
333334
//End of tweaking for OAC is not implemented in CDK so tweaking is required
334335

335336
new CfnOutput(this, "DomainName", {
336-
value: "https://" + distribution.domainName,
337+
value: "https://" + this.distribution.domainName,
337338
});
338339

339340
const stackName = calculateMainStackName(params.hostingConfiguration);
340341

341342

342343
new ssm.StringParameter(this, 'SSMConnectionRegion', {
343344
parameterName: '/' + stackName + '/' + SSM_DOMAIN_STR,
344-
stringValue: distribution.domainName,
345+
stringValue: this.distribution.domainName,
345346
});
346347

347348

0 commit comments

Comments
 (0)