diff --git a/modules/aws/s3/bucket/main.tf b/modules/aws/s3/bucket/main.tf index 629fe542..9a90f48c 100644 --- a/modules/aws/s3/bucket/main.tf +++ b/modules/aws/s3/bucket/main.tf @@ -176,6 +176,28 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "this" { } } +resource "aws_s3_bucket_website_configuration" "this" { + count = var.enable_website ? 1 : 0 + bucket = aws_s3_bucket.this.id + routing_rules = var.routing_rules + + error_document { + key = var.error_document + } + + index_document { + suffix = var.index_document + } + + dynamic "redirect_all_requests_to" { + for_each = var.redirect_all_requests_to == null ? [] : [var.redirect_all_requests_to] + content { + host_name = redirect_all_requests_to.value.host_name + protocol = redirect_all_requests_to.value.protocol + } + } +} + resource "aws_s3_bucket_versioning" "this" { count = var.versioning_status == "Enabled" ? 1 : 0 bucket = aws_s3_bucket.this.id diff --git a/modules/aws/s3/bucket/variables.tf b/modules/aws/s3/bucket/variables.tf index 5fc3940f..4cc805c7 100644 --- a/modules/aws/s3/bucket/variables.tf +++ b/modules/aws/s3/bucket/variables.tf @@ -281,6 +281,34 @@ variable "sse_algorithm" { } } +###################### +# S3 Website Variables +###################### + +variable "error_document" { + type = string + description = "(Optional) An absolute path to the document to return in case of a 4XX error." + default = "error.html" +} + +variable "index_document" { + type = string + description = "(Optional) Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders." + default = "index.html" +} + +variable "redirect_all_requests_to" { + type = any + description = "(Optional) A hostname to redirect all website requests for this bucket to. Hostname can optionally be prefixed with a protocol (http:// or https://) to use when redirecting requests. The default is the protocol that is used in the original request." + default = null +} + +variable "routing_rules" { + type = any + description = "(Optional) A list of routing rules that can redirect requests to different directories or buckets. These rules are applied in the order that you specify them. For more information about routing rules, see Configuring advanced conditional redirects in the Amazon Simple Storage Service Developer Guide." + default = null +} + ###################### # S3 Versioning Variables ###################### @@ -349,12 +377,23 @@ variable "enable_s3_bucket_logging" { } } +variable "enable_website" { + type = bool + description = "(Optional) Enable static website hosting for S3 bucket. If true, this will create a website configuration for the bucket. Defaults to false." + default = false + validation { + condition = can(regex("true|false", var.enable_website)) + error_message = "The value must be true or false." + } +} + variable "expected_bucket_owner" { type = string description = "(Optional) Account ID of the expected bucket owner. If the bucket is owned by a different account, the request will fail with an HTTP 403 (Access Denied) error." default = null } + variable "tags" { type = map(any) description = "(Optional) A mapping of tags to assign to the bucket."