Skip to content

Commit

Permalink
UI stack can now use OAC via new L2 construct S3bucketOrigin - win!
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswilty committed Oct 23, 2024
1 parent 63f26b8 commit 2678c98
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions cloud/lib/ui-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import {
Distribution,
experimental,
LambdaEdgeEventType,
OriginAccessIdentity,
OriginRequestPolicy,
PriceClass,
ResponseHeadersPolicy,
ViewerProtocolPolicy,
} from 'aws-cdk-lib/aws-cloudfront';
import { HttpOrigin, S3Origin } from 'aws-cdk-lib/aws-cloudfront-origins';
import { CanonicalUserPrincipal, Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { HttpOrigin, S3BucketOrigin } from 'aws-cdk-lib/aws-cloudfront-origins';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import { AaaaRecord, ARecord, IHostedZone, RecordTarget } from 'aws-cdk-lib/aws-route53';
import { CloudFrontTarget } from 'aws-cdk-lib/aws-route53-targets';
Expand Down Expand Up @@ -63,8 +62,6 @@ export class UiStack extends Stack {
throw new Error('Region not defined in stack env, cannot continue!');
}

const cloudfrontOAI = new OriginAccessIdentity(this, generateResourceId('cloudfront-OAI'));

/*
UI Host Bucket
*/
Expand All @@ -76,15 +73,6 @@ export class UiStack extends Stack {
removalPolicy: RemovalPolicy.DESTROY,
autoDeleteObjects: true,
});
hostBucket.addToResourcePolicy(
new PolicyStatement({
actions: ['s3:GetObject'],
resources: [hostBucket.arnForObjects('*')],
principals: [
new CanonicalUserPrincipal(cloudfrontOAI.cloudFrontOriginAccessIdentityS3CanonicalUserId),
],
})
);

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

0 comments on commit 2678c98

Please sign in to comment.