Browse the Repo

file-type-icon.circleci
file-type-iconexamples
file-type-iconmodules
file-type-icon_docs
file-type-icons3-cloudfront
file-type-icons3-static-website
file-type-iconREADME.adoc
file-type-iconcore-concepts.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
file-type-icontest
file-type-icon.gitignore
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.md

Browse the Repo

file-type-icon.circleci
file-type-iconexamples
file-type-iconmodules
file-type-icon_docs
file-type-icons3-cloudfront
file-type-icons3-static-website
file-type-iconREADME.adoc
file-type-iconcore-concepts.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvars.tf
file-type-icontest
file-type-icon.gitignore
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.md
S3

S3

Deploy your static content and static websites on S3. Supports bucket versioning, redirects, and access logging.

Code Preview

Preview the Code

mobile file icon

core-concepts.md

down

Quick Start

How to test the website?

This module outputs the domain name of your website using the website_domain_name output variable.

By default, the domain name will be of the form:

<BUCKET_NAME>.s3-website-<AWS_REGION>.amazonaws.com/

Where BUCKET_NAME is the name you specified for the bucket and AWS_REGION is the region you created the bucket in. For example, if the bucket was called foo and you deployed it in us-east-1, the URL would be:

foo.s3-website-us-east-1.amazonaws.com

If you set var.create_route53_entry to true, then this module will create a DNS A record in Route 53 for your S3 bucket with the domain name in var.website_domain_name, and you will be able to use that custom domain name to access your bucket instead of the amazonaws.com domain.

How to configure HTTPS (SSL) or a CDN?

By default, the static content in an S3 bucket is only accessible over HTTP. To be able to access it over HTTPS, you need to deploy a CloudFront distribution in front of the S3 bucket. This will also act as a Content Distribution Network (CDN), which will reduce latency for your users. You will need to set the use_with_cloudfront parameter to true.

To set up a CloudFront distribution, see the s3-cloudfront module.

How do I handle www + root domains?

If you are using your S3 bucket for both the www. and root domain of a website (e.g. www.foo.com and foo.com), you need to create two buckets. One of the buckets contains the actual static content. The other sets the should_redirect_all_requests parameter to true and sets the redirect_all_requests_to parameter to the URL of the first site. See the Setting Up a Static Website Using a Custom Domain documentation for more info.

foo

How do I configure Cross Origin Resource Sharing (CORS)?

To enable Cross Origin Resource Sharing (CORS), you need to set the cors_rule parameter in this module:

module "s3_static_website" {
  source = "git::git@github.com:gruntwork-io/package-static-assets.git//modules/s3-static-website?ref=<VERSION>"

  # ... other params omitted ...

  # CORS settings
  cors_rule = [
    {
      allowed_headers = ["*"]
      allowed_methods = ["GET"]
      allowed_origins = ["www.your-domain.com"]
      expose_headers  = ["ETag","Origin","Access-Control-Request-Headers","Access-Control-Request-Method"]
      max_age_seconds = 3000
    },
    
    {
      allowed_headers = ["*"]
      allowed_methods = ["GET"]
      allowed_origins = ["www.another-domain.com"]
      expose_headers  = ["ETag","Origin","Access-Control-Request-Headers","Access-Control-Request-Method"]
      max_age_seconds = 3000
    }
  ]  
}

NOTE #1: due to a bug in Terraform, you CANNOT pass multiple origins in the allowed_origins parameter! Instead, add a separate entry to cors_rule for each origin.

NOTE #2: if you're also using the s3-cloudfront module, you MUST forward the Origin header using the forward_headers parameter or CORS won't work!

module "s3_cloudfront" {
  source = "git::git@github.com:gruntwork-io/package-static-assets.git//modules/s3-cloudfront?ref=<VERSION>"
  
  # ... other params omitted ...

  # MUST be specified or CORS won't work
  forward_headers = ["Origin"]
}

Questions? Ask away.

We're here to talk about our services, answer any questions, give advice, or just to chat.

Ready to hand off the Gruntwork?