This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
frontend.ts
87 lines (81 loc) · 2.62 KB
/
frontend.ts
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
import { Construct } from 'constructs';
import { RemovalPolicy, Stack } from 'aws-cdk-lib';
import { BlockPublicAccess, Bucket, BucketEncryption, IBucket } from 'aws-cdk-lib/aws-s3';
import { CloudFrontWebDistribution, OriginAccessIdentity } from 'aws-cdk-lib/aws-cloudfront';
import { NodejsBuild } from 'deploy-time-build';
import { Auth } from './auth';
import { IHttpApi } from 'aws-cdk-lib/aws-apigatewayv2';
export interface FrontendProps {
readonly backendApi: IHttpApi;
readonly auth: Auth;
readonly accessLogBucket: IBucket;
}
export class Frontend extends Construct {
readonly cloudFrontWebDistribution: CloudFrontWebDistribution;
constructor(scope: Construct, id: string, props: FrontendProps) {
super(scope, id);
const assetBucket = new Bucket(this, 'AssetBucket', {
encryption: BucketEncryption.S3_MANAGED,
blockPublicAccess: BlockPublicAccess.BLOCK_ALL,
enforceSSL: true,
removalPolicy: RemovalPolicy.DESTROY,
autoDeleteObjects: true,
});
const originAccessIdentity = new OriginAccessIdentity(this, 'OriginAccessIdentity');
const distribution = new CloudFrontWebDistribution(this, 'Distribution', {
originConfigs: [
{
s3OriginSource: {
s3BucketSource: assetBucket,
originAccessIdentity,
},
behaviors: [
{
isDefaultBehavior: true,
},
],
},
],
errorConfigurations: [
{
errorCode: 404,
errorCachingMinTtl: 0,
responseCode: 200,
responsePagePath: '/',
},
{
errorCode: 403,
errorCachingMinTtl: 0,
responseCode: 200,
responsePagePath: '/',
},
],
loggingConfig: {
bucket: props.accessLogBucket,
prefix: 'Frontend/',
},
});
new NodejsBuild(this, 'ReactBuild', {
assets: [
{
path: '../frontend',
exclude: ['node_modules', 'dist'],
commands: ['npm ci'],
// prevent too frequent frontend deployment, for temporary use
// assetHash: 'frontend_asset',
},
],
buildCommands: ['npm run bundle'],
buildEnvironment: {
VITE_BACKEND_API_URL: props.backendApi.apiEndpoint,
VITE_USER_POOL_ID: props.auth.userPool.userPoolId,
VITE_USER_POOL_CLIENT_ID: props.auth.client.userPoolClientId,
VITE_AWS_REGION: Stack.of(props.auth.userPool).region,
},
destinationBucket: assetBucket,
distribution,
outputSourceDirectory: 'dist',
});
this.cloudFrontWebDistribution = distribution;
}
}