Skip to content

Commit fbfaf51

Browse files
mbarrienczimergebot
authored andcommitted
Add internal features to aws-single-page-static-site (#125)
Add internal features to aws-single-page-static-site### Summary This PR brings features over from CZI's internal repo to the public aws-single-page-static-site module. This includes overriding the bucket name, public access blocks, more allowed headers, ordered cache behavior, more outputs, and upgrading the minimum TLS version.
1 parent 2289b2d commit fbfaf51

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

aws-single-page-static-site/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,25 @@ module "site" {
4545
| aliases | Vanity aliases. Make sure your provided cert supports these. | list | `<list>` | no |
4646
| aws\_acm\_cert\_arn | An AWS ACM cert. Note that Cloudfront requires certs to be in us-east-1. | string | n/a | yes |
4747
| aws\_route53\_zone\_id | A route53 zone ID used to write records. | string | n/a | yes |
48+
| bucket\_name | Name of the bucket to created. If not given, it will use the domain name. | string | `""` | no |
4849
| cloudfront\_price\_class | Cloudfront [price class](https://aws.amazon.com/cloudfront/pricing/). | string | `"PriceClass_100"` | no |
4950
| env | Env for tagging and naming. See [doc](../README.md#consistent-tagging) | string | n/a | yes |
5051
| index\_document\_path | The path to the index document of your site. | string | `"index.html"` | no |
5152
| minimum\_tls\_version | Minimum TLS version to accept. | string | `"TLSv1_2016"` | no |
5253
| owner | Owner for tagging and naming. See [doc](../README.md#consistent-tagging) | string | n/a | yes |
54+
| path\_pattern | The pattern (for example, images/*.jpg) that specifies which requests you want this cache behavior to apply to. | string | `"*"` | no |
5355
| project | Project for tagging and naming. See [doc](../README.md#consistent-tagging) | string | n/a | yes |
5456
| service | Service for tagging and naming. See [doc](../README.md#consistent-tagging) | string | n/a | yes |
5557
| subdomain | The subdomain for this static site. | string | n/a | yes |
5658

59+
## Outputs
60+
61+
| Name | Description |
62+
|------|-------------|
63+
| bucket\_name | |
64+
| bucket\_arn | |
65+
| cloudfront\_arn | |
66+
| cloudfront\_domain\_name | |
67+
| cloudfront\_hosted\_zone\_id | |
68+
5769
<!-- END -->

aws-single-page-static-site/main.tf

+36-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ locals {
99

1010
domain = "${replace(data.aws_route53_zone.zone.name, "/\\.$/", "")}"
1111
website_fqdn = "${var.subdomain}.${local.domain}"
12-
bucket_name = "${local.website_fqdn}"
12+
bucket_name = "${var.bucket_name != "" ? var.bucket_name : local.website_fqdn}"
1313

1414
aliases = [
1515
"${local.website_fqdn}",
@@ -54,8 +54,9 @@ resource "aws_s3_bucket" "bucket" {
5454

5555
// Cloudfront needs this to compress assets
5656
// https://stackoverflow.com/questions/35590622/cloudfront-with-s3-website-as-origin-is-not-serving-gzipped-files
57+
// Content-Type is also needed to allow CORS json requests
5758
cors_rule {
58-
allowed_headers = ["Authorization", "Content-Length"]
59+
allowed_headers = ["Authorization", "Content-Length", "Content-Type"]
5960
allowed_methods = ["GET"]
6061
allowed_origins = ["*"]
6162
max_age_seconds = 3000
@@ -72,6 +73,15 @@ resource "aws_s3_bucket" "bucket" {
7273
tags = "${local.tags}"
7374
}
7475

76+
resource "aws_s3_bucket_public_access_block" "bucket" {
77+
bucket = "${aws_s3_bucket.bucket.id}"
78+
79+
block_public_acls = true
80+
block_public_policy = true
81+
ignore_public_acls = true
82+
restrict_public_buckets = true
83+
}
84+
7585
resource "aws_cloudfront_distribution" "s3_distribution" {
7686
origin {
7787
domain_name = "${aws_s3_bucket.bucket.bucket_domain_name}"
@@ -89,7 +99,7 @@ resource "aws_cloudfront_distribution" "s3_distribution" {
8999
aliases = "${concat(var.aliases, local.aliases)}"
90100

91101
default_cache_behavior {
92-
allowed_methods = ["GET", "HEAD"]
102+
allowed_methods = ["GET", "HEAD", "OPTIONS"]
93103
cached_methods = ["GET", "HEAD"]
94104

95105
target_origin_id = "${local.website_fqdn}"
@@ -109,6 +119,29 @@ resource "aws_cloudfront_distribution" "s3_distribution" {
109119
compress = true
110120
}
111121

122+
ordered_cache_behavior {
123+
path_pattern = "${var.path_pattern}"
124+
allowed_methods = ["GET", "HEAD", "OPTIONS"]
125+
cached_methods = ["GET", "HEAD"]
126+
127+
target_origin_id = "${local.website_fqdn}"
128+
129+
forwarded_values {
130+
query_string = true
131+
headers = ["Origin"]
132+
133+
cookies {
134+
forward = "none"
135+
}
136+
}
137+
138+
viewer_protocol_policy = "redirect-to-https"
139+
min_ttl = 0
140+
default_ttl = 3600
141+
max_ttl = 86400
142+
compress = true
143+
}
144+
112145
restrictions {
113146
geo_restriction {
114147
restriction_type = "none"

aws-single-page-static-site/outputs.tf

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
output "bucket_name" {
2+
value = local.bucket_name
3+
}
4+
5+
output "bucket_arn" {
6+
value = aws_s3_bucket.bucket.arn
7+
}
8+
9+
output "cloudfront_arn" {
10+
value = aws_cloudfront_distribution.s3_distribution.arn
11+
}
12+
113
output "cloudfront_domain_name" {
214
value = aws_cloudfront_distribution.s3_distribution.domain_name
315
}

aws-single-page-static-site/variables.tf

+13-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ variable "cloudfront_price_class" {
4747

4848
variable "minimum_tls_version" {
4949
type = "string"
50-
default = "TLSv1_2016"
50+
default = "TLSv1.1_2016"
5151
description = "Minimum TLS version to accept."
5252
}
5353

@@ -56,3 +56,15 @@ variable "aliases" {
5656
default = []
5757
description = "Vanity aliases. Make sure your provided cert supports these."
5858
}
59+
60+
variable "bucket_name" {
61+
type = "string"
62+
description = "Name of the bucket to created. If not given, it will use the domain name."
63+
default = ""
64+
}
65+
66+
variable "path_pattern" {
67+
type = "string"
68+
description = "The pattern (for example, images/*.jpg) that specifies which requests you want this cache behavior to apply to."
69+
default = "*"
70+
}

0 commit comments

Comments
 (0)