Skip to content

Commit

Permalink
Add condition to associate the default function (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinpashok authored Apr 29, 2024
1 parent b3b0294 commit 3b23e18
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
| <a name="input_certificate_settings"></a> [certificate\_settings](#input\_certificate\_settings) | CloudFront certificate settings | `any` | <pre>{<br> "minimum_protocol_version": "TLSv1.2_2021",<br> "ssl_support_method": "sni-only"<br>}</pre> | no |
| <a name="input_cloudfront_logging_config"></a> [cloudfront\_logging\_config](#input\_cloudfront\_logging\_config) | The logging configuration that controls how logs are written to your distribution | `map(string)` | `{}` | no |
| <a name="input_cors_rule"></a> [cors\_rule](#input\_cors\_rule) | List of maps containing rules for Cross-Origin Resource Sharing for S3 bucket. | `any` | <pre>{<br> "cors_rule": {<br> "allowed_headers": [<br> "*"<br> ],<br> "allowed_methods": [<br> "PUT",<br> "POST",<br> "GET",<br> "DELETE"<br> ],<br> "allowed_origins": [<br> "*"<br> ],<br> "expose_headers": [<br> "ETag"<br> ],<br> "max_age_seconds": 3000<br> }<br>}</pre> | no |
| <a name="input_create_associate_function"></a> [create\_associate\_function](#input\_create\_associate\_function) | If the CloudFront function should be associated with the default cache behavior. | `bool` | `false` | no |
| <a name="input_create_bucket"></a> [create\_bucket](#input\_create\_bucket) | Whether to create S3 bucket, default to true | `bool` | `true` | no |
| <a name="input_create_certificate"></a> [create\_certificate](#input\_create\_certificate) | Create ACM certificate | `bool` | `true` | no |
| <a name="input_create_distribution"></a> [create\_distribution](#input\_create\_distribution) | Whether to create distribution | `bool` | `true` | no |
Expand Down
11 changes: 6 additions & 5 deletions cloudfront.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ module "cdn" {
origin_request_policy_id = data.aws_cloudfront_origin_request_policy.this.id
cache_policy_id = data.aws_cloudfront_cache_policy.this.id

function_association = {
viewer-request = {
function_arn = aws_cloudfront_function.viewer_request.arn
}
}
function_association = var.create_associate_function ? [{
event_type = "viewer-request"
function_arn = aws_cloudfront_function.viewer_request[0].arn
}] : []

}, var.default_cache_behavior)

ordered_cache_behavior = var.ordered_cache_behavior
Expand All @@ -76,6 +76,7 @@ module "cdn" {
}

resource "aws_cloudfront_function" "viewer_request" {
count = var.create_associate_function ? 1 : 0
name = var.default_index_function_name
runtime = "cloudfront-js-1.0"
publish = true
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ variable "web_acl_id" {
default = ""
}

variable "create_associate_function" {
description = "If the CloudFront function should be associated with the default cache behavior."
type = bool
default = false
}

variable "default_index_function_name" {
description = "Name of the CloudFront Function to create for index page redirection"
type = string
Expand Down

0 comments on commit 3b23e18

Please sign in to comment.