Skip to content

Commit f6e3d6f

Browse files
Merge commit '78ab4d0f60d3e4c32aeb6f782168377c218f667c' into release
2 parents 885d9de + 78ab4d0 commit f6e3d6f

File tree

10 files changed

+228
-16
lines changed

10 files changed

+228
-16
lines changed

.github/workflows/tf-docs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ jobs:
1212
update-docs:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v2
15+
- uses: actions/checkout@v3
1616
- name: Update Terraform docs in README
17-
uses: terraform-docs/gh-actions@v0.10.0
17+
uses: terraform-docs/gh-actions@v1.0.0
1818
with:
1919
config-file: '.terraform-docs.yml'
2020
output-file: README.md

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## [Unreleased]
44

5+
## [12.1.3] - 2022-08-12
6+
7+
- Uses Image Optimizer module of [Next.js 12.1.3](https://github.com/vercel/next.js/releases/tag/v12.1.3) ([#121](https://github.com/milliHQ/terraform-aws-next-js-image-optimization/pull/121))
8+
- Updates sharp from `v0.30.3` to [`v0.30.7`](https://github.com/lovell/sharp/releases/tag/v0.30.7) ([#136](https://github.com/milliHQ/terraform-aws-next-js-image-optimization/pull/136))
9+
- Support usage of custom domain and SSL certificate with the default CloudFront distribution ([#134](https://github.com/milliHQ/terraform-aws-next-js-image-optimization/pull/134))
10+
- Minimum protocol version for the default CloudFront distribution can now set using `cloudfront_minimum_protocol_version` variable. New default value is `TLSv1` (was `TLSv1.2_2018` before) ([#134](https://github.com/milliHQ/terraform-aws-next-js-image-optimization/pull/134))
11+
512
## [12.1.2] - 2022-04-16
613

714
- Uses Image Optimizer module of [Next.js 12.1.2](https://github.com/vercel/next.js/releases/tag/v12.1.2) ([#118](https://github.com/milliHQ/terraform-aws-next-js-image-optimization/pull/118))

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ Then rebuild and redeploy your Next.js application to make use of the changed co
104104
Use the image optimizer with an existing CloudFront distribution.
105105

106106
<!-- prettier-ignore-start -->
107-
<!--- STOP: Auto generated values - Make no manual edits here --->
108-
<!--- BEGIN_TF_DOCS --->
107+
<!-- STOP: Auto generated values - Make no manual edits here -->
108+
<!-- BEGIN_TF_DOCS -->
109109
## Requirements
110110

111111
| Name | Version |
@@ -123,10 +123,13 @@ Then rebuild and redeploy your Next.js application to make use of the changed co
123123

124124
| Name | Description | Type | Default | Required |
125125
|------|-------------|------|---------|:--------:|
126+
| cloudfront\_acm\_certificate\_arn | CloudFront ACM certificate to use. | `string` | `null` | no |
127+
| cloudfront\_aliases | Custom domain(s) for CloudFront. | `list(string)` | `[]` | no |
126128
| cloudfront\_create\_distribution | Controls whether a CloudFront distribution should be created. | `bool` | `true` | no |
127129
| cloudfront\_enable\_origin\_shield | Controls whether CloudFront Origin Shield should be enabled on the image optimizer lambdas. | `bool` | `true` | no |
130+
| cloudfront\_minimum\_protocol\_version | The minimum version of the SSL protocol that you want CloudFront to use for HTTPS connections. One of SSLv3, TLSv1, TLSv1\_2016, TLSv1.1\_2016, TLSv1.2\_2018 TLSv1.2\_2019 or TLSv1.2\_2021. | `string` | `"TLSv1"` | no |
128131
| cloudfront\_origin\_id | Override the id for the custom CloudFront id. | `string` | `"tf-next-image-optimizer"` | no |
129-
| cloudfront\_origin\_shield\_region | Override the region choosen for the CloudFront origin shield. Use `auto` to automatically determine the optimal region. | `string` | `"auto"` | no |
132+
| cloudfront\_origin\_shield\_region | Override the region chosen for the CloudFront origin shield. Use `auto` to automatically determine the optimal region. | `string` | `"auto"` | no |
130133
| cloudfront\_price\_class | Price class for the CloudFront distribution. One of PriceClass\_All, PriceClass\_200, PriceClass\_100. | `string` | `"PriceClass_100"` | no |
131134
| debug\_use\_local\_packages | (Debug) Use local packages instead of downloading them from npm. | `bool` | `false` | no |
132135
| deployment\_name | Identifier for the deployment group (only lowercase alphanumeric characters and hyphens are allowed). | `string` | `"tf-next-image"` | no |
@@ -142,7 +145,7 @@ Then rebuild and redeploy your Next.js application to make use of the changed co
142145
| next\_image\_domains | Allowed origin domains that can be used for fetching images. | `list(string)` | `[]` | no |
143146
| next\_image\_formats | If the Accept head matches more than one of the configured formats, the first match in the array is used. Therefore, the array order matters. If there is no match, the Image Optimization API will fallback to the original image's format. | `list(string)` | <pre>[<br> "image/webp"<br>]</pre> | no |
144147
| next\_image\_image\_sizes | Allowed image sizes that should be used for image optimization. | `list(number)` | `null` | no |
145-
| next\_image\_version | Next.js version from where you want to use the image optimizer from. Supports semver ranges. | `string` | `"12.1.2"` | no |
148+
| next\_image\_version | Next.js version from where you want to use the image optimizer from. Supports semver ranges. | `string` | `"12.1.3"` | no |
146149
| 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 |
147150
| tags | Tag metadata to label AWS resources that support tags. | `map(string)` | `{}` | no |
148151

@@ -157,8 +160,7 @@ Then rebuild and redeploy your Next.js application to make use of the changed co
157160
| cloudfront\_origin | Predefined CloudFront origin. Can be used to embed the image optimizer into an existing CloudFront resource. |
158161
| cloudfront\_origin\_id | Id of the custom origin used for image optimization. |
159162
| cloudfront\_origin\_request\_policy\_id | Request policy id used for image optimization. |
160-
161-
<!--- END_TF_DOCS --->
163+
<!-- END_TF_DOCS -->
162164
<!-- prettier-ignore-end -->
163165

164166
## Limits
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Custom Domain Example
2+
3+
This example shows how to configure a custom domain for the [Terraform Next.js Image Optimization module for AWS](https://github.com/milliHQ/terraform-aws-next-js-image-optimization).
4+
5+
> **Note:** The full example code is available on [GitHub](https://github.com/milliHQ/terraform-aws-next-js-image-optimization/tree/main/examples/with-custom-domain)
6+
7+
## Setup the Terraform module
8+
9+
1. Create a new file called `main.tf` and paste the code from the following source: [main.tf on GitHub](https://github.com/milliHQ/terraform-aws-next-js-image-optimization/blob/main/examples/with-custom-domain/main.tf):
10+
11+
2. Then configure the domain you want to use:
12+
13+
```tf
14+
# main.tf
15+
16+
...
17+
###########
18+
# Variables
19+
###########
20+
21+
variable "custom_domain" {
22+
description = "Your custom domain"
23+
type = string
24+
default = "example.com"
25+
}
26+
27+
variable "custom_domain_zone_name" {
28+
description = "The Route53 zone name of the custom domain"
29+
type = string
30+
default = "example.com."
31+
}
32+
33+
...
34+
```
35+
36+
3. Run Terraform to deploy the image optimizer to your AWS account:
37+
38+
```sh
39+
terraform init # Only needed on the first time running Terraform
40+
41+
terraform plan # (Optional) See what resources Terraform will create
42+
terraform apply # Deploy the image optimizer module to your AWS account
43+
```
44+
45+
After Terraform has successfully created all resources in your AWS account, you should see the following output on the terminal:
46+
47+
```sh
48+
> Apply complete!
49+
>
50+
> Outputs:
51+
>
52+
> custom_domain = "example.com"
53+
> cloudfront_domain = "<distribution-id>.cloudfront.net"
54+
```
55+
56+
4. Integrate with Next.js by editing `next.config.js`:
57+
58+
```diff
59+
// next.config.js
60+
61+
module.exports = {
62+
+ images: {
63+
+ path: 'https://example.com/_next/image'
64+
+ },
65+
}
66+
```
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 4.0"
6+
}
7+
}
8+
}
9+
10+
# AWS region where the image optimizer should be deployed to.
11+
# While the optimized images are cached globally (via CloudFront CDN) this
12+
# determines the location where the optimization (Lambda) should happen, when
13+
# the cache is missed.
14+
provider "aws" {
15+
region = "us-east-1"
16+
}
17+
18+
###########
19+
# Variables
20+
###########
21+
22+
variable "custom_domain" {
23+
description = "Your custom domain"
24+
type = string
25+
default = "example.com"
26+
}
27+
28+
# Assuming that the hosted ZONE of your domain is already registered in your
29+
# AWS account (Route 53)
30+
# https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/AboutHZWorkingWith.html
31+
variable "custom_domain_zone_name" {
32+
description = "The Route53 zone name of the custom domain"
33+
type = string
34+
default = "example.com."
35+
}
36+
37+
# Get the hosted zone for the custom domain
38+
data "aws_route53_zone" "custom_domain_zone" {
39+
name = var.custom_domain_zone_name
40+
}
41+
42+
# Create a new record in Route 53 for the domain
43+
resource "aws_route53_record" "cloudfront_alias_domain" {
44+
zone_id = data.aws_route53_zone.custom_domain_zone.zone_id
45+
name = each.key
46+
type = "A"
47+
48+
alias {
49+
name = module.next_image_optimizer.cloudfront_domain_name
50+
zone_id = module.next_image_optimizer.cloudfront_hosted_zone_id
51+
evaluate_target_health = false
52+
}
53+
}
54+
55+
##########
56+
# SSL Cert
57+
##########
58+
59+
# Creates a free SSL certificate for CloudFront distribution
60+
# For more options (e.g. multiple domains) see:
61+
# https://registry.terraform.io/modules/terraform-aws-modules/acm/aws/
62+
module "cloudfront_cert" {
63+
source = "terraform-aws-modules/acm/aws"
64+
version = "~> 3.0"
65+
66+
domain_name = var.custom_domain
67+
zone_id = data.aws_route53_zone.custom_domain_zone.zone_id
68+
subject_alternative_names = slice(local.aliases, 1, length(local.aliases))
69+
70+
tags = {
71+
Name = "CloudFront ${var.custom_domain}"
72+
}
73+
74+
# CloudFront works only with certs stored in us-east-1
75+
providers = {
76+
aws = aws.global_region
77+
}
78+
}
79+
80+
#######################
81+
# Route53 Domain record
82+
#######################
83+
84+
module "next_image_optimizer" {
85+
source = "milliHQ/next-js-image-optimization/aws"
86+
87+
cloudfront_aliases = [var.custom_domain]
88+
cloudfront_acm_certificate_arn = module.cloudfront_cert.acm_certificate_arn
89+
next_image_domains = ["assets.vercel.com"]
90+
}
91+
92+
output "custom_domain" {
93+
value = var.custom_domain
94+
}
95+
96+
output "cloudfront_domain" {
97+
value = module.next_image_optimizer.cloudfront_domain_name
98+
}

examples/with-next-js/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
aws = {
44
source = "hashicorp/aws"
5-
version = "~> 3.0"
5+
version = "~> 4.0"
66
}
77
}
88
}

main.tf

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,13 @@ resource "aws_cloudfront_cache_policy" "this" {
239239
module "cloudfront" {
240240
source = "./modules/cloudfront-cache"
241241

242-
cloudfront_create_distribution = var.cloudfront_create_distribution
243-
cloudfront_price_class = var.cloudfront_price_class
244-
cloudfront_origin = local.cloudfront_origin
245-
cloudfront_default_behavior = local.cloudfront_cache_behavior
242+
cloudfront_create_distribution = var.cloudfront_create_distribution
243+
cloudfront_price_class = var.cloudfront_price_class
244+
cloudfront_origin = local.cloudfront_origin
245+
cloudfront_default_behavior = local.cloudfront_cache_behavior
246+
cloudfront_minimum_protocol_version = var.cloudfront_minimum_protocol_version
247+
cloudfront_acm_certificate_arn = var.cloudfront_acm_certificate_arn
248+
cloudfront_aliases = var.cloudfront_aliases
246249

247250
deployment_name = var.deployment_name
248251
tags = var.tags

modules/cloudfront-cache/main.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ resource "aws_cloudfront_distribution" "distribution" {
88
is_ipv6_enabled = true
99
comment = var.deployment_name
1010
price_class = var.cloudfront_price_class
11+
aliases = var.cloudfront_aliases
1112

1213
dynamic "default_cache_behavior" {
1314
for_each = [var.cloudfront_default_behavior]
@@ -68,7 +69,10 @@ resource "aws_cloudfront_distribution" "distribution" {
6869
}
6970

7071
viewer_certificate {
71-
cloudfront_default_certificate = true
72+
acm_certificate_arn = var.cloudfront_acm_certificate_arn
73+
cloudfront_default_certificate = var.cloudfront_acm_certificate_arn == null
74+
minimum_protocol_version = var.cloudfront_minimum_protocol_version
75+
ssl_support_method = var.cloudfront_acm_certificate_arn != null ? "sni-only" : null
7276
}
7377

7478
restrictions {

modules/cloudfront-cache/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@ variable "deployment_name" {
2121
variable "tags" {
2222
type = map(string)
2323
}
24+
25+
variable "cloudfront_minimum_protocol_version" {
26+
type = string
27+
}
28+
29+
variable "cloudfront_acm_certificate_arn" {
30+
type = string
31+
default = null
32+
}
33+
34+
variable "cloudfront_aliases" {
35+
type = list(string)
36+
default = []
37+
}

variables.tf

Lines changed: 20 additions & 2 deletions
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 = "12.1.2"
7+
default = "12.1.3"
88
}
99

1010
variable "next_image_base_origin" {
@@ -118,7 +118,7 @@ variable "cloudfront_enable_origin_shield" {
118118
}
119119

120120
variable "cloudfront_origin_shield_region" {
121-
description = "Override the region choosen for the CloudFront origin shield. Use `auto` to automatically determine the optimal region."
121+
description = "Override the region chosen for the CloudFront origin shield. Use `auto` to automatically determine the optimal region."
122122
type = string
123123
default = "auto"
124124
}
@@ -129,6 +129,24 @@ variable "cloudfront_origin_id" {
129129
default = "tf-next-image-optimizer"
130130
}
131131

132+
variable "cloudfront_minimum_protocol_version" {
133+
description = "The minimum version of the SSL protocol that you want CloudFront to use for HTTPS connections. One of SSLv3, TLSv1, TLSv1_2016, TLSv1.1_2016, TLSv1.2_2018 TLSv1.2_2019 or TLSv1.2_2021."
134+
type = string
135+
default = "TLSv1"
136+
}
137+
138+
variable "cloudfront_acm_certificate_arn" {
139+
description = "CloudFront ACM certificate to use."
140+
type = string
141+
default = null
142+
}
143+
144+
variable "cloudfront_aliases" {
145+
description = "Custom domain(s) for CloudFront."
146+
type = list(string)
147+
default = []
148+
}
149+
132150
##########
133151
# Labeling
134152
##########

0 commit comments

Comments
 (0)