Skip to content

This repository contains the Terraform configuration to deploy a static site distribution in AWS.

License

Notifications You must be signed in to change notification settings

aflalasker/aws-static-site-distribution

Repository files navigation

Static site distribution with AWS

This repository contains the terraform configuration required to start distributing a static site in AWS. The module will provision,

  1. S3 bucket - for static site and the Lambda@Edge code package.
  2. Cloudfront distribution for serving the static site
  3. Lambda function running Lambda@Edge to correctly render index pages from sub folders.
  4. Route53 domain and records - Optional.
  5. ACM certificate for the Route53 domain - Optional.

Usage

# This is a very basic example of how to use the module. 
module "my_static_site" {
  source      = "git@github.com:aflalasker/aws-static-site-distribution.git?ref=v1.0.0"
  bucket_name = "my-site-bucket"
  ordered_cache_behavior = {
    "/index.html" = {
      path_pattern = "/*"
      compress     = true
    }
  }
}

Module documentation

Below is the documentation for the module generated using Terraform-docs.

Requirements

Name Version
terraform ~>1.9.5
archive ~>2.6.0
aws ~>5.66.0
random ~>3.6.3

Providers

Name Version
archive ~>2.6.0
aws ~>5.66.0
random ~>3.6.3

Resources

Name Type
aws_acm_certificate.cert resource
aws_acm_certificate_validation.cert_validation resource
aws_cloudfront_distribution.distribution resource
aws_cloudfront_origin_access_control.oac resource
aws_cloudwatch_log_group.log_group resource
aws_iam_policy.policy resource
aws_iam_role.lambda_role resource
aws_iam_role_policy_attachment.lambda_policy_attachment resource
aws_lambda_function.edge_lambda resource
aws_route53_record.records resource
aws_route53_record.www resource
aws_route53_zone.primary resource
aws_route53domains_registered_domain.main resource
aws_s3_bucket.code resource
aws_s3_bucket.site resource
aws_s3_bucket_policy.policy_attachment resource
aws_s3_object.code_package resource
random_string.random_suffix resource
archive_file.zip data source
aws_iam_policy_document.bucket_policy data source
aws_iam_policy_document.lambda_assume_role_policy data source
aws_iam_policy_document.lambda_exec_role_policy data source

Inputs

Name Description Type Default Required
auto_renew Whether to automatically renew the domain registration. bool true no
bucket_name The name of the S3 bucket where the site contents will be published to. string null no
comment The comment for the Cloudfront distribution. string "Distributes the site content." no
default_cache_behavior_allowed_methods The allowed methods for the default cache behavior. list(string)
[
"GET",
"HEAD",
"OPTIONS"
]
no
default_cache_behavior_cache_policy_id The ID of the cache policy to associate with the default cache behavior. string "658327ea-f89d-4fab-a63d-7e88639e58f6" no
default_cache_behavior_cached_methods The cached methods for the default cache behavior. list(string)
[
"GET",
"HEAD"
]
no
default_cache_behavior_viewer_protocol_policy The viewer protocol policy for the default cache behavior. string "redirect-to-https" no
default_root_object The default root object for the Cloudfront distribution. string "index.html" no
lambda_log_retention The number of days to retain the logs for the Lambda function. number 1 no
ordered_cache_behavior values for ordered cache behavior
map(object({
path_pattern = string
cache_policy_id = optional(string, "658327ea-f89d-4fab-a63d-7e88639e58f6")
allowed_methods = optional(list(string), ["GET", "HEAD"])
cached_methods = optional(list(string), ["GET", "HEAD"])
viewer_protocol_policy = optional(string, "redirect-to-https")
compress = optional(bool, true)
}))
n/a yes
route53_domain_name The domain name to register with Route53. string null no
subject_alternative_names A list of additional domain names to include in the certificate. list(string) [] no
tags A map of tags to apply to the resources. map(string) {} no
transfer_lock Whether to lock the domain registration to prevent transfer. bool false no

Outputs

Name Description
cloudfront_distribution_config The Cloudfront distribution configuration
site_bucket_config The site bucket configuration

Contributions

If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on our GitHub repository.

License

This project is licensed under the Apache 2.0 License. See the LICENSE file for more details.

About

This repository contains the Terraform configuration to deploy a static site distribution in AWS.

Topics

Resources

License

Stars

Watchers

Forks