Skip to content

Commit b546a1d

Browse files
authored
Merge pull request #32 from chris-qa-org/add-readme-key-features-and-usage-examples
Add Readme key features and usage examples
2 parents e8700ff + 7366e92 commit b546a1d

File tree

7 files changed

+349
-0
lines changed

7 files changed

+349
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
# terraform-aws-publii-hosting
2+
23
Terraform module to host a static site generated by Publii
34

45
[![Terraform CI](https://github.com/chris-qa-org/terraform-aws-publii-hosting/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/chris-qa-org/terraform-aws-publii-hosting/actions/workflows/main.yml?branch=main)
56
[![GitHub release](https://img.shields.io/github/release/chris-qa-org/terraform-aws-publii-hosting.svg)](https://github.com/chris-qa-org/terraform-aws-publii-hosting/releases/)
67

8+
This module launches static hosting resources (eg. S3 bucket, Cloudfront) specifically for sites generated by the Static Site Generator [Publii][1]
9+
10+
It can in most cases be used to host any static site, however this module adds some configurations for the features of [Publii][1]
11+
12+
## Key features:
13+
14+
- Creates an S3 bucket along with an IAM user which has the minimum required permissions to sync from [Publii][1]
15+
- Creates a CloudFront endpoint (And optionally ACM certificates and Route53 records in an existing Hosted Zone)
16+
- Creates a Lambda function to run a Cloudfront Invalidation when `files.publii.json` is created/updated (This file is updated on every sync)
17+
- Optionally have CloudFront do the right thing when 'Pretty URLs' are enabled (This is achieved via a CloudFront function which adds `index.html` to the URI if there is no extention)
18+
- Optionally redirect from the apex domain (eg. example.com) to www (www.example.com). If this is enabled (`var.cloudfront_enable_apex_to_www_redirect`), the 'Website Url' within 'Server' options should be set to www.yourdomain.com - [Publii S3 Server Settings docs (point 26)]
19+
- Optionally enable WAF
20+
- Optionally add custom origins and cache behaviours
21+
22+
## Usage
23+
24+
- [Full launch with existing Route53 Zone](./examples/full-launch-with-existing-route53-zone/README.tf)
25+
- [Use existing certificate and create own Route53 records](./examples/use-existing-certificate-and-create-own-route53-records/README.tf)
26+
- [Adding custom origins and cache behaviours](./examples/adding-custom-origins-and-cache-behaviours/README.tf)
27+
728
<!-- BEGIN_TF_DOCS -->
829
## Requirements
930

@@ -107,3 +128,7 @@ Terraform module to host a static site generated by Publii
107128
| <a name="output_project_random_id"></a> [project\_random\_id](#output\_project\_random\_id) | The random ID generated to ensure unique resource names |
108129
| <a name="output_s3_bucket_frontend"></a> [s3\_bucket\_frontend](#output\_s3\_bucket\_frontend) | S3 bucket frontend attributes |
109130
<!-- END_TF_DOCS -->
131+
132+
<!-- LINKS -->
133+
[1]: https://getpublii.com/
134+
[2]: https://getpublii.com/docs/setup-static-website-hosting-amazon-s3.html
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Adding custom origins and cache behaviours
2+
3+
- [main.tf](./main.tf)
4+
5+
```
6+
provider "aws" {
7+
region = "us-east-1"
8+
alias = "useast1"
9+
default_tags {
10+
tags = {
11+
Project = "my-project"
12+
}
13+
}
14+
}
15+
16+
resource "aws_route53_zone" "example" {
17+
name = "example.com"
18+
}
19+
20+
module "aws_publii_hosting" {
21+
source = "chris-qa-org/terraform-aws-publii-hosting/aws"
22+
version = "v1.0.0"
23+
24+
providers = {
25+
aws.useast1 = aws.useast1
26+
}
27+
28+
site_url = "example.com"
29+
s3_bucket_acl = "private"
30+
cloudfront_enable_ipv6 = true
31+
cloudfront_enable_waf = true // Note: This will cost at least $5.00/month - https://aws.amazon.com/waf/pricing/ (default: false)
32+
cloudfront_enable_apex_to_www_redirect = true
33+
enable_publii_pretty_urls = true
34+
route53_hosted_zone_options = {
35+
id = aws_route53_zone.example.id
36+
create_certificate_dns_validation_records = true
37+
create_site_url_dns_records = true
38+
}
39+
40+
cloudfront_origins = [
41+
{
42+
domain_name = aws_s3_bucket.example.bucket_regional_domain_name
43+
origin_id = "example-custom-origin"
44+
45+
s3_origin_config = {
46+
origin_access_identity = aws_cloudfront_origin_access_identity.example.cloudfront_access_identity_path
47+
}
48+
}
49+
]
50+
51+
cloudfront_ordered_cache_behaviors = [
52+
{
53+
path_pattern = "/example/*"
54+
allowed_methods = ["GET", "HEAD"]
55+
cached_methods = ["GET", "HEAD"]
56+
target_origin_id = "example-custom-origin"
57+
58+
use_forwarded_values = true
59+
query_string = false
60+
headers = ["Origin"]
61+
cookies_forward = "none"
62+
63+
min_ttl = 0
64+
default_ttl = 86400
65+
max_ttl = 31536000
66+
compress = true
67+
viewer_protocol_policy = "redirect-to-https"
68+
}
69+
]
70+
}
71+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
alias = "useast1"
4+
default_tags {
5+
tags = {
6+
Project = "my-project"
7+
}
8+
}
9+
}
10+
11+
resource "aws_route53_zone" "example" {
12+
name = "example.com"
13+
}
14+
15+
module "aws_publii_hosting" {
16+
source = "chris-qa-org/terraform-aws-publii-hosting/aws"
17+
version = "v1.0.0"
18+
19+
providers = {
20+
aws.useast1 = aws.useast1
21+
}
22+
23+
site_url = "example.com"
24+
s3_bucket_acl = "private"
25+
cloudfront_enable_ipv6 = true
26+
cloudfront_enable_waf = true // Note: This will cost at least $5.00/month - https://aws.amazon.com/waf/pricing/ (default: false)
27+
cloudfront_enable_apex_to_www_redirect = true
28+
enable_publii_pretty_urls = true
29+
route53_hosted_zone_options = {
30+
id = aws_route53_zone.example.id
31+
create_certificate_dns_validation_records = true
32+
create_site_url_dns_records = true
33+
}
34+
35+
cloudfront_origins = [
36+
{
37+
domain_name = aws_s3_bucket.example.bucket_regional_domain_name
38+
origin_id = "example-custom-origin"
39+
40+
s3_origin_config = {
41+
origin_access_identity = aws_cloudfront_origin_access_identity.example.cloudfront_access_identity_path
42+
}
43+
}
44+
]
45+
46+
cloudfront_ordered_cache_behaviors = [
47+
{
48+
path_pattern = "/example/*"
49+
allowed_methods = ["GET", "HEAD"]
50+
cached_methods = ["GET", "HEAD"]
51+
target_origin_id = "example-custom-origin"
52+
53+
use_forwarded_values = true
54+
query_string = false
55+
headers = ["Origin"]
56+
cookies_forward = "none"
57+
58+
min_ttl = 0
59+
default_ttl = 86400
60+
max_ttl = 31536000
61+
compress = true
62+
viewer_protocol_policy = "redirect-to-https"
63+
}
64+
]
65+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Full launch with existing Route53 Zone
2+
3+
- [main.tf](./main.tf)
4+
5+
```
6+
provider "aws" {
7+
region = "us-east-1"
8+
alias = "useast1"
9+
default_tags {
10+
tags = {
11+
Project = "my-project"
12+
}
13+
}
14+
}
15+
16+
resource "aws_route53_zone" "example" {
17+
name = "example.com"
18+
}
19+
20+
module "aws_publii_hosting" {
21+
source = "chris-qa-org/terraform-aws-publii-hosting/aws"
22+
version = "v1.0.0"
23+
24+
providers = {
25+
aws.useast1 = aws.useast1
26+
}
27+
28+
site_url = "example.com"
29+
s3_bucket_acl = "private"
30+
cloudfront_enable_ipv6 = true
31+
cloudfront_enable_waf = true // Note: This will cost at least $5.00/month - https://aws.amazon.com/waf/pricing/ (default: false)
32+
cloudfront_enable_apex_to_www_redirect = true
33+
enable_publii_pretty_urls = true
34+
route53_hosted_zone_options = {
35+
id = aws_route53_zone.example.id
36+
create_certificate_dns_validation_records = true
37+
create_site_url_dns_records = true
38+
}
39+
}
40+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
alias = "useast1"
4+
default_tags {
5+
tags = {
6+
Project = "my-project"
7+
}
8+
}
9+
}
10+
11+
resource "aws_route53_zone" "example" {
12+
name = "example.com"
13+
}
14+
15+
module "aws_publii_hosting" {
16+
source = "chris-qa-org/terraform-aws-publii-hosting/aws"
17+
version = "v1.0.0"
18+
19+
providers = {
20+
aws.useast1 = aws.useast1
21+
}
22+
23+
site_url = "example.com"
24+
s3_bucket_acl = "private"
25+
cloudfront_enable_ipv6 = true
26+
cloudfront_enable_waf = true // Note: This will cost at least $5.00/month - https://aws.amazon.com/waf/pricing/ (default: false)
27+
cloudfront_enable_apex_to_www_redirect = true
28+
enable_publii_pretty_urls = true
29+
route53_hosted_zone_options = {
30+
id = aws_route53_zone.example.id
31+
create_certificate_dns_validation_records = true
32+
create_site_url_dns_records = true
33+
}
34+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Use existing certificate and create own Route53 records
2+
3+
- [main.tf](./main.tf)
4+
5+
```
6+
provider "aws" {
7+
region = "us-east-1"
8+
alias = "useast1"
9+
default_tags {
10+
tags = {
11+
Project = "my-project"
12+
}
13+
}
14+
}
15+
16+
module "aws_publii_hosting" {
17+
source = "chris-qa-org/terraform-aws-publii-hosting/aws"
18+
version = "v1.0.0"
19+
20+
providers = {
21+
aws.useast1 = aws.useast1
22+
}
23+
24+
site_url = "example.com"
25+
s3_bucket_acl = "private"
26+
cloudfront_tls_certificate_arn = "arn:aws:acm:us-east-1:123456789012:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
27+
cloudfront_enable_ipv6 = true
28+
cloudfront_enable_waf = true // Note: This will cost at least $5.00/month - https://aws.amazon.com/waf/pricing/ (default: false)
29+
cloudfront_enable_apex_to_www_redirect = true
30+
enable_publii_pretty_urls = true
31+
}
32+
33+
resource "aws_route53_zone" "example" {
34+
name = "example.com"
35+
}
36+
37+
resource "aws_route53_record" "frontend" {
38+
zone_id = aws_route53_zone.example.zone_id
39+
name = "www.example.com"
40+
type = "A"
41+
42+
alias {
43+
name = module.aws_publii_hosting.aws_cloudfront_distribution_frontend.domain_name
44+
zone_id = module.aws_publii_hosting.aws_cloudfront_distribution_frontend.hosted_zone_id
45+
evaluate_target_health = true
46+
}
47+
}
48+
49+
resource "aws_route53_record" "apex_redirect" {
50+
zone_id = aws_route53_zone.example.zone_id
51+
name = "example.com"
52+
type = "A"
53+
54+
alias {
55+
name = module.aws_publii_hosting.aws_cloudfront_distribution_frontend_www_redirect.domain_name
56+
zone_id = module.aws_publii_hosting.aws_cloudfront_distribution_frontend_www_redirect.hosted_zone_id
57+
evaluate_target_health = true
58+
}
59+
}
60+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
alias = "useast1"
4+
default_tags {
5+
tags = {
6+
Project = "my-project"
7+
}
8+
}
9+
}
10+
11+
module "aws_publii_hosting" {
12+
source = "chris-qa-org/terraform-aws-publii-hosting/aws"
13+
version = "v1.0.0"
14+
15+
providers = {
16+
aws.useast1 = aws.useast1
17+
}
18+
19+
site_url = "example.com"
20+
s3_bucket_acl = "private"
21+
cloudfront_tls_certificate_arn = "arn:aws:acm:us-east-1:123456789012:certificate/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
22+
cloudfront_enable_ipv6 = true
23+
cloudfront_enable_waf = true // Note: This will cost at least $5.00/month - https://aws.amazon.com/waf/pricing/ (default: false)
24+
cloudfront_enable_apex_to_www_redirect = true
25+
enable_publii_pretty_urls = true
26+
}
27+
28+
resource "aws_route53_zone" "example" {
29+
name = "example.com"
30+
}
31+
32+
resource "aws_route53_record" "frontend" {
33+
zone_id = aws_route53_zone.example.zone_id
34+
name = "www.example.com"
35+
type = "A"
36+
37+
alias {
38+
name = module.aws_publii_hosting.aws_cloudfront_distribution_frontend.domain_name
39+
zone_id = module.aws_publii_hosting.aws_cloudfront_distribution_frontend.hosted_zone_id
40+
evaluate_target_health = true
41+
}
42+
}
43+
44+
resource "aws_route53_record" "apex_redirect" {
45+
zone_id = aws_route53_zone.example.zone_id
46+
name = "example.com"
47+
type = "A"
48+
49+
alias {
50+
name = module.aws_publii_hosting.aws_cloudfront_distribution_frontend_www_redirect.domain_name
51+
zone_id = module.aws_publii_hosting.aws_cloudfront_distribution_frontend_www_redirect.hosted_zone_id
52+
evaluate_target_health = true
53+
}
54+
}

0 commit comments

Comments
 (0)