@@ -7,7 +7,6 @@ import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
77import * as targets from 'aws-cdk-lib/aws-route53-targets' ;
88import * as cloudfront_origins from 'aws-cdk-lib/aws-cloudfront-origins' ;
99import { CfnOutput , Duration , RemovalPolicy , Stack } from 'aws-cdk-lib' ;
10- import * as iam from 'aws-cdk-lib/aws-iam' ;
1110import { Construct } from 'constructs' ;
1211import 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