Skip to content

Commit f8bae59

Browse files
authored
Merge pull request #22 from DNXLabs/feature/function
add function for cloudfront distribution
2 parents 0e82d74 + 6dca277 commit f8bae59

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,15 @@ If you have specified cloudfront_default_certificate, TLSv1 must be specified.
5858
| alb\_dns\_name | ALB DNS Name that CloudFront will point as origin | `string` | n/a | yes |
5959
| certificate\_arn | Certificate for this app to use in CloudFront (US), must cover `hostname`. | `string` | n/a | yes |
6060
| cloudfront\_forward\_headers | Headers to forward to origin from CloudFront | `list(string)` | <pre>[<br> "*"<br>]</pre> | no |
61+
| cloudfront\_function\_arn | ARN of an existing CloudFront Function (use this if create\_cloudfront\_function=false) | `string` | `null` | no |
62+
| cloudfront\_function\_code | JavaScript code (cloudfront-js-2.0) of the CloudFront Function | `string` | `null` | no |
63+
| cloudfront\_function\_event\_type | Event type to associate with the function: viewer-request or viewer-response | `string` | `"viewer-request"` | no |
64+
| cloudfront\_function\_name | Name of the CloudFront Function | `string` | `null` | no |
6165
| cloudfront\_logging\_bucket | Bucket to store logs from app | `string` | `null` | no |
6266
| cloudfront\_logging\_prefix | Logging prefix | `string` | `""` | no |
6367
| cloudfront\_origin\_keepalive\_timeout | The amount of time, in seconds, that CloudFront maintains an idle connection with a custom origin server before closing the connection. Valid values are from 1 to 60 seconds. | `number` | `5` | no |
6468
| cloudfront\_origin\_read\_timeout | The amount of time, in seconds, that CloudFront waits for a response from a custom origin. The value applies both to the time that CloudFront waits for an initial response and the time that CloudFront waits for each subsequent packet. Valid values are from 4 to 60 seconds. | `number` | `30` | no |
69+
| create\_cloudfront\_function | If true, create and publish a CloudFront Function based on provided code | `bool` | `false` | no |
6570
| dynamic\_custom\_error\_response | One or more custom error response elements (multiples allowed) | <pre>list(object({<br> error_code = optional(number)<br> response_code = optional(number)<br> response_page_path = optional(string)<br> }))</pre> | `[]` | no |
6671
| dynamic\_custom\_origin\_config | Configuration for the custom origin config to be used in dynamic block | `any` | `[]` | no |
6772
| dynamic\_ordered\_cache\_behavior | Ordered Cache Behaviors to be used in dynamic block | `any` | `[]` | no |

_variables.tf

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,40 @@ variable "record_type" {
164164
type = string
165165
description = "Type of the record to create on Route53"
166166
default = "CNAME"
167-
}
167+
}
168+
########
169+
variable "create_cloudfront_function" {
170+
description = "If true, create and publish a CloudFront Function based on provided code"
171+
type = bool
172+
default = false
173+
}
174+
175+
variable "cloudfront_function_name" {
176+
description = "Name of the CloudFront Function"
177+
type = string
178+
default = null
179+
}
180+
181+
variable "cloudfront_function_code" {
182+
description = "JavaScript code (cloudfront-js-2.0) of the CloudFront Function"
183+
type = string
184+
default = null
185+
}
186+
187+
variable "cloudfront_function_event_type" {
188+
description = "Event type to associate with the function: viewer-request or viewer-response"
189+
type = string
190+
default = "viewer-request"
191+
192+
validation {
193+
condition = var.cloudfront_function_event_type == "viewer-request" || var.cloudfront_function_event_type == "viewer-response"
194+
error_message = "cloudfront_function_event_type must be 'viewer-request' or 'viewer-response'."
195+
}
196+
}
197+
198+
variable "cloudfront_function_arn" {
199+
description = "ARN of an existing CloudFront Function (use this if create_cloudfront_function=false)"
200+
type = string
201+
default = null
202+
}
203+

cloudfront.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ resource "aws_cloudfront_distribution" "default" {
118118
}
119119
}
120120

121+
dynamic "function_association" {
122+
for_each = local.resolved_cloudfront_function_arn == null ? [] : [1]
123+
content {
124+
event_type = var.cloudfront_function_event_type
125+
function_arn = local.resolved_cloudfront_function_arn
126+
}
127+
}
128+
121129
viewer_protocol_policy = "redirect-to-https"
122130
min_ttl = 0
123131
default_ttl = 3600
@@ -186,3 +194,18 @@ resource "aws_cloudfront_distribution" "default" {
186194
}
187195

188196
}
197+
198+
locals {
199+
resolved_cloudfront_function_arn = var.create_cloudfront_function ? (
200+
try(aws_cloudfront_function.this[0].arn, null)
201+
) : var.cloudfront_function_arn
202+
}
203+
204+
resource "aws_cloudfront_function" "this" {
205+
count = var.create_cloudfront_function ? 1 : 0
206+
name = var.cloudfront_function_name
207+
runtime = "cloudfront-js-2.0"
208+
comment = "Managed by terraform-aws-ecs-app-front"
209+
publish = true
210+
code = var.cloudfront_function_code
211+
}

0 commit comments

Comments
 (0)