Skip to content

Commit

Permalink
Merge pull request #1060 from aligent/feature/Add-Class-Properties
Browse files Browse the repository at this point in the history
Add class properties
  • Loading branch information
AdamJHall authored Jul 31, 2023
2 parents 633c019 + 9ff54c2 commit 0178fd5
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions packages/static-hosting/lib/static-hosting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Construct, CfnOutput, RemovalPolicy } from "@aws-cdk/core";
import {
Bucket,
IBucket,
BucketEncryption,
BlockPublicAccess,
BucketProps,
Expand All @@ -19,6 +20,7 @@ import {
CfnDistribution,
ResponseHeadersPolicy,
HttpVersion,
IDistribution,
} from "@aws-cdk/aws-cloudfront";
import { HostedZone, ARecord } from "@aws-cdk/aws-route53";
import { User, Group, Policy, PolicyStatement, Effect } from "@aws-cdk/aws-iam";
Expand Down Expand Up @@ -106,6 +108,9 @@ export interface ResponseHeaderMappings {
}

export class StaticHosting extends Construct {
public readonly distribution: IDistribution;
public readonly bucket: IBucket;

private staticFiles = [
"js",
"css",
Expand Down Expand Up @@ -157,7 +162,7 @@ export class StaticHosting extends Construct {
});
}

const bucket = new Bucket(this, "ContentBucket", {
this.bucket = new Bucket(this, "ContentBucket", {
bucketName: siteName,
encryption: BucketEncryption.S3_MANAGED,
blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
Expand All @@ -168,15 +173,15 @@ export class StaticHosting extends Construct {

new CfnOutput(this, "Bucket", {
description: "BucketName",
value: bucket.bucketName,
value: this.bucket.bucketName,
exportName: `${exportPrefix}BucketName`,
});

const oai = new OriginAccessIdentity(this, "OriginAccessIdentity", {
comment: "Allow CloudFront to access S3",
});

bucket.grantRead(oai);
this.bucket.grantRead(oai);

const publisherUser = props.createPublisherUser
? new User(this, "PublisherUser", {
Expand All @@ -197,7 +202,7 @@ export class StaticHosting extends Construct {
: undefined;

if (publisherGroup) {
bucket.grantReadWrite(publisherGroup);
this.bucket.grantReadWrite(publisherGroup);

new CfnOutput(this, "PublisherGroupName", {
description: "PublisherGroup",
Expand Down Expand Up @@ -256,7 +261,7 @@ export class StaticHosting extends Construct {
// Create default origin
originConfigs.push({
s3OriginSource: {
s3BucketSource: bucket,
s3BucketSource: this.bucket,
originAccessIdentity: oai,
},
// if behaviors have been passed via props use them instead
Expand Down Expand Up @@ -328,7 +333,7 @@ export class StaticHosting extends Construct {
};
}

const distribution = new CloudFrontWebDistribution(
this.distribution = new CloudFrontWebDistribution(
this,
"BucketCdn",
distributionProps
Expand All @@ -346,7 +351,8 @@ export class StaticHosting extends Construct {
},
});

const cfnDistribution = distribution.node.defaultChild as CfnDistribution;
const cfnDistribution = this.distribution.node
.defaultChild as CfnDistribution;
// In the current version of CDK there's no nice way to do this...
// Instead just override the CloudFormation property directly
cfnDistribution.addOverride(
Expand All @@ -367,7 +373,8 @@ export class StaticHosting extends Construct {
* the cache behaviors
*/
if (props.responseHeadersPolicies) {
const cfnDistribution = distribution.node.defaultChild as CfnDistribution;
const cfnDistribution = this.distribution.node
.defaultChild as CfnDistribution;

/**
* If we prepend custom origin configs,
Expand Down Expand Up @@ -452,7 +459,7 @@ export class StaticHosting extends Construct {
"cloudfront:ListInvalidations",
],
resources: [
`arn:aws:cloudfront::*:distribution/${distribution.distributionId}`,
`arn:aws:cloudfront::*:distribution/${this.distribution.distributionId}`,
],
});

Expand All @@ -467,12 +474,12 @@ export class StaticHosting extends Construct {
}
new CfnOutput(this, "DistributionId", {
description: "DistributionId",
value: distribution.distributionId,
value: this.distribution.distributionId,
exportName: `${exportPrefix}DistributionID`,
});
new CfnOutput(this, "DistributionDomainName", {
description: "DistributionDomainName",
value: distribution.distributionDomainName,
value: this.distribution.distributionDomainName,
exportName: `${exportPrefix}DistributionName`,
});

Expand All @@ -483,7 +490,7 @@ export class StaticHosting extends Construct {

new ARecord(this, "SiteAliasRecord", {
recordName: siteName,
target: RecordTarget.fromAlias(new CloudFrontTarget(distribution)),
target: RecordTarget.fromAlias(new CloudFrontTarget(this.distribution)),
zone: zone,
});
}
Expand Down

0 comments on commit 0178fd5

Please sign in to comment.