Skip to content

Commit 8440b30

Browse files
authored
Multi distribution support
Only keeping what's needed for personal use.
1 parent d66669f commit 8440b30

File tree

4 files changed

+203
-353
lines changed

4 files changed

+203
-353
lines changed

main.tf

Lines changed: 43 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,23 @@
1-
locals {
2-
create_origin_access_identity = var.create_origin_access_identity && length(keys(var.origin_access_identities)) > 0
3-
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
5-
}
6-
7-
resource "aws_cloudfront_origin_access_identity" "this" {
8-
for_each = local.create_origin_access_identity ? var.origin_access_identities : {}
9-
10-
comment = each.value
11-
12-
lifecycle {
13-
create_before_destroy = true
14-
}
15-
}
16-
17-
resource "aws_cloudfront_origin_access_control" "this" {
18-
for_each = local.create_origin_access_control ? var.origin_access_control : {}
19-
20-
name = each.key
21-
22-
description = each.value["description"]
23-
origin_access_control_origin_type = each.value["origin_type"]
24-
signing_behavior = each.value["signing_behavior"]
25-
signing_protocol = each.value["signing_protocol"]
26-
}
27-
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-
471
resource "aws_cloudfront_distribution" "this" {
48-
count = var.create_distribution ? 1 : 0
49-
50-
aliases = var.aliases
51-
comment = var.comment
52-
continuous_deployment_policy_id = var.continuous_deployment_policy_id
53-
default_root_object = var.default_root_object
54-
enabled = var.enabled
55-
http_version = var.http_version
56-
is_ipv6_enabled = var.is_ipv6_enabled
57-
price_class = var.price_class
58-
retain_on_delete = var.retain_on_delete
59-
staging = var.staging
60-
wait_for_deployment = var.wait_for_deployment
61-
web_acl_id = var.web_acl_id
62-
tags = var.tags
2+
#count = each.value.create_distribution ? 1 : 0
3+
for_each = { for idx, config in var.distributions : idx => config }
4+
5+
aliases = each.value.aliases
6+
comment = each.value.comment
7+
continuous_deployment_policy_id = each.value.continuous_deployment_policy_id
8+
default_root_object = each.value.default_root_object
9+
enabled = each.value.enabled
10+
http_version = each.value.http_version
11+
is_ipv6_enabled = each.value.is_ipv6_enabled
12+
price_class = each.value.price_class
13+
retain_on_delete = each.value.retain_on_delete
14+
staging = each.value.staging
15+
wait_for_deployment = each.value.wait_for_deployment
16+
web_acl_id = each.value.web_acl_id
17+
tags = each.value.tags
6318

6419
dynamic "logging_config" {
65-
for_each = length(keys(var.logging_config)) == 0 ? [] : [var.logging_config]
20+
for_each = length(keys(each.value.logging_config)) == 0 ? [] : [each.value.logging_config]
6621

6722
content {
6823
bucket = logging_config.value["bucket"]
@@ -72,7 +27,7 @@ resource "aws_cloudfront_distribution" "this" {
7227
}
7328

7429
dynamic "origin" {
75-
for_each = var.origin
30+
for_each = each.value.origin
7631

7732
content {
7833
domain_name = origin.value.domain_name
@@ -82,14 +37,6 @@ resource "aws_cloudfront_distribution" "this" {
8237
connection_timeout = lookup(origin.value, "connection_timeout", null)
8338
origin_access_control_id = lookup(origin.value, "origin_access_control_id", lookup(lookup(aws_cloudfront_origin_access_control.this, lookup(origin.value, "origin_access_control", ""), {}), "id", null))
8439

85-
dynamic "s3_origin_config" {
86-
for_each = length(keys(lookup(origin.value, "s3_origin_config", {}))) == 0 ? [] : [lookup(origin.value, "s3_origin_config", {})]
87-
88-
content {
89-
origin_access_identity = lookup(s3_origin_config.value, "cloudfront_access_identity_path", lookup(lookup(aws_cloudfront_origin_access_identity.this, lookup(s3_origin_config.value, "origin_access_identity", ""), {}), "cloudfront_access_identity_path", null))
90-
}
91-
}
92-
9340
dynamic "custom_origin_config" {
9441
for_each = length(lookup(origin.value, "custom_origin_config", "")) == 0 ? [] : [lookup(origin.value, "custom_origin_config", "")]
9542

@@ -120,21 +67,11 @@ resource "aws_cloudfront_distribution" "this" {
12067
origin_shield_region = origin_shield.value.origin_shield_region
12168
}
12269
}
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-
}
13370
}
13471
}
13572

13673
dynamic "origin_group" {
137-
for_each = var.origin_group
74+
for_each = each.value.origin_group != null ? each.value.origin_group : {}
13875

13976
content {
14077
origin_id = lookup(origin_group.value, "origin_id", origin_group.key)
@@ -154,7 +91,7 @@ resource "aws_cloudfront_distribution" "this" {
15491
}
15592

15693
dynamic "default_cache_behavior" {
157-
for_each = [var.default_cache_behavior]
94+
for_each = [each.value.default_cache_behavior]
15895
iterator = i
15996

16097
content {
@@ -214,18 +151,11 @@ resource "aws_cloudfront_distribution" "this" {
214151
function_arn = f.value.function_arn
215152
}
216153
}
217-
218-
dynamic "grpc_config" {
219-
for_each = try([i.value.grpc_config], [])
220-
content {
221-
enabled = grpc_config.value.enabled
222-
}
223-
}
224154
}
225155
}
226156

227157
dynamic "ordered_cache_behavior" {
228-
for_each = var.ordered_cache_behavior
158+
for_each = each.value.ordered_cache_behavior != null ? each.value.ordered_cache_behavior : []
229159
iterator = i
230160

231161
content {
@@ -286,27 +216,36 @@ resource "aws_cloudfront_distribution" "this" {
286216
function_arn = f.value.function_arn
287217
}
288218
}
219+
}
220+
}
289221

290-
dynamic "grpc_config" {
291-
for_each = try([i.value.grpc_config], [])
292-
content {
293-
enabled = grpc_config.value.enabled
294-
}
295-
}
222+
/*
223+
dynamic "viewer_certificate" {
224+
for_each = each.value.viewer_certificate
225+
iterator = k
226+
227+
content {
228+
acm_certificate_arn = lookup(k.value, "acm_certificate_arn", [])
229+
cloudfront_default_certificate = lookup(k.value, "cloudfront_default_certificate", false)
230+
iam_certificate_id = lookup(k.value, "iam_certificate_id", null)
231+
minimum_protocol_version = lookup(k.value, "minimum_protocol_version", "TLSv1.2_2018")
232+
ssl_support_method = lookup(k.value, "ssl_support_method", "sni-only")
296233
}
297234
}
235+
*/
298236

299237
viewer_certificate {
300-
acm_certificate_arn = lookup(var.viewer_certificate, "acm_certificate_arn", null)
301-
cloudfront_default_certificate = lookup(var.viewer_certificate, "cloudfront_default_certificate", null)
302-
iam_certificate_id = lookup(var.viewer_certificate, "iam_certificate_id", null)
238+
acm_certificate_arn = lookup(each.value.viewer_certificate, "acm_certificate_arn", null)
239+
cloudfront_default_certificate = lookup(each.value.viewer_certificate, "cloudfront_default_certificate", null)
240+
iam_certificate_id = lookup(each.value.viewer_certificate, "iam_certificate_id", null)
303241

304-
minimum_protocol_version = lookup(var.viewer_certificate, "minimum_protocol_version", "TLSv1")
305-
ssl_support_method = lookup(var.viewer_certificate, "ssl_support_method", null)
242+
minimum_protocol_version = lookup(each.value.viewer_certificate, "minimum_protocol_version", "TLSv1")
243+
ssl_support_method = lookup(each.value.viewer_certificate, "ssl_support_method", null)
306244
}
307245

246+
308247
dynamic "custom_error_response" {
309-
for_each = length(flatten([var.custom_error_response])[0]) > 0 ? flatten([var.custom_error_response]) : []
248+
for_each = length(each.value.custom_error_response) > 0 ? each.value.custom_error_response : []
310249

311250
content {
312251
error_code = custom_error_response.value["error_code"]
@@ -319,7 +258,7 @@ resource "aws_cloudfront_distribution" "this" {
319258

320259
restrictions {
321260
dynamic "geo_restriction" {
322-
for_each = [var.geo_restriction]
261+
for_each = [each.value.geo_restriction]
323262

324263
content {
325264
restriction_type = lookup(geo_restriction.value, "restriction_type", "none")
@@ -329,18 +268,6 @@ resource "aws_cloudfront_distribution" "this" {
329268
}
330269
}
331270

332-
resource "aws_cloudfront_monitoring_subscription" "this" {
333-
count = var.create_distribution && var.create_monitoring_subscription ? 1 : 0
334-
335-
distribution_id = aws_cloudfront_distribution.this[0].id
336-
337-
monitoring_subscription {
338-
realtime_metrics_subscription_config {
339-
realtime_metrics_subscription_status = var.realtime_metrics_subscription_status
340-
}
341-
}
342-
}
343-
344271
data "aws_cloudfront_cache_policy" "this" {
345272
for_each = toset([for v in concat([var.default_cache_behavior], var.ordered_cache_behavior) : v.cache_policy_name if can(v.cache_policy_name)])
346273

outputs.tf

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,54 @@
11
output "cloudfront_distribution_id" {
22
description = "The identifier for the distribution."
3-
value = try(aws_cloudfront_distribution.this[0].id, "")
3+
value = { for k, v in aws_cloudfront_distribution.this : k => v.id }
44
}
55

66
output "cloudfront_distribution_arn" {
77
description = "The ARN (Amazon Resource Name) for the distribution."
8-
value = try(aws_cloudfront_distribution.this[0].arn, "")
8+
value = { for k, v in aws_cloudfront_distribution.this : k => v.arn }
99
}
1010

1111
output "cloudfront_distribution_caller_reference" {
1212
description = "Internal value used by CloudFront to allow future updates to the distribution configuration."
13-
value = try(aws_cloudfront_distribution.this[0].caller_reference, "")
13+
value = { for k, v in aws_cloudfront_distribution.this : k => v.caller_reference }
1414
}
1515

1616
output "cloudfront_distribution_status" {
17-
description = "The current status of the distribution. Deployed if the distribution's information is fully propagated throughout the Amazon CloudFront system."
18-
value = try(aws_cloudfront_distribution.this[0].status, "")
17+
description = "The current status of the distribution."
18+
value = { for k, v in aws_cloudfront_distribution.this : k => v.status }
1919
}
2020

2121
output "cloudfront_distribution_trusted_signers" {
2222
description = "List of nested attributes for active trusted signers, if the distribution is set up to serve private content with signed URLs"
23-
value = try(aws_cloudfront_distribution.this[0].trusted_signers, "")
23+
value = { for k, v in aws_cloudfront_distribution.this : k => v.trusted_signers }
2424
}
2525

2626
output "cloudfront_distribution_domain_name" {
2727
description = "The domain name corresponding to the distribution."
28-
value = try(aws_cloudfront_distribution.this[0].domain_name, "")
28+
value = { for k, v in aws_cloudfront_distribution.this : k => v.domain_name }
2929
}
3030

3131
output "cloudfront_distribution_last_modified_time" {
3232
description = "The date and time the distribution was last modified."
33-
value = try(aws_cloudfront_distribution.this[0].last_modified_time, "")
33+
value = { for k, v in aws_cloudfront_distribution.this : k => v.last_modified_time }
3434
}
3535

3636
output "cloudfront_distribution_in_progress_validation_batches" {
3737
description = "The number of invalidation batches currently in progress."
38-
value = try(aws_cloudfront_distribution.this[0].in_progress_validation_batches, "")
38+
value = { for k, v in aws_cloudfront_distribution.this : k => v.in_progress_validation_batches }
3939
}
4040

4141
output "cloudfront_distribution_etag" {
4242
description = "The current version of the distribution's information."
43-
value = try(aws_cloudfront_distribution.this[0].etag, "")
43+
value = { for k, v in aws_cloudfront_distribution.this : k => v.etag }
4444
}
4545

4646
output "cloudfront_distribution_hosted_zone_id" {
4747
description = "The CloudFront Route 53 zone ID that can be used to route an Alias Resource Record Set to."
48-
value = try(aws_cloudfront_distribution.this[0].hosted_zone_id, "")
49-
}
50-
51-
output "cloudfront_origin_access_identities" {
52-
description = "The origin access identities created"
53-
value = { for k, v in aws_cloudfront_origin_access_identity.this : k => v if local.create_origin_access_identity }
54-
}
55-
56-
output "cloudfront_origin_access_identity_ids" {
57-
description = "The IDS of the origin access identities created"
58-
value = [for v in aws_cloudfront_origin_access_identity.this : v.id if local.create_origin_access_identity]
59-
}
60-
61-
output "cloudfront_origin_access_identity_iam_arns" {
62-
description = "The IAM arns of the origin access identities created"
63-
value = [for v in aws_cloudfront_origin_access_identity.this : v.iam_arn if local.create_origin_access_identity]
64-
}
65-
66-
output "cloudfront_monitoring_subscription_id" {
67-
description = " The ID of the CloudFront monitoring subscription, which corresponds to the `distribution_id`."
68-
value = try(aws_cloudfront_monitoring_subscription.this[0].id, "")
48+
value = { for k, v in aws_cloudfront_distribution.this : k => v.hosted_zone_id }
6949
}
7050

7151
output "cloudfront_distribution_tags" {
72-
description = "Tags of the distribution's"
73-
value = try(aws_cloudfront_distribution.this[0].tags_all, "")
74-
}
75-
76-
output "cloudfront_origin_access_controls" {
77-
description = "The origin access controls created"
78-
value = local.create_origin_access_control ? { for k, v in aws_cloudfront_origin_access_control.this : k => v } : {}
79-
}
80-
81-
output "cloudfront_origin_access_controls_ids" {
82-
description = "The IDS of the origin access identities created"
83-
value = local.create_origin_access_control ? [for v in aws_cloudfront_origin_access_control.this : v.id] : []
84-
}
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] : []
52+
description = "Tags of the distributions"
53+
value = { for k, v in aws_cloudfront_distribution.this : k => v.tags_all }
8954
}

0 commit comments

Comments
 (0)