Skip to content

Commit 0d7de2d

Browse files
Jiří Oláhjindrichskupa
authored andcommitted
SRE-12 - Migrate CF to S3 access from OAI to OAC and update S3 module version
1 parent 5326c4d commit 0d7de2d

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

main.tf

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3539
data "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

5567
module "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" {

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = "~> 5.0"
7+
version = "~> 5.27"
88
configuration_aliases = [aws.us_east_1]
99
}
1010
}

0 commit comments

Comments
 (0)