@@ -28,12 +28,17 @@ module "certificate" {
2828 tags = local. tags
2929}
3030
31- resource "aws_cloudfront_origin_access_identity" "this" {
32- comment = " Access from CF to S3 - ${ local . main_domain } "
31+ resource "aws_cloudfront_origin_access_control" "this" {
32+ name = " Access from CF to S3 - ${ local . main_domain } "
33+ description = " Access from CF to S3 - ${ local . main_domain } "
34+ origin_access_control_origin_type = " s3"
35+ signing_behavior = " always"
36+ signing_protocol = " sigv4"
3337}
3438
3539data "aws_iam_policy_document" "bucket_policy" {
3640 statement {
41+ sid = " AllowCloudFrontServicePrincipalReadOnly"
3742 actions = [
3843 " s3:GetObject" ,
3944 ]
@@ -43,18 +48,25 @@ data "aws_iam_policy_document" "bucket_policy" {
4348 ]
4449
4550 principals {
46- type = " AWS "
51+ type = " Service "
4752
4853 identifiers = [
49- aws_cloudfront_origin_access_identity . this . iam_arn ,
54+ " cloudfront.amazonaws.com " ,
5055 ]
5156 }
57+
58+ condition {
59+ test = " StringEquals"
60+ variable = " AWS:SourceArn"
61+ values = [aws_cloudfront_distribution . this . arn ]
62+ }
63+
5264 }
5365}
5466
5567module "s3_bucket" {
5668 source = " terraform-aws-modules/s3-bucket/aws"
57- version = " 3.15.1 "
69+ version = " 4.1.2 "
5870
5971 bucket = var. s3_bucket_name
6072
@@ -92,12 +104,9 @@ resource "aws_cloudfront_distribution" "this" {
92104 comment = local. main_domain
93105
94106 origin {
95- domain_name = module. s3_bucket . s3_bucket_bucket_regional_domain_name
96- origin_id = var. s3_bucket_name
97-
98- s3_origin_config {
99- origin_access_identity = aws_cloudfront_origin_access_identity. this . cloudfront_access_identity_path
100- }
107+ domain_name = module. s3_bucket . s3_bucket_bucket_regional_domain_name
108+ origin_id = var. s3_bucket_name
109+ origin_access_control_id = aws_cloudfront_origin_access_control. this . id
101110 }
102111
103112 dynamic "origin" {
0 commit comments