Skip to content

Latest commit

 

History

History
90 lines (65 loc) · 7.14 KB

File metadata and controls

90 lines (65 loc) · 7.14 KB

Terraform AWS CloudFront S3 hosting

Use this module to provision a CloudFront distribution to serve an static website or assets from a private S3 bucket, along with policies and related resources.

The default_root_object is expected to be "index.html" but a function_association can be provided to customize requests handling.

Check the .tool-versions file to see the tools and the versions used in this project.

Website Deployment

The basic steps to deploy an static website with this setup would be:

  1. Build your site
  2. Upload/sync your new build to S3
  3. Invalidate CloudFront cache

For example:

# Build (outputs to the dist directory)
npm run build

# Sync the dist directory to the S3 bucket my-web-bucket
aws s3 sync ./dist s3://my-web-bucket/ --delete

# Invalidate CloudFront cache
aws cloudfront create-invalidation \
  --distribution-id $CLOUDFRONT_DIST_ID \
  --paths "/*"

NOTICE: The Terraform documentation in this README is added by terraform-docs running as a pre-commit hook, see the .pre-commit-config.yaml file.

Example

An example of how this module is used can be found in this Terraform configuration.

Requirements

Name Version
terraform 1.13.3
aws ~> 6.15

Providers

Name Version
aws ~> 6.15

Resources

Name Type
aws_cloudfront_distribution.cdn resource
aws_cloudfront_origin_access_control.oac resource
aws_cloudwatch_log_delivery.this resource
aws_cloudwatch_log_delivery_destination.this resource
aws_cloudwatch_log_delivery_source.this resource
aws_cloudwatch_log_group.this resource
aws_route53_record.this resource
aws_s3_bucket.origin resource
aws_s3_bucket_policy.origin resource
aws_cloudfront_cache_policy.optimize data source
aws_iam_policy_document.origin data source

Inputs

Name Description Type Default Required
acm_cert_arn ACM certificate to use if aliases are given. A CloudFront cert is used by default string null no
cdn_aliases n/a list(string) [] no
env Deploy environment, e.g., prod, dev string n/a yes
function_association Forwarded to the aws_cloudfront_distribution resource any null no
project Name of the project in lowercase without spaces, e.g., myproject string n/a yes
route53_zone_id Hosted zone where the CDN aliases will be added to string null no

Outputs

Name Description
cloudfront_dist_aliases CloudFront distribution aliases
cloudfront_dist_domain_name CloudFront distribution domain name
cloudfront_dist_id CloudFront distribution id
cloudfront_hosted_zone_id CloudFront distribution hosted_zone_id
s3_bucket_name Name of the S3 bucket used for the site