Skip to content

Commit 95212c3

Browse files
stenczeltkaiz-io
andauthored
typescript/static-site: S3Origin -> S3BucketOrigin (#1107)
and use OriginAccessControl over OriginAccessIdentity Co-authored-by: Michael Kaiser <[email protected]>
1 parent c368c9b commit 95212c3

File tree

1 file changed

+1
-11
lines changed

1 file changed

+1
-11
lines changed

typescript/static-site/static-site.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
77
import * as targets from 'aws-cdk-lib/aws-route53-targets';
88
import * as cloudfront_origins from 'aws-cdk-lib/aws-cloudfront-origins';
99
import { CfnOutput, Duration, RemovalPolicy, Stack } from 'aws-cdk-lib';
10-
import * as iam from 'aws-cdk-lib/aws-iam';
1110
import { Construct } from 'constructs';
1211
import path = require('path');
1312

@@ -28,9 +27,6 @@ export class StaticSite extends Construct {
2827

2928
const zone = route53.HostedZone.fromLookup(this, 'Zone', { domainName: props.domainName });
3029
const siteDomain = props.siteSubDomain + '.' + props.domainName;
31-
const cloudfrontOAI = new cloudfront.OriginAccessIdentity(this, 'cloudfront-OAI', {
32-
comment: `OAI for ${name}`
33-
});
3430

3531
new CfnOutput(this, 'Site', { value: 'https://' + siteDomain });
3632

@@ -54,12 +50,6 @@ export class StaticSite extends Construct {
5450
autoDeleteObjects: true, // NOT recommended for production code
5551
});
5652

57-
// Grant access to cloudfront
58-
siteBucket.addToResourcePolicy(new iam.PolicyStatement({
59-
actions: ['s3:GetObject'],
60-
resources: [siteBucket.arnForObjects('*')],
61-
principals: [new iam.CanonicalUserPrincipal(cloudfrontOAI.cloudFrontOriginAccessIdentityS3CanonicalUserId)]
62-
}));
6353
new CfnOutput(this, 'Bucket', { value: siteBucket.bucketName });
6454

6555
// TLS certificate
@@ -85,7 +75,7 @@ export class StaticSite extends Construct {
8575
}
8676
],
8777
defaultBehavior: {
88-
origin: new cloudfront_origins.S3Origin(siteBucket, {originAccessIdentity: cloudfrontOAI}),
78+
origin: cloudfront_origins.S3BucketOrigin.withOriginAccessControl(siteBucket),
8979
compress: true,
9080
allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
9181
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,

0 commit comments

Comments
 (0)