Skip to content

v0.38.0

Compare
Choose a tag to compare
@cloudpossebot cloudpossebot released this 07 Jun 16:23
e9eae04
Ability to specify website hosting configuration @azec-pdx (#91)

what

  • We already use this module for S3 and want to be able to use it to deploy S3 buckets with static website hosting configs
  • The change in this PR adds that support

why

  • Would like to be able to use cloudposse/terraform-aws-s3-bucket (this module) to deploy buckets that support static website hosting and have very custom-tailored S3 bucket policy.
  • Tried using cloudposse/terraform-aws-s3-website for the same need and hit the roadblock of it not supporting custom bucket policies.
  • Our TF modules (in our org) are pinned down to use cloudposse/terraform-aws-s3-bucket for most S3 bucket needs, so we didn't have interest in using and modifying cloudposse/terraform-aws-s3-website module to support bucket custom bucket policies that are different then the "opinionated" ones that come with cloudposse/terraform-aws-s3-website.

usage

Downstream TF modules using this feature then can specify website configuration as in the example below.

  • file: profiles/s3_hosting_bucket/main.tf

     module "s3_bucket" {
          source = "git::https://github.com/SkywardIO/terraform-aws-s3-bucket.git//?ref=feature/website-hosting-config"
          
          ## Switch to CP release version if PR gets accepted
          # source  = "cloudposse/s3-bucket/aws"
          # version = "0.38.0"
        
          #...
          # other config of the module
          #...
        
          
          # website hosting - switchboard specific
          website_inputs = var.website_inputs
     }
  • file: profiles/s3_hosting_bucket/variables.tf

    # ...
    # other variables
    # ...
    variable "website_inputs" {
    
        type = list(object({
          index_document           = string
          error_document           = string
          redirect_all_requests_to = string
          routing_rules            = string # if you need to render valid JSON for this, use var.routing_rules and export such map construct to JSON
        }))
        default = null
      
        description = "Specifies the static website hosting configuration object."
    }
  • file: profiles/website/main.tf

    module "website" {
      source     = "../../profiles/s3_hosting_bucket"
      context    = module.this.context
      region     = var.region
      name       = module.this.name
      
      #...
      # other config of the module
      #...
      
      # determines static website hosting of the bucket - we are not interested in any kind of redirects by S3
      website_inputs = [
        {
          index_document           = "index.html"
          error_document           = "error.html"
          redirect_all_requests_to = null
          routing_rules            = var.routing_rules == null ? "" : jsonencode(var.routing_rules)
        }
      ]
      # next 4 variables determine "aws_s3_bucket_public_access_block" configuration
      block_public_acls       = var.block_public_acls
      block_public_policy     = var.block_public_policy
      ignore_public_acls      = var.ignore_public_acls
      restrict_public_buckets = var.restrict_public_buckets
      
    }
  • file: profiles/website/variables.tf

    # ...
    # other variables
    # ...
    variable "routing_rules" {
       type = list(object({
         RoutingRuleCondition = object({
           KeyPrefixEquals             = string
           HttpErrorCodeReturnedEquals = string
         })
         RedirectRule = object({
           HostName             = string
           HttpRedirectCode     = string
           Protocol             = string
           ReplaceKeyPrefixWith = string
           ReplaceKeyWith       = string
         })
       }))
       default = null
     
       description = "Specifies the helper generator for routing_rules key of website_inputs var - to obtain JSON easily."
    }

references

  • Can open an issue in this repo if there is need to further elaborate this.