Skip to content

Commit cfe50fe

Browse files
committed
fix: Updates
1 parent e954506 commit cfe50fe

File tree

5 files changed

+15
-66
lines changed

5 files changed

+15
-66
lines changed

README.md

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ module "cdn" {
139139
# }
140140
# }
141141
142-
# Option 2: Dynamic reference to module-managed functions by name
142+
# Option 2: Dynamic reference to module-managed functions by key/name
143143
function_association = {
144144
viewer-request = {
145-
function_name = "viewer-request-function"
145+
function_key = "viewer-request-function"
146146
}
147147
viewer-response = {
148-
function_name = "viewer-response-function"
148+
function_key = "viewer-response-function"
149149
}
150150
}
151151
}
@@ -157,48 +157,6 @@ module "cdn" {
157157
}
158158
```
159159

160-
**CloudFront Functions Features:**
161-
162-
- **Lightweight JavaScript execution** at CloudFront edge locations
163-
- **Sub-millisecond execution** for viewer request/response modifications
164-
- **Runtime options**: `cloudfront-js-1.0` (10KB limit) or `cloudfront-js-2.0` (30KB limit, default)
165-
- **Event types**: viewer-request, viewer-response (not origin-request/response)
166-
- **Key-Value Store integration**: Associate functions with CloudFront KeyValueStore (max 1 per function)
167-
- **Cost-effective**: Lower cost than Lambda@Edge for simple transformations
168-
169-
**Common use cases:**
170-
171-
- URL redirects and rewrites
172-
- Request/response header manipulation
173-
- Access control and authentication
174-
- A/B testing and feature flags
175-
- Cache key normalization
176-
177-
**Usage Pattern Note:**
178-
179-
The module supports two flexible patterns for associating CloudFront Functions with cache behaviors:
180-
181-
1. **Direct ARN Reference** (`function_arn`): Pass the ARN directly from external `aws_cloudfront_function` resources
182-
183-
```hcl
184-
function_association = {
185-
viewer-request = {
186-
function_arn = aws_cloudfront_function.external.arn
187-
}
188-
}
189-
```
190-
191-
2. **Dynamic Name Reference** (`function_name`): Reference module-managed functions by their map key
192-
```hcl
193-
function_association = {
194-
viewer-request = {
195-
function_name = "viewer-request-function" # Key from cloudfront_functions map
196-
}
197-
}
198-
```
199-
200-
The module automatically resolves function ARNs using Terraform's `try()` function, checking for `function_arn` first, then falling back to `function_name` lookup in module-created functions. This eliminates circular dependency issues while maintaining flexibility.
201-
202160
## Examples
203161

204162
- [Complete](https://github.com/terraform-aws-modules/terraform-aws-cloudfront/tree/master/examples/complete) - Complete example which creates AWS CloudFront distribution and integrates it with other [terraform-aws-modules](https://github.com/terraform-aws-modules) to create additional resources: S3 buckets, Lambda Functions, CloudFront Functions, VPC Origins, ACM Certificate, Route53 Records.
@@ -208,7 +166,7 @@ The module automatically resolves function ARNs using Terraform's `try()` functi
208166
- `Error: updating CloudFront Distribution (ETXXXXXXXXXXXX): InvalidArgument: The parameter ForwardedValues cannot be used when a cache policy is associated to the cache behavior.`
209167
- When defining a behavior in `ordered_cache_behavior` and `default_cache_behavior` with a cache policy, you must specify `use_forwarded_values = false`.
210168

211-
```
169+
```hcl
212170
ordered_cache_behavior = [{
213171
path_pattern = "/my/path"
214172
target_origin_id = "my-origin"
@@ -261,7 +219,7 @@ No modules.
261219
| Name | Description | Type | Default | Required |
262220
|------|-------------|------|---------|:--------:|
263221
| <a name="input_aliases"></a> [aliases](#input\_aliases) | Extra CNAMEs (alternate domain names), if any, for this distribution. | `list(string)` | `null` | no |
264-
| <a name="input_cloudfront_functions"></a> [cloudfront\_functions](#input\_cloudfront\_functions) | Map of CloudFront Function configurations. Key is used as default function name if 'name' not specified. | <pre>map(object({<br/> name = optional(string)<br/> runtime = optional(string, "cloudfront-js-2.0")<br/> comment = optional(string)<br/> publish = optional(bool)<br/> code = string<br/> key_value_store_associations = optional(list(string))<br/> }))</pre> | `{}` | no |
222+
| <a name="input_cloudfront_functions"></a> [cloudfront\_functions](#input\_cloudfront\_functions) | Map of CloudFront Function configurations. Key is used as default function name if 'name' not specified. | <pre>map(object({<br/> name = optional(string)<br/> runtime = optional(string, "cloudfront-js-2.0")<br/> comment = optional(string)<br/> publish = optional(bool)<br/> code = string<br/> key_value_store_associations = optional(list(string))<br/> }))</pre> | `null` | no |
265223
| <a name="input_comment"></a> [comment](#input\_comment) | Any comments you want to include about the distribution. | `string` | `null` | no |
266224
| <a name="input_continuous_deployment_policy_id"></a> [continuous\_deployment\_policy\_id](#input\_continuous\_deployment\_policy\_id) | Identifier of a continuous deployment policy. This argument should only be set on a production distribution. | `string` | `null` | no |
267225
| <a name="input_create_cloudfront_function"></a> [create\_cloudfront\_function](#input\_create\_cloudfront\_function) | Controls if CloudFront Functions should be created | `bool` | `false` | no |

examples/complete/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ module "cloudfront" {
187187
function_arn = aws_cloudfront_function.example.arn
188188
}
189189

190-
# Option 2: Dynamic reference to module-managed function by name
190+
# Option 2: Dynamic reference to module-managed function by key/name
191191
# Uncomment to use module-managed functions instead:
192192
# viewer-request = {
193-
# function_name = "viewer-request-security"
193+
# function_key = "viewer-request-security"
194194
# }
195195

196196
# viewer-response = {
197-
# function_name = "viewer-response-headers"
197+
# function_key = "viewer-response-headers"
198198
# }
199199

200200
# For this example, using standalone function for both

main.tf

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,6 @@ resource "aws_cloudfront_distribution" "this" {
217217
web_acl_id = var.web_acl_id
218218
tags = var.tags
219219

220-
# Ensure CloudFront Functions are created before the distribution
221-
depends_on = [
222-
aws_cloudfront_function.this
223-
]
224-
225220
dynamic "logging_config" {
226221
for_each = length(keys(var.logging_config)) == 0 ? [] : [var.logging_config]
227222

@@ -372,7 +367,7 @@ resource "aws_cloudfront_distribution" "this" {
372367

373368
content {
374369
event_type = f.key
375-
function_arn = lookup(f.value, "function_arn", try(aws_cloudfront_function.this[lookup(f.value, "function_name", "")].arn, null))
370+
function_arn = lookup(f.value, "function_arn", try(aws_cloudfront_function.this[f.value.function_key].arn, null))
376371
}
377372
}
378373

@@ -488,6 +483,10 @@ resource "aws_cloudfront_distribution" "this" {
488483
}
489484
}
490485
}
486+
487+
depends_on = [
488+
aws_cloudfront_function.this
489+
]
491490
}
492491

493492
resource "aws_cloudfront_monitoring_subscription" "this" {

variables.tf

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,5 @@ variable "cloudfront_functions" {
304304
code = string
305305
key_value_store_associations = optional(list(string))
306306
}))
307-
default = {}
308-
309-
validation {
310-
condition = alltrue([
311-
for k, v in var.cloudfront_functions :
312-
contains(["cloudfront-js-1.0", "cloudfront-js-2.0"], v.runtime)
313-
])
314-
error_message = "Runtime must be 'cloudfront-js-1.0' or 'cloudfront-js-2.0'. Provided runtime is invalid."
315-
}
307+
default = null
316308
}

wrappers/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module "wrapper" {
44
for_each = var.items
55

66
aliases = try(each.value.aliases, var.defaults.aliases, null)
7-
cloudfront_functions = try(each.value.cloudfront_functions, var.defaults.cloudfront_functions, {})
7+
cloudfront_functions = try(each.value.cloudfront_functions, var.defaults.cloudfront_functions, null)
88
comment = try(each.value.comment, var.defaults.comment, null)
99
continuous_deployment_policy_id = try(each.value.continuous_deployment_policy_id, var.defaults.continuous_deployment_policy_id, null)
1010
create_cloudfront_function = try(each.value.create_cloudfront_function, var.defaults.create_cloudfront_function, false)

0 commit comments

Comments
 (0)