Skip to content

Commit 2678c98

Browse files
committed
UI stack can now use OAC via new L2 construct S3bucketOrigin - win!
1 parent 63f26b8 commit 2678c98

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

cloud/lib/ui-stack.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import {
77
Distribution,
88
experimental,
99
LambdaEdgeEventType,
10-
OriginAccessIdentity,
1110
OriginRequestPolicy,
1211
PriceClass,
1312
ResponseHeadersPolicy,
1413
ViewerProtocolPolicy,
1514
} from 'aws-cdk-lib/aws-cloudfront';
16-
import { HttpOrigin, S3Origin } from 'aws-cdk-lib/aws-cloudfront-origins';
17-
import { CanonicalUserPrincipal, Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
15+
import { HttpOrigin, S3BucketOrigin } from 'aws-cdk-lib/aws-cloudfront-origins';
16+
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
1817
import { Runtime } from 'aws-cdk-lib/aws-lambda';
1918
import { AaaaRecord, ARecord, IHostedZone, RecordTarget } from 'aws-cdk-lib/aws-route53';
2019
import { CloudFrontTarget } from 'aws-cdk-lib/aws-route53-targets';
@@ -63,8 +62,6 @@ export class UiStack extends Stack {
6362
throw new Error('Region not defined in stack env, cannot continue!');
6463
}
6564

66-
const cloudfrontOAI = new OriginAccessIdentity(this, generateResourceId('cloudfront-OAI'));
67-
6865
/*
6966
UI Host Bucket
7067
*/
@@ -76,15 +73,6 @@ export class UiStack extends Stack {
7673
removalPolicy: RemovalPolicy.DESTROY,
7774
autoDeleteObjects: true,
7875
});
79-
hostBucket.addToResourcePolicy(
80-
new PolicyStatement({
81-
actions: ['s3:GetObject'],
82-
resources: [hostBucket.arnForObjects('*')],
83-
principals: [
84-
new CanonicalUserPrincipal(cloudfrontOAI.cloudFrontOriginAccessIdentityS3CanonicalUserId),
85-
],
86-
})
87-
);
8876

8977
/*
9078
Edge lambda as JWT token verifier, to check request has access token
@@ -151,10 +139,9 @@ export class UiStack extends Stack {
151139
},
152140
],
153141
defaultBehavior: {
154-
origin: new S3Origin(hostBucket, {
155-
originAccessIdentity: cloudfrontOAI,
156-
}),
142+
origin: S3BucketOrigin.withOriginAccessControl(hostBucket),
157143
cachePolicy: new CachePolicy(this, generateResourceId('site-cache-policy'), {
144+
// TODO Try removing this: cookie should only be needed on backend calls
158145
cookieBehavior: CacheCookieBehavior.allowList(`${appName}.sid`),
159146
}),
160147
compress: true,

0 commit comments

Comments
 (0)