Skip to content

Commit fadeb3d

Browse files
committed
add variable types
1 parent a7eeabb commit fadeb3d

File tree

2 files changed

+62
-63
lines changed

2 files changed

+62
-63
lines changed

main.tf

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
locals {
22
public_dir_with_leading_slash = "${length(var.public_dir) > 0 ? "/${var.public_dir}" : ""}"
3-
static_website_routing_rules = <<EOF
3+
static_website_routing_rules = <<EOF
44
[{
55
"Condition": {
66
"KeyPrefixEquals": "${var.public_dir}/${var.public_dir}/"
@@ -40,9 +40,9 @@ data "aws_iam_policy_document" "static_website_read_with_secret" {
4040
}
4141

4242
condition {
43-
test = "StringEquals"
43+
test = "StringEquals"
4444
variable = "aws:UserAgent"
45-
values = ["${var.secret}"]
45+
values = [var.secret]
4646
}
4747
}
4848
}
@@ -63,15 +63,15 @@ resource "aws_cloudfront_distribution" "cdn" {
6363
origin_id = "${local.s3_origin_id}"
6464

6565
custom_origin_config {
66-
http_port = 80
67-
https_port = 443
68-
origin_protocol_policy = "http-only"
69-
origin_ssl_protocols = ["TLSv1.2", "TLSv1.1", "TLSv1"]
66+
http_port = 80
67+
https_port = 443
68+
origin_protocol_policy = "http-only"
69+
origin_ssl_protocols = ["TLSv1.2", "TLSv1.1", "TLSv1"]
7070
}
7171

7272
custom_header {
7373
name = "User-Agent"
74-
value = "${var.secret}"
74+
value = var.secret
7575
}
7676
}
7777

@@ -82,15 +82,15 @@ resource "aws_cloudfront_distribution" "cdn" {
8282
aliases = ["${var.domain_name}"]
8383

8484
custom_error_response {
85-
error_code = 403
86-
response_page_path = "/error.html"
87-
response_code = 404
85+
error_code = 403
86+
response_page_path = "/error.html"
87+
response_code = 404
8888
}
8989

9090
custom_error_response {
91-
error_code = 404
92-
response_page_path = "/error.html"
93-
response_code = 404
91+
error_code = 404
92+
response_page_path = "/error.html"
93+
response_code = 404
9494
}
9595

9696
default_cache_behavior {
@@ -116,62 +116,45 @@ resource "aws_cloudfront_distribution" "cdn" {
116116
}
117117

118118
viewer_certificate {
119-
acm_certificate_arn = "${var.cert_arn}"
120-
ssl_support_method = "sni-only"
121-
minimum_protocol_version = "TLSv1.1_2016"
119+
acm_certificate_arn = "${var.cert_arn}"
120+
ssl_support_method = "sni-only"
121+
minimum_protocol_version = "TLSv1.1_2016"
122122
}
123123

124124
tags = "${merge(map("Name", "${var.domain_name}-cdn"), var.tags)}"
125125
}
126126

127-
resource "aws_route53_record" "alias" {
128-
count = "${length(var.zone_id) > 0 ? 1 : 0}"
129-
130-
zone_id = "${var.zone_id}"
131-
name = "${var.domain_name}"
132-
type = "A"
133-
134-
alias {
135-
name = "${aws_cloudfront_distribution.cdn.domain_name}"
136-
zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}"
137-
evaluate_target_health = false
138-
}
139-
}
140127

141128
resource "aws_s3_bucket" "redirect" {
142-
count = "${length(var.redirects)}"
143-
144-
bucket = "${element(var.redirects, count.index)}"
145-
129+
for_each = var.redirects
130+
bucket = each.value
146131
website {
147132
redirect_all_requests_to = "https://${var.domain_name}"
148133
}
149-
150-
tags = "${merge(map("Name", "${element(var.redirects, count.index)}-redirect"), var.tags)}"
134+
tags = "${merge(map("Name", each.key), var.tags)}"
151135
}
152136

153137
resource "aws_cloudfront_distribution" "redirect" {
154-
count = "${length(var.redirects)}"
155-
138+
for_each = var.redirects
156139
origin {
157-
domain_name = "${element(aws_s3_bucket.redirect.*.website_endpoint, count.index)}"
158-
origin_id = "cloudfront-distribution-origin-${element(var.redirects, count.index)}.s3.amazonaws.com"
140+
domain_name = each.value
141+
origin_id = "cloudfront-distribution-origin-${each.key}.s3.amazonaws.com"
159142

160143
custom_origin_config {
161-
http_port = 80
162-
https_port = 443
163-
origin_protocol_policy = "http-only"
164-
origin_ssl_protocols = ["TLSv1.2", "TLSv1.1", "TLSv1"]
144+
http_port = 80
145+
https_port = 443
146+
origin_protocol_policy = "http-only"
147+
origin_ssl_protocols = ["TLSv1.2", "TLSv1.1", "TLSv1"]
165148
}
166149
}
167150

168-
comment = "CDN for ${element(var.redirects, count.index)} S3 Bucket (redirect)"
151+
comment = "CDN for ${each.value} S3 Bucket (redirect)"
169152
enabled = true
170153
is_ipv6_enabled = true
171-
aliases = ["${element(var.redirects, count.index)}"]
154+
aliases = ["${each.value}"]
172155

173156
default_cache_behavior {
174-
target_origin_id = "cloudfront-distribution-origin-${element(var.redirects, count.index)}.s3.amazonaws.com"
157+
target_origin_id = "cloudfront-distribution-origin-${each.key}.s3.amazonaws.com"
175158
allowed_methods = ["GET", "HEAD"]
176159
cached_methods = ["GET", "HEAD"]
177160

@@ -193,25 +176,40 @@ resource "aws_cloudfront_distribution" "redirect" {
193176
}
194177

195178
viewer_certificate {
196-
acm_certificate_arn = "${var.cert_arn}"
197-
ssl_support_method = "sni-only"
198-
minimum_protocol_version = "TLSv1.1_2016"
179+
acm_certificate_arn = "${var.cert_arn}"
180+
ssl_support_method = "sni-only"
181+
minimum_protocol_version = "TLSv1.1_2016"
199182
}
200183

201-
tags = "${merge(map("Name", "${element(var.redirects, count.index)}-cdn_redirect"), var.tags)}"
184+
tags = "${merge(map("Name", "${each.key}-cdn_redirect"), var.tags)}"
202185
}
203186

204-
resource "aws_route53_record" "redirect" {
205-
count = "${length(var.zone_id) > 0 ? length(var.redirects) : 0}"
187+
resource "aws_route53_record" "alias" {
188+
#count = "${length(var.zone_id) > 0 ? 1 : 0}"
206189

207190
zone_id = "${var.zone_id}"
208-
# Work-around (see: https://github.com/hashicorp/terraform/issues/11210)
209-
name = "${length(var.redirects) > 0 ? element(concat(var.redirects, list("")), count.index): ""}"
191+
name = "${var.domain_name}"
210192
type = "A"
211193

212194
alias {
213-
name = "${element(aws_cloudfront_distribution.redirect.*.domain_name, count.index)}"
214-
zone_id = "${element(aws_cloudfront_distribution.redirect.*.hosted_zone_id, count.index)}"
215-
evaluate_target_health = false
195+
name = "${aws_cloudfront_distribution.cdn.domain_name}"
196+
zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}"
197+
evaluate_target_health = false
198+
}
199+
}
200+
201+
resource "aws_route53_record" "redirect" {
202+
#count = "${length(var.zone_id) > 0 ? length(var.redirects) : 0}"
203+
204+
for_each = var.redirects
205+
zone_id = "${var.zone_id}"
206+
# Work-around (see: https://github.com/hashicorp/terraform/issues/11210)
207+
name = each.key
208+
type = "A"
209+
210+
alias {
211+
name = each.value
212+
zone_id = each.value
213+
evaluate_target_health = false
216214
}
217215
}

variables.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
variable "cert_arn" {
22
description = "ARN of the SSL Certificate to use for the Cloudfront Distribution"
3-
type = "string"
3+
type = string
44
}
55

66
variable "domain_name" {
77
description = "Domain name for the website (i.e. www.example.com)"
8-
type = "string"
8+
type = string
99
}
1010

1111
variable "public_dir" {
@@ -15,20 +15,21 @@ variable "public_dir" {
1515

1616
variable "redirects" {
1717
description = "A list of domains that should redirect to domain_name (i.e. for redirecting naked domain to www-version)"
18-
default = []
18+
type = map(string)
1919
}
2020

2121
variable "secret" {
2222
description = "A secret string between CloudFront and S3 to control access"
23-
type = "string"
23+
type = string
2424
}
2525

2626
variable "tags" {
2727
description = "A mapping of tags to assign to each resource"
28+
type = map(string)
2829
default = {}
2930
}
3031

3132
variable "zone_id" {
3233
description = "ID of the Route 53 Hosted Zone in which to create an alias record"
33-
type = "string"
34+
type = string
3435
}

0 commit comments

Comments
 (0)