All files / iac/lib/stacks staticWebsiteStack.ts

94.28% Statements 33/35
65.38% Branches 17/26
100% Functions 2/2
94.28% Lines 33/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236                                                                                1x   1x       1x   1x           1x 1x         1x 1x 1x 1x 1x                     1x 1x           1x                                     1x                                         1x               1x   1x   1x             1x             1x   1x                                                           1x             1x 1x   1x         1x       1x                   1x       1x           1x     1x           1x              
import { join } from "node:path";
import { CfnOutput, Duration, RemovalPolicy } from "aws-cdk-lib";
import {
	Certificate,
	type ICertificate,
} from "aws-cdk-lib/aws-certificatemanager";
import {
	Distribution,
	type ErrorResponse,
	FunctionCode,
	FunctionEventType,
	Function as LambdaFunction,
	OriginAccessIdentity,
	ViewerProtocolPolicy,
} from "aws-cdk-lib/aws-cloudfront";
import { S3BucketOrigin } from "aws-cdk-lib/aws-cloudfront-origins";
import { ARecord, HostedZone, RecordTarget } from "aws-cdk-lib/aws-route53";
import { CloudFrontTarget } from "aws-cdk-lib/aws-route53-targets";
import {
	BlockPublicAccess,
	Bucket,
	BucketAccessControl,
	BucketEncryption,
	HttpMethods,
} from "aws-cdk-lib/aws-s3";
import { BucketDeployment, Source } from "aws-cdk-lib/aws-s3-deployment";
import type { Construct } from "constructs";
import {
	CustomStack,
	type CustomStackProps,
	type DomainConfig,
} from "./customStack";
 
export interface StaticWebsiteStackProps extends CustomStackProps {
	domains: DomainConfig[];
	distFolderPath: string;
}
 
