forked from cloudposse/terraform-aws-cloudfront-cdn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
177 lines (141 loc) · 5.61 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
module "origin_label" {
source = "cloudposse/label/null"
version = "0.22.1"
attributes = ["origin"]
context = module.this.context
}
resource "aws_cloudfront_origin_access_identity" "default" {
count = module.this.enabled ? 1 : 0
comment = module.origin_label.id
}
module "logs" {
source = "cloudposse/s3-log-storage/aws"
version = "0.16.0"
enabled = module.this.enabled && var.logging_enabled && length(var.log_bucket_fqdn) == 0
attributes = compact(concat(module.this.attributes, ["origin", "logs"]))
lifecycle_prefix = var.log_prefix
standard_transition_days = var.log_standard_transition_days
glacier_transition_days = var.log_glacier_transition_days
expiration_days = var.log_expiration_days
context = module.this.context
}
resource "aws_cloudfront_distribution" "default" {
count = module.this.enabled ? 1 : 0
enabled = var.distribution_enabled
is_ipv6_enabled = var.is_ipv6_enabled
comment = var.comment
default_root_object = var.default_root_object
price_class = var.price_class
dynamic "logging_config" {
for_each = var.logging_enabled ? ["true"] : []
content {
include_cookies = var.log_include_cookies
bucket = length(var.log_bucket_fqdn) > 0 ? var.log_bucket_fqdn : module.logs.bucket_domain_name
prefix = var.log_prefix
}
}
aliases = var.aliases
dynamic "custom_error_response" {
for_each = var.custom_error_response
content {
error_caching_min_ttl = lookup(custom_error_response.value, "error_caching_min_ttl", null)
error_code = custom_error_response.value.error_code
response_code = lookup(custom_error_response.value, "response_code", null)
response_page_path = lookup(custom_error_response.value, "response_page_path", null)
}
}
origin {
domain_name = var.origin_domain_name
origin_id = module.this.id
origin_path = var.origin_path
custom_origin_config {
http_port = var.origin_http_port
https_port = var.origin_https_port
origin_protocol_policy = var.origin_protocol_policy
origin_ssl_protocols = var.origin_ssl_protocols
origin_keepalive_timeout = var.origin_keepalive_timeout
origin_read_timeout = var.origin_read_timeout
}
dynamic "custom_header" {
for_each = var.custom_header
content {
name = custom_header.value.name
value = custom_header.value.value
}
}
}
viewer_certificate {
acm_certificate_arn = var.acm_certificate_arn
ssl_support_method = "sni-only"
minimum_protocol_version = var.viewer_minimum_protocol_version
cloudfront_default_certificate = var.acm_certificate_arn == "" ? true : false
}
default_cache_behavior {
allowed_methods = var.allowed_methods
cached_methods = var.cached_methods
target_origin_id = module.this.id
compress = var.compress
forwarded_values {
headers = var.forward_headers
query_string = var.forward_query_string
cookies {
forward = var.forward_cookies
whitelisted_names = var.forward_cookies_whitelisted_names
}
}
viewer_protocol_policy = var.viewer_protocol_policy
default_ttl = var.default_ttl
min_ttl = var.min_ttl
max_ttl = var.max_ttl
}
dynamic "ordered_cache_behavior" {
for_each = var.ordered_cache
content {
path_pattern = ordered_cache_behavior.value.path_pattern
allowed_methods = ordered_cache_behavior.value.allowed_methods
cached_methods = ordered_cache_behavior.value.cached_methods
target_origin_id = ordered_cache_behavior.value.target_origin_id == "" ? module.this.id : ordered_cache_behavior.value.target_origin_id
compress = ordered_cache_behavior.value.compress
trusted_signers = var.trusted_signers
forwarded_values {
query_string = ordered_cache_behavior.value.forward_query_string
headers = ordered_cache_behavior.value.forward_header_values
cookies {
forward = ordered_cache_behavior.value.forward_cookies
}
}
viewer_protocol_policy = ordered_cache_behavior.value.viewer_protocol_policy
default_ttl = ordered_cache_behavior.value.default_ttl
min_ttl = ordered_cache_behavior.value.min_ttl
max_ttl = ordered_cache_behavior.value.max_ttl
dynamic "lambda_function_association" {
for_each = ordered_cache_behavior.value.lambda_function_association
content {
event_type = lambda_function_association.value.event_type
include_body = lookup(lambda_function_association.value, "include_body", null)
lambda_arn = lambda_function_association.value.lambda_arn
}
}
}
}
web_acl_id = var.web_acl_id
restrictions {
geo_restriction {
restriction_type = var.geo_restriction_type
locations = var.geo_restriction_locations
}
}
tags = module.this.tags
}
module "dns" {
source = "cloudposse/route53-alias/aws"
version = "0.10.0"
enabled = (module.this.enabled && var.dns_aliases_enabled) ? true : false
aliases = var.aliases
parent_zone_id = var.parent_zone_id
parent_zone_name = var.parent_zone_name
target_dns_name = try(aws_cloudfront_distribution.default[0].domain_name, "")
target_zone_id = try(aws_cloudfront_distribution.default[0].hosted_zone_id, "")
ipv6_enabled = var.is_ipv6_enabled
context = module.this.context
}