Skip to content

Commit 9ff6c63

Browse files
feat!: Bump AWS provider version to 5.82, added VPC Origin support (#153)
Co-authored-by: Anton Babenko <[email protected]>
1 parent f48225a commit 9ff6c63

File tree

12 files changed

+148
-15
lines changed

12 files changed

+148
-15
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.96.1
3+
rev: v1.96.2
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_wrapper_module_for_each

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ 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, CloudFront 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, VPC Origins, ACM Certificate, Route53 Records.
8383

8484
## Notes
8585

@@ -107,13 +107,13 @@ ordered_cache_behavior = [{
107107
| Name | Version |
108108
|------|---------|
109109
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
110-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.12.0 |
110+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.82 |
111111

112112
## Providers
113113

114114
| Name | Version |
115115
|------|---------|
116-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.12.0 |
116+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.82 |
117117

118118
## Modules
119119

@@ -127,6 +127,7 @@ No modules.
127127
| [aws_cloudfront_monitoring_subscription.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_monitoring_subscription) | resource |
128128
| [aws_cloudfront_origin_access_control.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_origin_access_control) | resource |
129129
| [aws_cloudfront_origin_access_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_origin_access_identity) | resource |
130+
| [aws_cloudfront_vpc_origin.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_vpc_origin) | resource |
130131
| [aws_cloudfront_cache_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/cloudfront_cache_policy) | data source |
131132
| [aws_cloudfront_origin_request_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/cloudfront_origin_request_policy) | data source |
132133
| [aws_cloudfront_response_headers_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/cloudfront_response_headers_policy) | data source |
@@ -142,6 +143,7 @@ No modules.
142143
| <a name="input_create_monitoring_subscription"></a> [create\_monitoring\_subscription](#input\_create\_monitoring\_subscription) | If enabled, the resource for monitoring subscription will created. | `bool` | `false` | no |
143144
| <a name="input_create_origin_access_control"></a> [create\_origin\_access\_control](#input\_create\_origin\_access\_control) | Controls if CloudFront origin access control should be created | `bool` | `false` | no |
144145
| <a name="input_create_origin_access_identity"></a> [create\_origin\_access\_identity](#input\_create\_origin\_access\_identity) | Controls if CloudFront origin access identity should be created | `bool` | `false` | no |
146+
| <a name="input_create_vpc_origin"></a> [create\_vpc\_origin](#input\_create\_vpc\_origin) | If enabled, the resource for VPC origin will be created. | `bool` | `false` | no |
145147
| <a name="input_custom_error_response"></a> [custom\_error\_response](#input\_custom\_error\_response) | One or more custom error response elements | `any` | `{}` | no |
146148
| <a name="input_default_cache_behavior"></a> [default\_cache\_behavior](#input\_default\_cache\_behavior) | The default cache behavior for this distribution | `any` | `null` | no |
147149
| <a name="input_default_root_object"></a> [default\_root\_object](#input\_default\_root\_object) | The object that you want CloudFront to return (for example, index.html) when an end user requests the root URL. | `string` | `null` | no |
@@ -161,6 +163,7 @@ No modules.
161163
| <a name="input_staging"></a> [staging](#input\_staging) | Whether the distribution is a staging distribution. | `bool` | `false` | no |
162164
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to assign to the resource. | `map(string)` | `null` | no |
163165
| <a name="input_viewer_certificate"></a> [viewer\_certificate](#input\_viewer\_certificate) | The SSL configuration for this distribution | `any` | <pre>{<br/> "cloudfront_default_certificate": true,<br/> "minimum_protocol_version": "TLSv1"<br/>}</pre> | no |
166+
| <a name="input_vpc_origin"></a> [vpc\_origin](#input\_vpc\_origin) | Map of CloudFront VPC origin | <pre>map(object({<br/> name = string<br/> arn = string<br/> http_port = number<br/> https_port = number<br/> origin_protocol_policy = string<br/> origin_ssl_protocols = object({<br/> items = list(string)<br/> quantity = number<br/> })<br/> }))</pre> | `{}` | no |
164167
| <a name="input_wait_for_deployment"></a> [wait\_for\_deployment](#input\_wait\_for\_deployment) | If enabled, the resource will wait for the distribution status to change from InProgress to Deployed. Setting this to false will skip the process. | `bool` | `true` | no |
165168
| <a name="input_web_acl_id"></a> [web\_acl\_id](#input\_web\_acl\_id) | If you're using AWS WAF to filter CloudFront requests, the Id of the AWS WAF web ACL that is associated with the distribution. The WAF Web ACL must exist in the WAF Global (CloudFront) region and the credentials configuring this argument must have waf:GetWebACL permissions assigned. If using WAFv2, provide the ARN of the web ACL. | `string` | `null` | no |
166169

@@ -185,6 +188,7 @@ No modules.
185188
| <a name="output_cloudfront_origin_access_identities"></a> [cloudfront\_origin\_access\_identities](#output\_cloudfront\_origin\_access\_identities) | The origin access identities created |
186189
| <a name="output_cloudfront_origin_access_identity_iam_arns"></a> [cloudfront\_origin\_access\_identity\_iam\_arns](#output\_cloudfront\_origin\_access\_identity\_iam\_arns) | The IAM arns of the origin access identities created |
187190
| <a name="output_cloudfront_origin_access_identity_ids"></a> [cloudfront\_origin\_access\_identity\_ids](#output\_cloudfront\_origin\_access\_identity\_ids) | The IDS of the origin access identities created |
191+
| <a name="output_cloudfront_vpc_origin_ids"></a> [cloudfront\_vpc\_origin\_ids](#output\_cloudfront\_vpc\_origin\_ids) | The IDS of the VPC origin created |
188192
<!-- END_TF_DOCS -->
189193

190194
## Authors

examples/complete/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Configuration in this directory creates CloudFront distribution which demos such
88
- Lambda@Edge
99
- ACM certificate
1010
- Route53 record
11+
- VPC Origins
1112

1213
## Usage
1314

@@ -27,15 +28,15 @@ Note that this example may create resources which cost money. Run `terraform des
2728
| Name | Version |
2829
|------|---------|
2930
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
30-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.29 |
31+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.82 |
3132
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2.0 |
3233
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
3334

3435
## Providers
3536

3637
| Name | Version |
3738
|------|---------|
38-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.29 |
39+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.82 |
3940
| <a name="provider_null"></a> [null](#provider\_null) | >= 2.0 |
4041
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |
4142

@@ -45,6 +46,7 @@ Note that this example may create resources which cost money. Run `terraform des
4546
|------|--------|---------|
4647
| <a name="module_acm"></a> [acm](#module\_acm) | terraform-aws-modules/acm/aws | ~> 4.0 |
4748
| <a name="module_cloudfront"></a> [cloudfront](#module\_cloudfront) | ../../ | n/a |
49+
| <a name="module_ec2"></a> [ec2](#module\_ec2) | terraform-aws-modules/ec2-instance/aws | ~> 5.0 |
4850
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 7.0 |
4951
| <a name="module_log_bucket"></a> [log\_bucket](#module\_log\_bucket) | terraform-aws-modules/s3-bucket/aws | ~> 4.0 |
5052
| <a name="module_records"></a> [records](#module\_records) | terraform-aws-modules/route53/aws//modules/records | ~> 2.0 |
@@ -58,6 +60,7 @@ Note that this example may create resources which cost money. Run `terraform des
5860
| [aws_s3_bucket_policy.bucket_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
5961
| [null_resource.download_package](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
6062
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
63+
| [aws_ami.al2023](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
6164
| [aws_canonical_user_id.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/canonical_user_id) | data source |
6265
| [aws_cloudfront_log_delivery_canonical_user_id.cloudfront](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/cloudfront_log_delivery_canonical_user_id) | data source |
6366
| [aws_iam_policy_document.s3_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -84,4 +87,5 @@ No inputs.
8487
| <a name="output_cloudfront_origin_access_identities"></a> [cloudfront\_origin\_access\_identities](#output\_cloudfront\_origin\_access\_identities) | The origin access identities created |
8588
| <a name="output_cloudfront_origin_access_identity_iam_arns"></a> [cloudfront\_origin\_access\_identity\_iam\_arns](#output\_cloudfront\_origin\_access\_identity\_iam\_arns) | The IAM arns of the origin access identities created |
8689
| <a name="output_cloudfront_origin_access_identity_ids"></a> [cloudfront\_origin\_access\_identity\_ids](#output\_cloudfront\_origin\_access\_identity\_ids) | The IDS of the origin access identities created |
90+
| <a name="output_cloudfront_vpc_origin_ids"></a> [cloudfront\_vpc\_origin\_ids](#output\_cloudfront\_vpc\_origin\_ids) | The IDS of the VPC origin created |
8791
<!-- END_TF_DOCS -->

examples/complete/main.tf

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ module "cloudfront" {
5353
}
5454
}
5555

56+
create_vpc_origin = true
57+
vpc_origin = {
58+
ec2_vpc_origin = {
59+
name = random_pet.this.id
60+
arn = module.ec2.arn
61+
http_port = 80
62+
https_port = 443
63+
origin_protocol_policy = "http-only"
64+
origin_ssl_protocols = {
65+
items = ["TLSv1.2"]
66+
quantity = 1
67+
}
68+
}
69+
}
70+
5671
logging_config = {
5772
bucket = module.log_bucket.s3_bucket_bucket_domain_name
5873
prefix = "cloudfront"
@@ -98,6 +113,14 @@ module "cloudfront" {
98113
origin_access_control = "s3_oac" # key in `origin_access_control`
99114
# origin_access_control_id = "E345SXM82MIOSU" # external OAС resource
100115
}
116+
117+
ec2_vpc_origin = {
118+
domain_name = module.ec2.private_dns
119+
vpc_origin_config = {
120+
vpc_origin = "ec2_vpc_origin" # key in `vpc_origin`
121+
# vpc_origin_id = "vo_Cg6A14otX0DB1yyDQ6Nond" # external VPC Origin resource
122+
}
123+
}
101124
}
102125

103126
origin_group = {
@@ -170,7 +193,16 @@ module "cloudfront" {
170193
# Using Cache/ResponseHeaders/OriginRequest policies is not allowed together with `compress` and `query_string` settings
171194
compress = true
172195
query_string = true
196+
},
197+
{
198+
path_pattern = "/vpc-origin/*"
199+
target_origin_id = "ec2_vpc_origin"
200+
viewer_protocol_policy = "redirect-to-https"
201+
202+
allowed_methods = ["GET", "HEAD", "OPTIONS"]
203+
cached_methods = ["GET", "HEAD"]
173204
}
205+
174206
]
175207

176208
viewer_certificate = {
@@ -316,6 +348,10 @@ module "records" {
316348
]
317349
}
318350

351+
#########################################
352+
# S3 bucket policy
353+
#########################################
354+
319355
data "aws_iam_policy_document" "s3_policy" {
320356
# Origin Access Identities
321357
statement {
@@ -351,16 +387,41 @@ resource "aws_s3_bucket_policy" "bucket_policy" {
351387
policy = data.aws_iam_policy_document.s3_policy.json
352388
}
353389

390+
#########################################
391+
# CloudFront function
392+
#########################################
393+
394+
resource "aws_cloudfront_function" "example" {
395+
name = "example-${random_pet.this.id}"
396+
runtime = "cloudfront-js-1.0"
397+
code = file("${path.module}/example-function.js")
398+
}
399+
400+
#########################################
401+
# EC2 instance for CloudFront VPC origin
402+
#########################################
403+
404+
data "aws_ami" "al2023" {
405+
most_recent = true
406+
owners = ["amazon"]
407+
408+
filter {
409+
name = "name"
410+
values = ["al2023-ami-2023*-x86_64"]
411+
}
412+
}
413+
414+
module "ec2" {
415+
source = "terraform-aws-modules/ec2-instance/aws"
416+
version = "~> 5.0"
417+
418+
ami = data.aws_ami.al2023.id
419+
}
420+
354421
########
355422
# Extra
356423
########
357424

358425
resource "random_pet" "this" {
359426
length = 2
360427
}
361-
362-
resource "aws_cloudfront_function" "example" {
363-
name = "example-${random_pet.this.id}"
364-
runtime = "cloudfront-js-1.0"
365-
code = file("${path.module}/example-function.js")
366-
}

examples/complete/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,8 @@ output "cloudfront_origin_access_identity_iam_arns" {
6262
description = "The IAM arns of the origin access identities created"
6363
value = module.cloudfront.cloudfront_origin_access_identity_iam_arns
6464
}
65+
66+
output "cloudfront_vpc_origin_ids" {
67+
description = "The IDS of the VPC origin created"
68+
value = module.cloudfront.cloudfront_vpc_origin_ids
69+
}

examples/complete/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.29"
7+
version = ">= 5.82"
88
}
99
random = {
1010
source = "hashicorp/random"

main.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
locals {
22
create_origin_access_identity = var.create_origin_access_identity && length(keys(var.origin_access_identities)) > 0
33
create_origin_access_control = var.create_origin_access_control && length(keys(var.origin_access_control)) > 0
4+
create_vpc_origin = var.create_vpc_origin && length(keys(var.vpc_origin)) > 0
45
}
56

67
resource "aws_cloudfront_origin_access_identity" "this" {
@@ -24,6 +25,25 @@ resource "aws_cloudfront_origin_access_control" "this" {
2425
signing_protocol = each.value["signing_protocol"]
2526
}
2627

28+
resource "aws_cloudfront_vpc_origin" "this" {
29+
for_each = local.create_vpc_origin ? var.vpc_origin : {}
30+
31+
vpc_origin_endpoint_config {
32+
name = each.value["name"]
33+
arn = each.value["arn"]
34+
http_port = each.value["http_port"]
35+
https_port = each.value["https_port"]
36+
origin_protocol_policy = each.value["origin_protocol_policy"]
37+
38+
origin_ssl_protocols {
39+
items = each.value.origin_ssl_protocols.items
40+
quantity = each.value.origin_ssl_protocols.quantity
41+
}
42+
}
43+
44+
tags = var.tags
45+
}
46+
2747
resource "aws_cloudfront_distribution" "this" {
2848
count = var.create_distribution ? 1 : 0
2949

@@ -100,6 +120,16 @@ resource "aws_cloudfront_distribution" "this" {
100120
origin_shield_region = origin_shield.value.origin_shield_region
101121
}
102122
}
123+
124+
dynamic "vpc_origin_config" {
125+
for_each = length(keys(lookup(origin.value, "vpc_origin_config", {}))) == 0 ? [] : [lookup(origin.value, "vpc_origin_config", {})]
126+
127+
content {
128+
vpc_origin_id = lookup(vpc_origin_config.value, "vpc_origin_id", lookup(lookup(aws_cloudfront_vpc_origin.this, lookup(vpc_origin_config.value, "vpc_origin", ""), {}), "id", null))
129+
origin_keepalive_timeout = lookup(vpc_origin_config.value, "origin_keepalive_timeout", null)
130+
origin_read_timeout = lookup(vpc_origin_config.value, "origin_read_timeout", null)
131+
}
132+
}
103133
}
104134
}
105135

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,8 @@ output "cloudfront_origin_access_controls_ids" {
8282
description = "The IDS of the origin access identities created"
8383
value = local.create_origin_access_control ? [for v in aws_cloudfront_origin_access_control.this : v.id] : []
8484
}
85+
86+
output "cloudfront_vpc_origin_ids" {
87+
description = "The IDS of the VPC origin created"
88+
value = local.create_vpc_origin ? [for v in aws_cloudfront_vpc_origin.this : v.id] : []
89+
}

variables.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,25 @@ variable "realtime_metrics_subscription_status" {
181181
type = string
182182
default = "Enabled"
183183
}
184+
185+
variable "create_vpc_origin" {
186+
description = "If enabled, the resource for VPC origin will be created."
187+
type = bool
188+
default = false
189+
}
190+
191+
variable "vpc_origin" {
192+
description = "Map of CloudFront VPC origin"
193+
type = map(object({
194+
name = string
195+
arn = string
196+
http_port = number
197+
https_port = number
198+
origin_protocol_policy = string
199+
origin_ssl_protocols = object({
200+
items = list(string)
201+
quantity = number
202+
})
203+
}))
204+
default = {}
205+
}

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 5.12.0"
7+
version = ">= 5.82"
88
}
99
}
1010
}

wrappers/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module "wrapper" {
1010
create_monitoring_subscription = try(each.value.create_monitoring_subscription, var.defaults.create_monitoring_subscription, false)
1111
create_origin_access_control = try(each.value.create_origin_access_control, var.defaults.create_origin_access_control, false)
1212
create_origin_access_identity = try(each.value.create_origin_access_identity, var.defaults.create_origin_access_identity, false)
13+
create_vpc_origin = try(each.value.create_vpc_origin, var.defaults.create_vpc_origin, false)
1314
custom_error_response = try(each.value.custom_error_response, var.defaults.custom_error_response, {})
1415
default_cache_behavior = try(each.value.default_cache_behavior, var.defaults.default_cache_behavior, null)
1516
default_root_object = try(each.value.default_root_object, var.defaults.default_root_object, null)
@@ -39,6 +40,7 @@ module "wrapper" {
3940
cloudfront_default_certificate = true
4041
minimum_protocol_version = "TLSv1"
4142
})
43+
vpc_origin = try(each.value.vpc_origin, var.defaults.vpc_origin, {})
4244
wait_for_deployment = try(each.value.wait_for_deployment, var.defaults.wait_for_deployment, true)
4345
web_acl_id = try(each.value.web_acl_id, var.defaults.web_acl_id, null)
4446
}

wrappers/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 5.12.0"
7+
version = ">= 5.82"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)