export class StaticWebsiteStack extends CustomStack {
	constructor(scope: Construct, props: StaticWebsiteStackProps) {
		super(scope, props);
 
		Iif (!props.domains || props.domains.length === 0) {
			throw new Error("At least one domain configuration is required");
		}
 
		const primaryDomain = props.domains[0];
		const primaryFullDomainName =
			primaryDomain.subdomain && primaryDomain.subdomain.length > 0
				? [primaryDomain.subdomain, primaryDomain.domainName]
						.join(".")
						.toLowerCase()
				: primaryDomain.domainName.toLowerCase();
 
		const allDomainNames = props.domains.map((domain) =>
			domain.subdomain && domain.subdomain.length > 0
				? `${domain.subdomain}.${domain.domainName}`.toLowerCase()
				: domain.domainName.toLowerCase(),
		);
 
		const certificateMap = new Map<string, ICertificate>();
		for (const domain of props.domains) {
			Eif (!certificateMap.has(domain.certificateId)) {
				const certificateArn = `arn:aws:acm:us-east-1:${props.env?.account}:certificate/${domain.certificateId}`;
				certificateMap.set(
					domain.certificateId,
					Certificate.fromCertificateArn(
						this,
						`certificate-${domain.certificateId}`,
						certificateArn,
					),
				);
			}
		}
 
		const primaryCertificate = certificateMap.get(primaryDomain.certificateId);
		Iif (!primaryCertificate) {
			throw new Error(
				`Certificate not found for ${primaryDomain.certificateId}`,
			);
		}
 
		const loggingBucket = new Bucket(this, "logging-bucket", {
			accessControl: BucketAccessControl.LOG_DELIVERY_WRITE,
			publicReadAccess: false,
			versioned: false,
			removalPolicy: RemovalPolicy.DESTROY,
			bucketName: `${primaryFullDomainName}-logs`,
			autoDeleteObjects: true,
			blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
			encryption: BucketEncryption.S3_MANAGED,
			enforceSSL: true,
			lifecycleRules: [
				{
					id: "DeleteOldLogs",
					expiration: Duration.days(90),
					enabled: true,
				},
			],
		});
 
		const bucketWebsite = new Bucket(this, "static-website-bucket", {
			accessControl: BucketAccessControl.PRIVATE,
			publicReadAccess: false,
			versioned: false,
			removalPolicy: RemovalPolicy.DESTROY,
			bucketName: primaryFullDomainName,
			autoDeleteObjects: true,
			blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
			encryption: BucketEncryption.S3_MANAGED,
			cors: [
				{
					allowedMethods: [HttpMethods.GET, HttpMethods.HEAD],
					allowedOrigins: ["*"],
					allowedHeaders: ["*"],
				},
			],
			enforceSSL: true,
			serverAccessLogsBucket: loggingBucket,
			serverAccessLogsPrefix: "s3-access-logs/",
		});
 
		const originAccessIdentity = new OriginAccessIdentity(
			this,
			"originAccessIdentity",
			{
				comment: `Setup access from CloudFront to the bucket ${primaryFullDomainName} (read)`,
			},
		);
 
		bucketWebsite.grantRead(originAccessIdentity);
 
		const errorResponses: ErrorResponse[] = [];
 
		const errorResponse403: ErrorResponse = {
			httpStatus: 403,
			responseHttpStatus: 200,
			responsePagePath: "/index.html",
			ttl: Duration.seconds(10),
		};
 
		const errorResponse404: ErrorResponse = {
			httpStatus: 404,
			responseHttpStatus: 200,
			responsePagePath: "/index.html",
			ttl: Duration.seconds(10),
		};
 
		errorResponses.push(errorResponse403, errorResponse404);
 
		const distribution = new Distribution(this, "distribution", {
			domainNames: allDomainNames,
			defaultBehavior: {
				origin: S3BucketOrigin.withOriginAccessIdentity(bucketWebsite, {
					originAccessIdentity: originAccessIdentity,
				}),
				viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
				functionAssociations: [
					{
						eventType: FunctionEventType.VIEWER_REQUEST,
						function: new LambdaFunction(
							this,
							`${primaryFullDomainName}-url-rewrite`.toLowerCase(),
							{
								code: FunctionCode.fromFile({
									filePath: join(__dirname, "cloudfront-url-rewrite.js"),
								}),
							},
						),
					},
				],
			},
			defaultRootObject: "index.html",
			certificate: primaryCertificate,
			errorResponses: errorResponses,
			enableLogging: true,
			logBucket: loggingBucket,
			logFilePrefix: "cloudfront-logs/",
		});
 
		new BucketDeployment(this, "deploy-static-website", {
			sources: [Source.asset(props.distFolderPath)],
			destinationBucket: bucketWebsite,
			distribution,
			distributionPaths: ["/*"],
		});
 
		const aliasRecords: ARecord[] = [];
		for (const [index, domainConfig] of props.domains.entries()) {
			const fullDomainName =
				domainConfig.subdomain && domainConfig.subdomain.length > 0
					? `${domainConfig.subdomain}.${domainConfig.domainName}`.toLowerCase()
					: domainConfig.domainName.toLowerCase();
 
			const zoneLogicalId =
				index === 0
					? "publicHostedZone-0"
					: `hostedZone-${fullDomainName.replace(/[.-]/g, "")}`;
 
			const zoneFromAttributes = HostedZone.fromHostedZoneAttributes(
				this,
				zoneLogicalId,
				{
					zoneName: domainConfig.domainName,
					hostedZoneId: domainConfig.hostedZoneId,
				},
			);
 
			const recordLogicalId =
				index === 0
					? "webDomainRecord-0"
					: `webDomainRecord-${fullDomainName.replace(/[.-]/g, "")}`;
 
			const aliasRecord = new ARecord(this, recordLogicalId, {
				zone: zoneFromAttributes,
				recordName: fullDomainName,
				target: RecordTarget.fromAlias(new CloudFrontTarget(distribution)),
			});
 
			aliasRecords.push(aliasRecord);
		}
 
		new CfnOutput(this, "CloudFrontDistributionDomainName", {
			value: distribution.distributionDomainName,
			description: "CloudFront distribution domain",
			exportName: `${this.getCloudFormationRepoName()}-${props.envName}-CdnDomainName`,
		});
 
		new CfnOutput(this, "DnsRecordName", {
			value: aliasRecords[0].domainName || allDomainNames[0],
			description: "The DNS record name (primary)",
			exportName: `${this.getCloudFormationRepoName()}-${props.envName}-AliasRecord`,
		});
	}
}