Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Cloudfront Distribution (CloudFrontWebDistribution will be depricated) #685

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions cdk/lib/constructs/frontend.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Construct } from "constructs";
import { CfnOutput, RemovalPolicy, Stack } from "aws-cdk-lib";
import { CfnOutput, Duration, RemovalPolicy, Stack } from "aws-cdk-lib";
import {
BlockPublicAccess,
Bucket,
BucketEncryption,
IBucket,
} from "aws-cdk-lib/aws-s3";
import {
CloudFrontWebDistribution,
OriginAccessIdentity,
CachePolicy,
Distribution,
ViewerProtocolPolicy,
} from "aws-cdk-lib/aws-cloudfront";
import { S3BucketOrigin } from "aws-cdk-lib/aws-cloudfront-origins";
import { NodejsBuild } from "deploy-time-build";
import { Auth } from "./auth";
import { Idp } from "../utils/identity-provider";
Expand All @@ -23,7 +25,7 @@ export interface FrontendProps {
}

export class Frontend extends Construct {
readonly cloudFrontWebDistribution: CloudFrontWebDistribution;
readonly cloudFrontWebDistribution: Distribution;
readonly assetBucket: Bucket;
constructor(scope: Construct, id: string, props: FrontendProps) {
super(scope, id);
Expand All @@ -38,46 +40,33 @@ export class Frontend extends Construct {
serverAccessLogsPrefix: "AssetBucket",
});

const originAccessIdentity = new OriginAccessIdentity(
this,
"OriginAccessIdentity"
);
const distribution = new CloudFrontWebDistribution(this, "Distribution", {
originConfigs: [
{
s3OriginSource: {
s3BucketSource: assetBucket,
originAccessIdentity,
},
behaviors: [
{
isDefaultBehavior: true,
},
],
},
],
errorConfigurations: [
const distribution = new Distribution(this, "Distribution", {
defaultRootObject: "index.html",
defaultBehavior: {
origin: S3BucketOrigin.withOriginAccessControl(assetBucket),
viewerProtocolPolicy: ViewerProtocolPolicy.HTTPS_ONLY,
cachePolicy: CachePolicy.CACHING_OPTIMIZED,
},
errorResponses: [
{
errorCode: 404,
errorCachingMinTtl: 0,
responseCode: 200,
httpStatus: 404,
ttl: Duration.seconds(0),
responseHttpStatus: 200,
responsePagePath: "/",
},
{
errorCode: 403,
errorCachingMinTtl: 0,
responseCode: 200,
httpStatus: 403,
ttl: Duration.seconds(0),
responseHttpStatus: 200,
responsePagePath: "/",
},
],
...(!this.shouldSkipAccessLogging() && {
loggingConfig: {
bucket: props.accessLogBucket,
prefix: "Frontend/",
},
logBucket: props.accessLogBucket,
logFilePrefix: "Frontend/",
}),
webACLId: props.webAclId,
enableIpV6: props.enableIpV6,
webAclId: props.webAclId,
enableIpv6: props.enableIpV6,
});

NagSuppressions.addResourceSuppressions(distribution, [
Expand Down
Loading