Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to specify website hosting configuration #91

Merged
merged 2 commits into from Jun 7, 2021
Merged

Ability to specify website hosting configuration #91

merged 2 commits into from Jun 7, 2021

Conversation

azec-pdx
Copy link
Contributor

@azec-pdx azec-pdx commented Jun 4, 2021

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.

@azec-pdx azec-pdx requested review from a team as code owners June 4, 2021 01:33
@azec-pdx azec-pdx requested review from dotCipher and brcnblc June 4, 2021 01:33
jamengual
jamengual previously approved these changes Jun 4, 2021
@jamengual
Copy link
Contributor

/test all

@jamengual
Copy link
Contributor

/test all

@jamengual jamengual merged commit e9eae04 into cloudposse:master Jun 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants