Skip to content

Commit d448584

Browse files
committed
Allow specifying Cloudfront origins
1 parent 77d547e commit d448584

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Terraform module to host a static site generated by Publii
8686
| <a name="input_cloudfront_enable_ipv6"></a> [cloudfront\_enable\_ipv6](#input\_cloudfront\_enable\_ipv6) | Enable IPv6 on CloudFront | `bool` | `true` | no |
8787
| <a name="input_cloudfront_enable_waf"></a> [cloudfront\_enable\_waf](#input\_cloudfront\_enable\_waf) | Enable CloudFront WAF | `bool` | `true` | no |
8888
| <a name="input_cloudfront_ordered_cache_behaviors"></a> [cloudfront\_ordered\_cache\_behaviors](#input\_cloudfront\_ordered\_cache\_behaviors) | List of ordered\_cache\_behavior objects. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#cache-behavior-arguments | `any` | `[]` | no |
89+
| <a name="input_cloudfront_orgins"></a> [cloudfront\_orgins](#input\_cloudfront\_orgins) | List of origin objects. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#origin-arguments | `any` | `[]` | no |
8990
| <a name="input_cloudfront_tls_certificate_arn"></a> [cloudfront\_tls\_certificate\_arn](#input\_cloudfront\_tls\_certificate\_arn) | CloudFront TLS certificate ARN (must be created in us-east-1 region) | `string` | `""` | no |
9091
| <a name="input_enable_publii_pretty_urls"></a> [enable\_publii\_pretty\_urls](#input\_enable\_publii\_pretty\_urls) | If you hae enabled 'Pretty URLs' in Publii, set this to true | `bool` | `false` | no |
9192
| <a name="input_route53_hosted_zone_options"></a> [route53\_hosted\_zone\_options](#input\_route53\_hosted\_zone\_options) | If you have a Route53 zone, the required DNS records can be created automatically. | <pre>object({<br> id = string<br> create_certificate_dns_validation_records = bool<br> create_site_url_dns_records = bool<br> })</pre> | <pre>{<br> "create_certificate_dns_validation_records": false,<br> "create_site_url_dns_records": false,<br> "id": ""<br>}</pre> | no |

cloudfront-frontend.tf

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,59 @@ resource "aws_cloudfront_distribution" "frontend" {
1212
}
1313
}
1414

15+
# copied/borrowed from https://github.com/terraform-aws-modules/terraform-aws-cloudfront/blob/master/main.tf
16+
dynamic "origin" {
17+
for_each = local.cloudfront_origins
18+
19+
content {
20+
domain_name = origin.value.domain_name
21+
origin_id = lookup(origin.value, "origin_id", origin.key)
22+
origin_path = lookup(origin.value, "origin_path", "")
23+
connection_attempts = lookup(origin.value, "connection_attempts", null)
24+
connection_timeout = lookup(origin.value, "connection_timeout", null)
25+
26+
dynamic "s3_origin_config" {
27+
for_each = length(keys(lookup(origin.value, "s3_origin_config", {}))) == 0 ? [] : [lookup(origin.value, "s3_origin_config", {})]
28+
29+
content {
30+
origin_access_identity = lookup(s3_origin_config.value, "cloudfront_access_identity_path", lookup(lookup(aws_cloudfront_origin_access_identity.this, lookup(s3_origin_config.value, "origin_access_identity", ""), {}), "cloudfront_access_identity_path", null))
31+
}
32+
}
33+
34+
dynamic "custom_origin_config" {
35+
for_each = length(lookup(origin.value, "custom_origin_config", "")) == 0 ? [] : [lookup(origin.value, "custom_origin_config", "")]
36+
37+
content {
38+
http_port = custom_origin_config.value.http_port
39+
https_port = custom_origin_config.value.https_port
40+
origin_protocol_policy = custom_origin_config.value.origin_protocol_policy
41+
origin_ssl_protocols = custom_origin_config.value.origin_ssl_protocols
42+
origin_keepalive_timeout = lookup(custom_origin_config.value, "origin_keepalive_timeout", null)
43+
origin_read_timeout = lookup(custom_origin_config.value, "origin_read_timeout", null)
44+
}
45+
}
46+
47+
dynamic "custom_header" {
48+
for_each = lookup(origin.value, "custom_header", [])
49+
50+
content {
51+
name = custom_header.value.name
52+
value = custom_header.value.value
53+
}
54+
}
55+
56+
dynamic "origin_shield" {
57+
for_each = length(keys(lookup(origin.value, "origin_shield", {}))) == 0 ? [] : [lookup(origin.value, "origin_shield", {})]
58+
59+
content {
60+
enabled = origin_shield.value.enabled
61+
origin_shield_region = origin_shield.value.origin_shield_region
62+
}
63+
}
64+
}
65+
}
66+
##
67+
1568
enabled = true
1669
aliases = local.cloudfront_enable_apex_to_www_redirect ? [
1770
"www.${local.site_url}"

locals.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ locals {
99
enable_publii_pretty_urls = var.enable_publii_pretty_urls
1010
route53_hosted_zone_options = var.route53_hosted_zone_options
1111
cloudfront_ordered_cache_behaviors = var.cloudfront_ordered_cache_behaviors
12+
cloudfront_origins = var.cloudfront_origins
1213
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ variable "cloudfront_enable_apex_to_www_redirect" {
3333
default = true
3434
}
3535

36+
variable "cloudfront_orgins" {
37+
description = "List of origin objects. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#origin-arguments"
38+
type = any
39+
default = []
40+
}
41+
3642
variable "cloudfront_ordered_cache_behaviors" {
3743
description = "List of ordered_cache_behavior objects. https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#cache-behavior-arguments"
3844
type = any

0 commit comments

Comments
 (0)