Skip to content

Commit

Permalink
Added support for any number of ordered caches (#70)
Browse files Browse the repository at this point in the history
* Added support for any number of ordered caches

These ordered caches have the same support, with the same api, as the
default cache already in this module.

This fixes #62

* Updated README.md

* fix missing bracket

* Executed 'terraform fmt'

* Updated README.md

Co-authored-by: Andriy Knysh <[email protected]>
Co-authored-by: Maxim Mironenko <[email protected]>
Co-authored-by: actions-bot <[email protected]>
  • Loading branch information
4 people authored Feb 24, 2020
1 parent 41c6c33 commit 99a794d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Available targets:
| error_document | An absolute path to the document to return in case of a 4XX error | string | `` | no |
| extra_logs_attributes | Additional attributes to put onto the log bucket label | list(string) | `<list>` | no |
| extra_origin_attributes | Additional attributes to put onto the origin label | list(string) | `<list>` | no |
| forward_cookies | Time in seconds that browser can cache the response for S3 bucket | string | `none` | no |
| forward_cookies | Specifies whether you want CloudFront to forward all or no cookies to the origin. Can be 'all' or 'none' | string | `none` | no |
| forward_header_values | A list of whitelisted header values to forward to the origin | list(string) | `<list>` | no |
| forward_query_string | Forward query strings to the origin that is associated with this cache behavior | bool | `false` | no |
| geo_restriction_locations | List of country codes for which CloudFront either to distribute content (whitelist) or not distribute your content (blacklist) | list(string) | `<list>` | no |
Expand All @@ -188,6 +188,7 @@ Available targets:
| minimum_protocol_version | Cloudfront TLS minimum protocol version | string | `TLSv1` | no |
| name | Name (e.g. `bastion` or `app`) | string | - | yes |
| namespace | Namespace (e.g. `eg` or `cp`) | string | `` | no |
| ordered_cache | An ordered list of cache behaviors resource for this distribution. List from top to bottom in order of precedence. The topmost cache behavior will have precedence 0. The fields can be described by the other variables in this file. For example, the field 'lambda_function_association' in this object has a description in var.lambda_function_association variable earlier in this file. The only difference is that fields on this object are in ordered caches, whereas the rest of the vars in this file apply only to the default cache. | object | `<list>` | no |
| origin_bucket | Origin S3 bucket name | string | `` | no |
| origin_force_destroy | Delete all objects from the bucket so that the bucket can be destroyed without error (e.g. `true` or `false`) | bool | `false` | no |
| origin_path | An optional element that causes CloudFront to request your content from a directory in your Amazon S3 bucket or your custom origin. It must begin with a /. Do not add a / at the end of the path. | string | `` | no |
Expand Down
3 changes: 2 additions & 1 deletion docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
| error_document | An absolute path to the document to return in case of a 4XX error | string | `` | no |
| extra_logs_attributes | Additional attributes to put onto the log bucket label | list(string) | `<list>` | no |
| extra_origin_attributes | Additional attributes to put onto the origin label | list(string) | `<list>` | no |
| forward_cookies | Time in seconds that browser can cache the response for S3 bucket | string | `none` | no |
| forward_cookies | Specifies whether you want CloudFront to forward all or no cookies to the origin. Can be 'all' or 'none' | string | `none` | no |
| forward_header_values | A list of whitelisted header values to forward to the origin | list(string) | `<list>` | no |
| forward_query_string | Forward query strings to the origin that is associated with this cache behavior | bool | `false` | no |
| geo_restriction_locations | List of country codes for which CloudFront either to distribute content (whitelist) or not distribute your content (blacklist) | list(string) | `<list>` | no |
Expand All @@ -44,6 +44,7 @@
| minimum_protocol_version | Cloudfront TLS minimum protocol version | string | `TLSv1` | no |
| name | Name (e.g. `bastion` or `app`) | string | - | yes |
| namespace | Namespace (e.g. `eg` or `cp`) | string | `` | no |
| ordered_cache | An ordered list of cache behaviors resource for this distribution. List from top to bottom in order of precedence. The topmost cache behavior will have precedence 0. The fields can be described by the other variables in this file. For example, the field 'lambda_function_association' in this object has a description in var.lambda_function_association variable earlier in this file. The only difference is that fields on this object are in ordered caches, whereas the rest of the vars in this file apply only to the default cache. | object | `<list>` | no |
| origin_bucket | Origin S3 bucket name | string | `` | no |
| origin_force_destroy | Delete all objects from the bucket so that the bucket can be destroyed without error (e.g. `true` or `false`) | bool | `false` | no |
| origin_path | An optional element that causes CloudFront to request your content from a directory in your Amazon S3 bucket or your custom origin. It must begin with a /. Do not add a / at the end of the path. | string | `` | no |
Expand Down
37 changes: 37 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,43 @@ resource "aws_cloudfront_distribution" "default" {
}
}

dynamic "ordered_cache_behavior" {
for_each = var.ordered_cache

content {
path_pattern = ordered_cache_behavior.value.path_pattern

allowed_methods = ordered_cache_behavior.value.allowed_methods
cached_methods = ordered_cache_behavior.value.cached_methods
target_origin_id = module.distribution_label.id
compress = ordered_cache_behavior.value.compress
trusted_signers = ordered_cache_behavior.value.trusted_signers

forwarded_values {
query_string = ordered_cache_behavior.value.forward_query_string
headers = ordered_cache_behavior.value.forward_header_values

cookies {
forward = ordered_cache_behavior.value.forward_cookies
}
}

viewer_protocol_policy = ordered_cache_behavior.value.viewer_protocol_policy
default_ttl = ordered_cache_behavior.value.default_ttl
min_ttl = ordered_cache_behavior.value.min_ttl
max_ttl = ordered_cache_behavior.value.max_ttl

dynamic "lambda_function_association" {
for_each = ordered_cache_behavior.value.lambda_function_association
content {
event_type = lambda_function_association.value.event_type
include_body = lookup(lambda_function_association.value, "include_body", null)
lambda_arn = lambda_function_association.value.lambda_arn
}
}
}
}

restrictions {
geo_restriction {
restriction_type = var.geo_restriction_type
Expand Down
34 changes: 33 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ variable "cors_max_age_seconds" {
variable "forward_cookies" {
type = string
default = "none"
description = "Time in seconds that browser can cache the response for S3 bucket"
description = "Specifies whether you want CloudFront to forward all or no cookies to the origin. Can be 'all' or 'none'"
}

variable "forward_header_values" {
Expand Down Expand Up @@ -372,6 +372,38 @@ variable "ipv6_enabled" {
description = "Set to true to enable an AAAA DNS record to be set as well as the A record"
}

variable "ordered_cache" {
type = list(object({
path_pattern = string

allowed_methods = list(string)
cached_methods = list(string)
compress = bool

viewer_protocol_policy = string
min_ttl = number
default_ttl = number
max_ttl = number

forward_query_string = bool
forward_header_values = list(string)
forward_cookies = string

lambda_function_association = list(object({
event_type = string
include_body = bool
lambda_arn = string
}))
}))
default = []
description = <<DESCRIPTION
An ordered list of cache behaviors resource for this distribution. List from top to bottom in order of precedence. The topmost cache behavior will have precedence 0.
The fields can be described by the other variables in this file. For example, the field 'lambda_function_association' in this object has
a description in var.lambda_function_association variable earlier in this file. The only difference is that fields on this object are in ordered caches, whereas the rest
of the vars in this file apply only to the default cache.
DESCRIPTION
}

variable "website_enabled" {
type = bool
default = false
Expand Down

0 comments on commit 99a794d

Please sign in to comment.