Skip to content

Commit 3b1a554

Browse files
merge commit
2 parents 8012848 + eff2ded commit 3b1a554

File tree

9 files changed

+94
-39
lines changed

9 files changed

+94
-39
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Changelog
22

3+
## 11.0.0 (June 15, 2021)
4+
5+
The output `cloudfront_origin_image_optimizer` is now deprecated.
6+
Use `cloudfront_origin` instead.
7+
8+
- Fixes image optimization for webp format ([#44](https://github.com/dealmore/terraform-aws-next-js-image-optimization/pull/44))
9+
Thanks to [@vcnc-hex](https://github.com/vcnc-hex) for contributing!
10+
- Uses Image Optimizer module of [Next.js 11.0.0](https://github.com/vercel/next.js/releases/tag/v11.0.0) ([#45](https://github.com/dealmore/terraform-aws-next-js-image-optimization/pull/45))
11+
- Add output for cache behavior ([#43](https://github.com/dealmore/terraform-aws-next-js-image-optimization/issues/43), [#48](https://github.com/dealmore/terraform-aws-next-js-image-optimization/pull/48))
12+
- Bump AWS API Gateway Terraform module from 0.11.0 to 1.1.0 ([#47](https://github.com/dealmore/terraform-aws-next-js-image-optimization/pull/47))
13+
- Bump AWS Lambda Terraform module from 1.47.0 to 2.4.0 ([#46](https://github.com/dealmore/terraform-aws-next-js-image-optimization/pull/46))
14+
315
## 10.2.3 (June 05, 2021)
416

517
- Improves caching of processed images by using [CloudFront Origin Shield](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/origin-shield.html) ([#2](https://github.com/dealmore/terraform-aws-next-js-image-optimization/issues/2), [#41](https://github.com/dealmore/terraform-aws-next-js-image-optimization/pull/41))

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,21 @@ Then rebuild and redeploy your Next.js application to make use of the changed co
135135
| next\_image\_device\_sizes | Allowed device sizes that should be used for image optimization. | `list(number)` | `null` | no |
136136
| next\_image\_domains | Allowed origin domains that can be used for fetching images. | `list(string)` | `[]` | no |
137137
| next\_image\_image\_sizes | Allowed image sizes that should be used for image optimization. | `list(number)` | `null` | no |
138-
| next\_image\_version | Next.js version from where you want to use the image optimizer from. Supports semver ranges. | `string` | `"10.2.3"` | no |
138+
| next\_image\_version | Next.js version from where you want to use the image optimizer from. Supports semver ranges. | `string` | `"11.0.0"` | no |
139139
| source\_bucket\_id | When your static files are deployed to a Bucket (e.g. with Terraform Next.js) the optimizer can pull the source from the bucket rather than over the internet. | `string` | `null` | no |
140140
| tags | Tag metadata to label AWS resources that support tags. | `map(string)` | `{}` | no |
141141

142142
## Outputs
143143

144144
| Name | Description |
145145
|------|-------------|
146+
| cloudfront\_cache\_behavior | Predefined CloudFront cache behavior. Can be used to embed the image optimizer into an existing CloudFront resource. |
146147
| cloudfront\_cache\_policy\_id | Cache policy id used for image optimization. |
147148
| cloudfront\_domain\_name | Domain of the internal CloudFront distribution. |
148149
| cloudfront\_hosted\_zone\_id | Zone id of the internal CloudFront distribution. |
150+
| cloudfront\_origin | Predefined CloudFront origin. Can be used to embed the image optimizer into an existing CloudFront resource. |
149151
| cloudfront\_origin\_id | Id of the custom origin used for image optimization. |
150-
| cloudfront\_origin\_image\_optimizer | Predefined CloudFront origin of the image optimizer. Can be used to embedd the image optimizer into an existing CloudFront resource. |
152+
| cloudfront\_origin\_image\_optimizer | Deprecated, please use cloudfront\_origin instead. |
151153
| cloudfront\_origin\_request\_policy\_id | Request policy id used for image optimization. |
152154

153155
<!--- END_TF_DOCS --->

examples/with-existing-cloudfront/main.tf

+13-8
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,21 @@ resource "aws_cloudfront_distribution" "distribution" {
3535
is_ipv6_enabled = true
3636
comment = "next-image-optimizer-example-external-cf"
3737

38-
default_cache_behavior {
39-
allowed_methods = ["GET", "HEAD"]
40-
cached_methods = ["GET", "HEAD"]
41-
target_origin_id = module.next_image_optimizer.cloudfront_origin_id
38+
# This is a generic dynamic to create the default cache behavior
39+
dynamic "default_cache_behavior" {
40+
for_each = [module.next_image_optimizer.cloudfront_cache_behavior]
4241

43-
viewer_protocol_policy = "redirect-to-https"
44-
compress = true
42+
content {
43+
allowed_methods = default_cache_behavior.value["allowed_methods"]
44+
cached_methods = default_cache_behavior.value["cached_methods"]
45+
target_origin_id = default_cache_behavior.value["target_origin_id"]
46+
47+
viewer_protocol_policy = default_cache_behavior.value["viewer_protocol_policy"]
48+
compress = default_cache_behavior.value["compress"]
4549

46-
origin_request_policy_id = module.next_image_optimizer.cloudfront_origin_request_policy_id
47-
cache_policy_id = module.next_image_optimizer.cloudfront_cache_policy_id
50+
origin_request_policy_id = default_cache_behavior.value["origin_request_policy_id"]
51+
cache_policy_id = default_cache_behavior.value["cache_policy_id"]
52+
}
4853
}
4954

5055
# This is a generic dynamic to create an origin

main.tf

+29-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ resource "random_id" "function_name" {
1818

1919
module "image_optimizer" {
2020
source = "terraform-aws-modules/lambda/aws"
21-
version = "1.47.0"
21+
version = "2.4.0"
2222

2323
function_name = random_id.function_name.hex
2424
description = var.deployment_name
@@ -42,7 +42,7 @@ module "image_optimizer" {
4242
allowed_triggers = {
4343
AllowExecutionFromAPIGateway = {
4444
service = "apigateway"
45-
arn = module.api_gateway.this_apigatewayv2_api_execution_arn
45+
arn = module.api_gateway.apigatewayv2_api_execution_arn
4646
}
4747
}
4848

@@ -60,7 +60,7 @@ module "image_optimizer" {
6060
#########################
6161
module "api_gateway" {
6262
source = "terraform-aws-modules/apigateway-v2/aws"
63-
version = "0.11.0"
63+
version = "1.1.0"
6464

6565
name = var.deployment_name
6666
description = "Managed by Terraform-next.js image optimizer"
@@ -70,7 +70,7 @@ module "api_gateway" {
7070

7171
integrations = {
7272
"GET /_next/{proxy+}" = {
73-
lambda_arn = module.image_optimizer.this_lambda_function_arn
73+
lambda_arn = module.image_optimizer.lambda_function_arn
7474
payload_format_version = "2.0"
7575
timeout_milliseconds = var.lambda_timeout * 1000
7676
}
@@ -87,6 +87,9 @@ module "api_gateway" {
8787
data "aws_region" "current" {}
8888

8989
locals {
90+
# Origin Shield
91+
###############
92+
9093
# Origin Shield mapping configuration
9194
# See: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/origin-shield.html
9295
origin_shield_region_mapping = {
@@ -104,7 +107,7 @@ locals {
104107
eu-west-2 = "eu-west-2" # Europe (London)
105108
sa-east-1 = "sa-east-1" # South America (São Paulo)
106109

107-
# Regions where Origin Shield is NOT avaible (choose closest region)
110+
# Regions where Origin Shield is NOT available (choose closest region)
108111
us-west-1 = "us-west-2" # US West (N. California)
109112
af-south-1 = "eu-west-1" # Africa (Cape Town)
110113
ap-east-1 = "ap-southeast-1" # Asia Pacific (Hong Kong)
@@ -131,15 +134,18 @@ locals {
131134
} : {}
132135

133136
# Query string parameters used by image optimizer
134-
# Must be sorted to prevent unnessesary updates of the cloudFront distribution
137+
# Must be sorted to prevent unnecessary updates of the cloudFront distribution
135138
cloudfront_allowed_query_string_keys = sort(["url", "w", "q"])
136139

137140
# Headers that are used by the image optimizer
138141
cloudfront_allowed_headers = sort(["accept", "referer"])
139142

140-
cloudfront_origin_image_optimizer = merge(
143+
144+
# CloudFront origin
145+
###################
146+
cloudfront_origin = merge(
141147
{
142-
domain_name = trimprefix(module.api_gateway.this_apigatewayv2_api_api_endpoint, "https://")
148+
domain_name = trimprefix(module.api_gateway.apigatewayv2_api_api_endpoint, "https://")
143149
origin_id = var.cloudfront_origin_id
144150

145151
custom_origin_config = {
@@ -151,6 +157,19 @@ locals {
151157
},
152158
local.cloudfront_origin_shield_config
153159
)
160+
161+
# CloudFront cache behavior
162+
###########################
163+
cloudfront_cache_behavior = {
164+
path_pattern = "/_next/image*"
165+
allowed_methods = ["GET", "HEAD"]
166+
cached_methods = ["GET", "HEAD"]
167+
target_origin_id = local.cloudfront_origin.origin_id
168+
viewer_protocol_policy = "redirect-to-https"
169+
compress = true
170+
origin_request_policy_id = aws_cloudfront_origin_request_policy.this.id
171+
cache_policy_id = aws_cloudfront_cache_policy.this.id
172+
}
154173
}
155174

156175
resource "random_id" "policy_name" {
@@ -219,10 +238,8 @@ module "cloudfront" {
219238

220239
cloudfront_create_distribution = var.cloudfront_create_distribution
221240
cloudfront_price_class = var.cloudfront_price_class
222-
cloudfront_origin = local.cloudfront_origin_image_optimizer
223-
224-
cloudfront_origin_request_policy_id = aws_cloudfront_origin_request_policy.this.id
225-
cloudfront_cache_policy_id = aws_cloudfront_cache_policy.this.id
241+
cloudfront_origin = local.cloudfront_origin
242+
cloudfront_default_behavior = local.cloudfront_cache_behavior
226243

227244
deployment_name = var.deployment_name
228245
tags = var.tags

modules/cloudfront-cache/main.tf

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@ resource "aws_cloudfront_distribution" "distribution" {
99
comment = var.deployment_name
1010
price_class = var.cloudfront_price_class
1111

12-
default_cache_behavior {
13-
allowed_methods = ["GET", "HEAD"]
14-
cached_methods = ["GET", "HEAD"]
15-
target_origin_id = lookup(var.cloudfront_origin, "origin_id", null)
12+
dynamic "default_cache_behavior" {
13+
for_each = [var.cloudfront_default_behavior]
1614

17-
viewer_protocol_policy = "redirect-to-https"
18-
compress = true
15+
content {
16+
allowed_methods = default_cache_behavior.value["allowed_methods"]
17+
cached_methods = default_cache_behavior.value["cached_methods"]
18+
target_origin_id = default_cache_behavior.value["target_origin_id"]
19+
20+
viewer_protocol_policy = default_cache_behavior.value["viewer_protocol_policy"]
21+
compress = default_cache_behavior.value["compress"]
1922

20-
origin_request_policy_id = var.cloudfront_origin_request_policy_id
21-
cache_policy_id = var.cloudfront_cache_policy_id
23+
origin_request_policy_id = default_cache_behavior.value["origin_request_policy_id"]
24+
cache_policy_id = default_cache_behavior.value["cache_policy_id"]
25+
}
2226
}
2327

2428
dynamic "origin" {

modules/cloudfront-cache/variables.tf

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ variable "cloudfront_origin" {
1010
type = any
1111
}
1212

13-
variable "cloudfront_origin_request_policy_id" {
14-
type = string
15-
}
16-
17-
variable "cloudfront_cache_policy_id" {
18-
type = string
13+
variable "cloudfront_default_behavior" {
14+
type = any
1915
}
2016

2117
variable "deployment_name" {

outputs.tf

+12-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@ output "cloudfront_origin_id" {
1313
value = var.cloudfront_origin_id
1414
}
1515

16+
output "cloudfront_origin" {
17+
description = "Predefined CloudFront origin. Can be used to embed the image optimizer into an existing CloudFront resource."
18+
value = local.cloudfront_origin
19+
}
20+
1621
output "cloudfront_origin_image_optimizer" {
17-
description = "Predefined CloudFront origin of the image optimizer. Can be used to embedd the image optimizer into an existing CloudFront resource."
18-
value = local.cloudfront_origin_image_optimizer
22+
description = "Deprecated, please use cloudfront_origin instead."
23+
value = local.cloudfront_origin
24+
}
25+
26+
output "cloudfront_cache_behavior" {
27+
description = "Predefined CloudFront cache behavior. Can be used to embed the image optimizer into an existing CloudFront resource."
28+
value = local.cloudfront_cache_behavior
1929
}
2030

2131
output "cloudfront_origin_request_policy_id" {

scripts/bundle.js

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ async function main() {
2929
'node_modules/next/node_modules/jest-worker/build/workers/threadChild.js'
3030
);
3131

32+
fileList.push(
33+
...glob.sync(
34+
'node_modules/next/dist/next-server/server/lib/squoosh/**/*.{js,wasm}',
35+
{
36+
cwd: workspaceRoot,
37+
}
38+
)
39+
);
40+
3241
// Create zip file
3342
await new Promise((resolve, reject) => {
3443
const outputFile = fs.createWriteStream(

variables.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
variable "next_image_version" {
55
description = "Next.js version from where you want to use the image optimizer from. Supports semver ranges."
66
type = string
7-
default = "10.2.3"
7+
default = "11.0.0"
88
}
99

1010
variable "next_image_domains" {

0 commit comments

Comments
 (0)