Skip to content

This is a Terraform module to provision a static website using AWS S3 and CloudFront.

License

Notifications You must be signed in to change notification settings

Reemote/terraform-aws-static-website

 
 

Repository files navigation

AWS Static Website Terraform module

This is a Terraform module to provision a static website using AWS S3 and CloudFront with optional custom domain ability

Overview Architecture Diagram

Usage

Without Custom Domain

module "static-website" {
  source         = "git@github.com:aashari/terraform-aws-static-website.git"
  name           = "test.ashari.me"
}

The code above will provide an S3 bucket and a Cloudfront Distribution serving static assets in an S3 bucket

With Custom Domain Cloudflare

module "static-website" {
  source                  = "git@github.com:aashari/terraform-aws-static-website.git"
  name                    = "hello.ashari.me"
  custom_domain_provider  = "CLOUDFLARE"
  custom_domain_records   = ["hello", "www.hello"]
  custom_domain_zone_id   = "abcdefghijklmnopqrstuvwxyz12345"
  cloudflare_api_token    = "AaaA11AaaaAaaA1aa11AAa11A76aaAAa9aAAaa-a"
}

The code above will provide an S3 bucket and Cloudfront Distribution serving static assets in an S3 bucket with additional ACM certificates for the custom domains assigned to Cloudfront and creating new records in the Cloudflare Zone

With Custom Domain Route53

module "static-website" {
  source                  = "git@github.com:aashari/terraform-aws-static-website.git"
  name                    = "hello.ashari.me"
  custom_domain_provider  = "ROUTE53"
  custom_domain_records   = ["hello", "www.hello"]
  custom_domain_zone_id   = "Z0ABCDEFGHI1234567"
}

The code above will provide an S3 bucket and Cloudfront Distribution serving static assets in an S3 bucket with additional ACM certificates for the custom domains assigned to Cloudfront and creating new records in the Route53 Zone

With CloudFront Function

main.tf

module "static-website" {
  source                        = "git@github.com:aashari/terraform-aws-static-website.git"
  name                          = "ashari.tech"
  cloudfront_function_file_path = "function.js"
  cloudfront_function_runtime   = "cloudfront-js-1.0"
  cloudfront_function_type      = "viewer-request"
}

The code above will provide an S3 bucket and a Cloudfront Distribution serving static assets in an S3 bucket with additional CloudFront function (function.js) which redirect www request to non-www request

With CloudFront Access Logs

main.tf

module "static-website" {
  source                                = "git@github.com:aashari/terraform-aws-static-website.git"
  name                                  = "ashari.tech"
  cloudfront_access_log_bucket          = "default-cloudfront-access-logs-ap-southeast-1-1234567890"
  cloudfront_access_log_enable_cookies  = true
}

The code above will provide an S3 bucket and a Cloudfront Distribution serving static assets in an S3 bucket with additional configuration on logging the access logs into the S3 bucket

Requirements

Name Version
terraform ~> 1.1.7
aws ~> 4.8.0

Providers

Name Version
aws ~> 4.8.0
random ~> 3.1.2
cloudflare ~> 3.0

Inputs

Name Description Type Default Required
name Name of the static website stacks.
For example: my-website, my-website-staging, ashari.com, andi.ashari.me
string "" yes
default_tags Default tags to apply to all resources map(string) {} no
default_root_object Default root object to serve string index.html no
default_not_found_page Default not found page string index.html no
cloudfront_access_log_bucket Cloudfront access log bucket name string "" no
cloudfront_access_log_enable_cookies Enable CloudFront access logs to include cookies bool true no
cloudfront_enable_cors Enable CloudFront CORS for preflight requests bool false no
cloudfront_function_file_path Path to the CloudFront function file string "" no
cloudfront_function_runtime CloudFront function runtime string cloudfront-js-1.0 no
cloudfront_function_type CloudFront function event type to trigger string viewer-request no
custom_domain_provider Custom domain provider name.
Available values: CLOUDFLARE, ROUTE53
string "" no
custom_domain_records Custom domain records name to use for CloudFront distribution, use @ to use the zone domain name.
For example ["hello", "www.hello"] which represent hello.{{ROOT_DOMAIN}} and www.hello.{{ROOT_DOMAIN}}
or ["@", "www"] which represent {{ROOT_DOMAIN}} and www.{{ROOT_DOMAIN}}.
where ROOT_DOMAIN is coming from domain name from Zone provided in custom_domain_zone_id variable
list(string) [] yes if custom_domain_provider is not empty
custom_domain_zone_id Domain Provider zone ID which custom domain is registered to.
In Cloudflare this is called Zone Id, in Route53 this is called Hosted Zone Id
string "" yes if custom_domain_provider is not empty
custom_domain_ttl Custom domain TTL number 300 no
cloudflare_api_token Cloudflare API token string "" yes if custom_domain_provider is equal to CLOUDFLARE
s3_bucket_name S3 Bucket Name string "" S3 Custom bucket name

Outputs

Name Description
s3_bucket_name S3 bucket name
cloudfront_distribution_domain_name CloudFront distribution domain name

License

Apache 2 Licensed. See LICENSE for full details.

About

This is a Terraform module to provision a static website using AWS S3 and CloudFront.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HCL 100.0%