Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions static-site/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ module "static_site" {
bucket_name = "example.org"
hosted_zone = "my-hosted_zone"

restriction = {
geo_restriction = {
type = "none"
locations = []
}

viewer_certificate = {
minimum_protocol_version = "TLSv1.2_2021"
minimum_protocol_version = "TLSv1.2_2025"
}

tags = {
Expand Down
26 changes: 13 additions & 13 deletions static-site/cloudfront.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ resource "aws_cloudfront_distribution" "static_site" {
is_ipv6_enabled = true
default_root_object = "index.html"

origin {
domain_name = aws_s3_bucket.static_site.bucket_regional_domain_name
origin_id = "S3-${aws_s3_bucket.static_site.bucket}"
origin_access_control_id = aws_cloudfront_origin_access_control.oac.id
viewer_certificate {
acm_certificate_arn = aws_acm_certificate.cloudfront_cert.arn
ssl_support_method = "sni-only"
minimum_protocol_version = var.viewer_certificate.minimum_protocol_version
cloudfront_default_certificate = false
}

restrictions {
geo_restriction {
restriction_type = var.cloudfront.restriction.type
locations = var.cloudfront.restriction.locations
restriction_type = var.geo_restriction.type
locations = var.geo_restriction.locations
}
}

origin {
domain_name = aws_s3_bucket.static_site.bucket_regional_domain_name
origin_id = "S3-${aws_s3_bucket.static_site.bucket}"
origin_access_control_id = aws_cloudfront_origin_access_control.oac.id
}

default_cache_behavior {
target_origin_id = "S3-${aws_s3_bucket.static_site.bucket}"
response_headers_policy_id = aws_cloudfront_response_headers_policy.cloudfront.id
Expand All @@ -36,13 +43,6 @@ resource "aws_cloudfront_distribution" "static_site" {
viewer_protocol_policy = "redirect-to-https"
}

viewer_certificate {
acm_certificate_arn = aws_acm_certificate.cloudfront_cert.arn
ssl_support_method = "sni-only"
minimum_protocol_version = var.cloudfront.viewer_certificate.minimum_protocol_version
cloudfront_default_certificate = false
}

custom_error_response {
error_code = 403
response_code = 200
Expand Down
14 changes: 10 additions & 4 deletions static-site/data.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
data "aws_caller_identity" "current" {}

data "aws_route53_zone" "hosted_zone" {
count = local.create_hosted_zone ? 0 : 1
name = var.hosted_zone
Expand All @@ -6,7 +8,7 @@ data "aws_route53_zone" "hosted_zone" {

data "aws_iam_policy_document" "cloudfront_to_s3" {
statement {
sid = "AllowCloudFrontToAccessBucket"
sid = "AllowCloudFrontToAccessBucket"
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.static_site.arn}/*"]

Expand All @@ -18,9 +20,13 @@ data "aws_iam_policy_document" "cloudfront_to_s3" {
condition {
test = "StringEquals"
variable = "AWS:SourceArn"
values = [
aws_cloudfront_distribution.static_site.arn
]
values = [aws_cloudfront_distribution.static_site.arn]
}

condition {
test = "StringEquals"
variable = "AWS:SourceAccount"
values = [data.aws_caller_identity.current.account_id]
}
}
}
38 changes: 15 additions & 23 deletions static-site/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,26 @@ variable "domains" {
}
}

variable "cloudfront" {
variable "geo_restriction" {
type = object({
restriction = optional(object({
type = string
locations = list(string)
}),
{
type = "none"
locations = []
})
type = optional(string, "none")
locations = optional(list(string), [])
})
default = {
type = "none"
locations = []
}
description = "GEO restriction configuration for the CloudFront distribution"
}

viewer_certificate = optional(object({
minimum_protocol_version = string
}),
{
minimum_protocol_version = "TLSv1.2_2021"
})
variable "viewer_certificate" {
type = object({
minimum_protocol_version = optional(string, "TLSv1.2_2025")
})
default = {
restriction = {
type = "none"
locations = []
}
viewer_certificate = {
minimum_protocol_version = "TLSv1.2_2021"
}
minimum_protocol_version = "TLSv1.2_2025"
}
description = "Additional configuration options for the CloudFront distribution"
description = "Viewer certificate configuration for the CloudFront distribution"
}

variable "tags" {
Expand Down