Skip to content

Commit

Permalink
SRE-12 - Migrate CF to S3 access from OAI to OAC and update S3 module…
Browse files Browse the repository at this point in the history
… version
  • Loading branch information
Jiří Oláh authored and jindrichskupa committed May 17, 2024
1 parent 5326c4d commit 0d7de2d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 20 additions & 11 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ module "certificate" {
tags = local.tags
}

resource "aws_cloudfront_origin_access_identity" "this" {
comment = "Access from CF to S3 - ${local.main_domain}"
resource "aws_cloudfront_origin_access_control" "this" {
name = "Access from CF to S3 - ${local.main_domain}"
description = "Access from CF to S3 - ${local.main_domain}"
origin_access_control_origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}

data "aws_iam_policy_document" "bucket_policy" {
statement {
sid = "AllowCloudFrontServicePrincipalReadOnly"
actions = [
"s3:GetObject",
]
Expand All @@ -43,18 +48,25 @@ data "aws_iam_policy_document" "bucket_policy" {
]

principals {
type = "AWS"
type = "Service"

identifiers = [
aws_cloudfront_origin_access_identity.this.iam_arn,
"cloudfront.amazonaws.com",
]
}

condition {
test = "StringEquals"
variable = "AWS:SourceArn"
values = [aws_cloudfront_distribution.this.arn]
}

}
}

module "s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
version = "3.15.1"
version = "4.1.2"

bucket = var.s3_bucket_name

Expand Down Expand Up @@ -92,12 +104,9 @@ resource "aws_cloudfront_distribution" "this" {
comment = local.main_domain

origin {
domain_name = module.s3_bucket.s3_bucket_bucket_regional_domain_name
origin_id = var.s3_bucket_name

s3_origin_config {
origin_access_identity = aws_cloudfront_origin_access_identity.this.cloudfront_access_identity_path
}
domain_name = module.s3_bucket.s3_bucket_bucket_regional_domain_name
origin_id = var.s3_bucket_name
origin_access_control_id = aws_cloudfront_origin_access_control.this.id
}

dynamic "origin" {
Expand Down
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
version = "~> 5.27"
configuration_aliases = [aws.us_east_1]
}
}
Expand Down

0 comments on commit 0d7de2d

Please sign in to comment.