From ad2df53a48ecf441da3a4eed1e838cfd861bdccd Mon Sep 17 00:00:00 2001 From: Adam Hall Date: Mon, 31 Jul 2023 12:19:07 +0930 Subject: [PATCH 1/3] Add distribution and bucket as read only class properties so that they can be referenced by other resources --- packages/static-hosting/lib/static-hosting.ts | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/static-hosting/lib/static-hosting.ts b/packages/static-hosting/lib/static-hosting.ts index 9bd963f0..39cf94e3 100644 --- a/packages/static-hosting/lib/static-hosting.ts +++ b/packages/static-hosting/lib/static-hosting.ts @@ -1,6 +1,7 @@ import { Construct, CfnOutput, RemovalPolicy } from "@aws-cdk/core"; import { Bucket, + IBucket, BucketEncryption, BlockPublicAccess, BucketProps, @@ -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"; @@ -106,6 +108,9 @@ export interface ResponseHeaderMappings { } export class StaticHosting extends Construct { + public readonly distribution: IDistribution; + public readonly bucket: IBucket; + private staticFiles = [ "js", "css", @@ -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, @@ -168,7 +173,7 @@ export class StaticHosting extends Construct { new CfnOutput(this, "Bucket", { description: "BucketName", - value: bucket.bucketName, + value: this.bucket.bucketName, exportName: `${exportPrefix}BucketName`, }); @@ -176,7 +181,7 @@ export class StaticHosting extends Construct { comment: "Allow CloudFront to access S3", }); - bucket.grantRead(oai); + this.bucket.grantRead(oai); const publisherUser = props.createPublisherUser ? new User(this, "PublisherUser", { @@ -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", @@ -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 @@ -328,7 +333,7 @@ export class StaticHosting extends Construct { }; } - const distribution = new CloudFrontWebDistribution( + this.distribution = new CloudFrontWebDistribution( this, "BucketCdn", distributionProps @@ -346,7 +351,7 @@ 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( @@ -367,7 +372,7 @@ 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, @@ -452,7 +457,7 @@ export class StaticHosting extends Construct { "cloudfront:ListInvalidations", ], resources: [ - `arn:aws:cloudfront::*:distribution/${distribution.distributionId}`, + `arn:aws:cloudfront::*:distribution/${this.distribution.distributionId}`, ], }); @@ -467,12 +472,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`, }); @@ -483,7 +488,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, }); } From 9ff54c2eb8b4c5fa7c08b2975a3de67ce0cc6bab Mon Sep 17 00:00:00 2001 From: Adam Hall Date: Mon, 31 Jul 2023 13:46:23 +0930 Subject: [PATCH 2/3] Run format fixes --- packages/static-hosting/lib/static-hosting.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/static-hosting/lib/static-hosting.ts b/packages/static-hosting/lib/static-hosting.ts index 39cf94e3..7cef92a5 100644 --- a/packages/static-hosting/lib/static-hosting.ts +++ b/packages/static-hosting/lib/static-hosting.ts @@ -202,7 +202,7 @@ export class StaticHosting extends Construct { : undefined; if (publisherGroup) { - this.bucket.grantReadWrite(publisherGroup); + this.bucket.grantReadWrite(publisherGroup); new CfnOutput(this, "PublisherGroupName", { description: "PublisherGroup", @@ -351,7 +351,8 @@ export class StaticHosting extends Construct { }, }); - const cfnDistribution = this.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( @@ -372,7 +373,8 @@ export class StaticHosting extends Construct { * the cache behaviors */ if (props.responseHeadersPolicies) { - const cfnDistribution = this.distribution.node.defaultChild as CfnDistribution; + const cfnDistribution = this.distribution.node + .defaultChild as CfnDistribution; /** * If we prepend custom origin configs, From 63d125823b12321675e10353b76f5025a1f29dec Mon Sep 17 00:00:00 2001 From: Krishan Thisera Date: Tue, 15 Aug 2023 11:32:41 +0930 Subject: [PATCH 3/3] DO-1495: expose prerender proxy props for individiual construct --- packages/prerender-proxy/index.ts | 18 +++++++++++++++--- .../prerender-proxy/lib/handlers/prerender.ts | 1 - 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/prerender-proxy/index.ts b/packages/prerender-proxy/index.ts index 9181546a..fc41368b 100644 --- a/packages/prerender-proxy/index.ts +++ b/packages/prerender-proxy/index.ts @@ -1,7 +1,16 @@ -import { PrerenderLambda } from "./lib/prerender-lambda-construct"; -import { PrerenderFunction } from "./lib/prerender-construct"; +import { + PrerenderLambda, + PrerenderLambdaProps, +} from "./lib/prerender-lambda-construct"; +import { + PrerenderFunction, + PrerenderFunctionOptions, +} from "./lib/prerender-construct"; import { PrerenderCheckFunction } from "./lib/prerender-check-construct"; -import { ErrorResponseFunction } from "./lib/error-response-construct"; +import { + ErrorResponseFunction, + ErrorResponseFunctionOptions, +} from "./lib/error-response-construct"; import { CloudFrontCacheControl, CloudFrontCacheControlOptions, @@ -14,4 +23,7 @@ export { ErrorResponseFunction, CloudFrontCacheControl, CloudFrontCacheControlOptions, + ErrorResponseFunctionOptions, + PrerenderFunctionOptions, + PrerenderLambdaProps, }; diff --git a/packages/prerender-proxy/lib/handlers/prerender.ts b/packages/prerender-proxy/lib/handlers/prerender.ts index 27cdb06f..1f9af75a 100644 --- a/packages/prerender-proxy/lib/handlers/prerender.ts +++ b/packages/prerender-proxy/lib/handlers/prerender.ts @@ -49,4 +49,3 @@ export const handler = async ( return request; }; -1;