Skip to content

Commit 32faee5

Browse files
authored
feat: Support for CloudFront Functions (#41)
1 parent e016f5d commit 32faee5

File tree

6 files changed

+54
-4
lines changed

6 files changed

+54
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,21 @@ module "cdn" {
7979

8080
## Examples:
8181

82-
- [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, ACM Certificate, Route53 Records.
82+
- [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, ACM Certificate, Route53 Records.
8383

8484
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
8585
## Requirements
8686

8787
| Name | Version |
8888
|------|---------|
8989
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.0 |
90-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.37.0 |
90+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.41.0 |
9191

9292
## Providers
9393

9494
| Name | Version |
9595
|------|---------|
96-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.37.0 |
96+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.41.0 |
9797

9898
## Modules
9999

examples/complete/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Note that this example may create resources which cost money. Run `terraform des
5454

5555
| Name | Type |
5656
|------|------|
57+
| [aws_cloudfront_function.example](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_function) | resource |
5758
| [aws_s3_bucket_policy.bucket_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
5859
| [null_resource.download_package](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
5960
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function handler(event) {
2+
// NOTE: This example function is for a viewer request event trigger.
3+
// Choose viewer request for event trigger when you associate this function with a distribution.
4+
var response = {
5+
statusCode: 302,
6+
statusDescription: 'Found',
7+
headers: {
8+
'cloudfront-functions': { value: 'generated-by-CloudFront-Functions' },
9+
'location': { value: 'https://aws.amazon.com/cloudfront/' }
10+
}
11+
};
12+
return response;
13+
}

examples/complete/main.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ module "cloudfront" {
110110
cached_methods = ["GET", "HEAD"]
111111
compress = true
112112
query_string = true
113+
114+
function_association = {
115+
# Valid keys: viewer-request, viewer-response
116+
viewer-request = {
117+
function_arn = aws_cloudfront_function.example.arn
118+
}
119+
120+
viewer-response = {
121+
function_arn = aws_cloudfront_function.example.arn
122+
}
123+
}
113124
}
114125
]
115126

@@ -270,3 +281,8 @@ resource "random_pet" "this" {
270281
length = 2
271282
}
272283

284+
resource "aws_cloudfront_function" "example" {
285+
name = "example-${random_pet.this.id}"
286+
runtime = "cloudfront-js-1.0"
287+
code = file("example-function.js")
288+
}

main.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ resource "aws_cloudfront_distribution" "this" {
138138
include_body = lookup(l.value, "include_body", null)
139139
}
140140
}
141+
142+
dynamic "function_association" {
143+
for_each = lookup(i.value, "function_association", [])
144+
iterator = f
145+
146+
content {
147+
event_type = f.key
148+
function_arn = f.value.function_arn
149+
}
150+
}
141151
}
142152
}
143153

@@ -190,6 +200,16 @@ resource "aws_cloudfront_distribution" "this" {
190200
include_body = lookup(l.value, "include_body", null)
191201
}
192202
}
203+
204+
dynamic "function_association" {
205+
for_each = lookup(i.value, "function_association", [])
206+
iterator = f
207+
208+
content {
209+
event_type = f.key
210+
function_arn = f.value.function_arn
211+
}
212+
}
193213
}
194214
}
195215

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ terraform {
22
required_version = ">= 0.13.0"
33

44
required_providers {
5-
aws = ">= 3.37.0"
5+
aws = ">= 3.41.0"
66
}
77
}

0 commit comments

Comments
 (0)