This terraform script creates an AWS CloudFront website by building out the following infrastructure:
- This includes a DNS entry for domain validation
- An
Apex
A record alias pointing to the newly created CloudFront domain -
- Example:
example.com
- Example:
- A
www
CNAME record also pointing the the CloudFront domain -
- Example:
www.example.com
- Example:
- Static web site hosting
-
- Bucket name:
www.example.com
- Bucket name:
- Bucket Versioning is enabled
- Logging is enable, using a predefined logs bucket
-
- The logs bucket is defined in a
.tfvars
file (see example below)
- The logs bucket is defined in a
- Bucket AES256 encryption
- An example
index.html
file will be uploaded
Block public access
is enabledBucket Policy
allows access only from the newly createdCloudFront Origin Access Identity
- Used by the CloudFront Distribution
- Used by the S3 bucket permission policy
- Use only North America and Europe (lowest/free pricing tier)
- Alternative domain names for:
-
- The Apex -
example.com
- The Apex -
-
www.example.com
- Custom SSL certificate (just created in ACM)
- TLSv1.2_2021
- Standard logging enabled
Origins
- Used the newly createdCF OAI
Viewer Protocol
- Redirect HTTP to HTTPS
- The included
redirect.js
file will be used to redirect the following: -
www.example.com/something
redirects towww.example.com/something/index.html
- To use this config, you would run:
-
terraform init
-
terraform validate
-
terraform plan -var-file="example.tfvars"
-
terraform apply -var-file="example.tfvars"
- The
apply
command will take about 5 minutes to complete