|
| 1 | +resource "aws_s3_bucket" "static_website" { |
| 2 | + bucket = "${var.domain_name}" |
| 3 | + |
| 4 | + website { |
| 5 | + index_document = "index.html" |
| 6 | + error_document = "error.html" |
| 7 | + } |
| 8 | + |
| 9 | + tags = "${merge(map("Name", "${var.domain_name}-static_website"), var.tags)}" |
| 10 | +} |
| 11 | + |
| 12 | +data "aws_iam_policy_document" "static_website_read_with_secret" { |
| 13 | + statement { |
| 14 | + sid = "1" |
| 15 | + actions = ["s3:GetObject"] |
| 16 | + resources = ["${aws_s3_bucket.static_website.arn}${var.public_dir}/*"] |
| 17 | + |
| 18 | + principals { |
| 19 | + type = "AWS" |
| 20 | + identifiers = ["*"] |
| 21 | + } |
| 22 | + |
| 23 | + condition { |
| 24 | + test = "StringEquals" |
| 25 | + variable = "aws:UserAgent" |
| 26 | + values = ["${var.secret}"] |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +resource "aws_s3_bucket_policy" "static_website_read_with_secret" { |
| 32 | + bucket = "${aws_s3_bucket.static_website.id}" |
| 33 | + policy = "${data.aws_iam_policy_document.static_website_read_with_secret.json}" |
| 34 | +} |
| 35 | + |
| 36 | +locals { |
| 37 | + s3_origin_id = "cloudfront-distribution-origin-${var.domain_name}.s3.amazonaws.com${var.public_dir}" |
| 38 | +} |
| 39 | + |
| 40 | +resource "aws_cloudfront_distribution" "cdn" { |
| 41 | + origin { |
| 42 | + domain_name = "${aws_s3_bucket.static_website.website_endpoint}" |
| 43 | + origin_path = "${var.public_dir}" |
| 44 | + origin_id = "${local.s3_origin_id}" |
| 45 | + |
| 46 | + custom_origin_config { |
| 47 | + http_port = 80 |
| 48 | + https_port = 443 |
| 49 | + origin_protocol_policy = "http-only" |
| 50 | + origin_ssl_protocols = ["TLSv1.2", "TLSv1.1", "TLSv1"] |
| 51 | + } |
| 52 | + |
| 53 | + custom_header { |
| 54 | + name = "User-Agent" |
| 55 | + value = "${var.secret}" |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + comment = "CDN for ${var.domain_name} S3 Bucket" |
| 60 | + enabled = true |
| 61 | + is_ipv6_enabled = true |
| 62 | + default_root_object = "index.html" |
| 63 | + aliases = ["${var.domain_name}"] |
| 64 | + |
| 65 | + custom_error_response { |
| 66 | + error_code = 403 |
| 67 | + response_page_path = "/error.html" |
| 68 | + response_code = 404 |
| 69 | + } |
| 70 | + |
| 71 | + custom_error_response { |
| 72 | + error_code = 404 |
| 73 | + response_page_path = "/error.html" |
| 74 | + response_code = 404 |
| 75 | + } |
| 76 | + |
| 77 | + default_cache_behavior { |
| 78 | + target_origin_id = "${local.s3_origin_id}" |
| 79 | + allowed_methods = ["GET", "HEAD"] |
| 80 | + cached_methods = ["GET", "HEAD"] |
| 81 | + |
| 82 | + forwarded_values { |
| 83 | + query_string = false |
| 84 | + |
| 85 | + cookies { |
| 86 | + forward = "none" |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + viewer_protocol_policy = "redirect-to-https" |
| 91 | + } |
| 92 | + |
| 93 | + restrictions { |
| 94 | + geo_restriction { |
| 95 | + restriction_type = "none" |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + viewer_certificate { |
| 100 | + acm_certificate_arn = "${var.cert_arn}" |
| 101 | + ssl_support_method = "sni-only" |
| 102 | + minimum_protocol_version = "TLSv1.1_2016" |
| 103 | + } |
| 104 | + |
| 105 | + tags = "${merge(map("Name", "${var.domain_name}-cdn"), var.tags)}" |
| 106 | +} |
| 107 | + |
| 108 | +resource "aws_route53_record" "alias" { |
| 109 | + count = "${length(var.zone_id) > 0 ? 1 : 0}" |
| 110 | + |
| 111 | + zone_id = "${var.zone_id}" |
| 112 | + name = "${var.domain_name}" |
| 113 | + type = "A" |
| 114 | + |
| 115 | + alias { |
| 116 | + name = "${aws_cloudfront_distribution.cdn.domain_name}" |
| 117 | + zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}" |
| 118 | + evaluate_target_health = false |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +resource "aws_s3_bucket" "redirect" { |
| 123 | + count = "${length(var.redirects)}" |
| 124 | + |
| 125 | + bucket = "${element(var.redirects, count.index)}" |
| 126 | + |
| 127 | + website { |
| 128 | + redirect_all_requests_to = "https://${var.domain_name}" |
| 129 | + } |
| 130 | + |
| 131 | + tags = "${merge(map("Name", "${element(var.redirects, count.index)}-redirect"), var.tags)}" |
| 132 | +} |
| 133 | + |
| 134 | +resource "aws_cloudfront_distribution" "redirect" { |
| 135 | + count = "${length(var.redirects)}" |
| 136 | + |
| 137 | + origin { |
| 138 | + domain_name = "${element(aws_s3_bucket.redirect.*.website_endpoint, count.index)}" |
| 139 | + origin_id = "cloudfront-distribution-origin-${element(var.redirects, count.index)}.s3.amazonaws.com" |
| 140 | + |
| 141 | + custom_origin_config { |
| 142 | + http_port = 80 |
| 143 | + https_port = 443 |
| 144 | + origin_protocol_policy = "http-only" |
| 145 | + origin_ssl_protocols = ["TLSv1.2", "TLSv1.1", "TLSv1"] |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + comment = "CDN for ${element(var.redirects, count.index)} S3 Bucket (redirect)" |
| 150 | + enabled = true |
| 151 | + is_ipv6_enabled = true |
| 152 | + aliases = ["${element(var.redirects, count.index)}"] |
| 153 | + |
| 154 | + default_cache_behavior { |
| 155 | + target_origin_id = "cloudfront-distribution-origin-${element(var.redirects, count.index)}.s3.amazonaws.com" |
| 156 | + allowed_methods = ["GET", "HEAD"] |
| 157 | + cached_methods = ["GET", "HEAD"] |
| 158 | + |
| 159 | + forwarded_values { |
| 160 | + query_string = false |
| 161 | + |
| 162 | + cookies { |
| 163 | + forward = "none" |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + viewer_protocol_policy = "redirect-to-https" |
| 168 | + } |
| 169 | + |
| 170 | + restrictions { |
| 171 | + geo_restriction { |
| 172 | + restriction_type = "none" |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + viewer_certificate { |
| 177 | + acm_certificate_arn = "${var.cert_arn}" |
| 178 | + ssl_support_method = "sni-only" |
| 179 | + minimum_protocol_version = "TLSv1.1_2016" |
| 180 | + } |
| 181 | + |
| 182 | + tags = "${merge(map("Name", "${element(var.redirects, count.index)}-cdn_redirect"), var.tags)}" |
| 183 | +} |
| 184 | + |
| 185 | +resource "aws_route53_record" "redirect" { |
| 186 | + count = "${length(var.zone_id) > 0 ? length(var.redirects) : 0}" |
| 187 | + |
| 188 | + zone_id = "${var.zone_id}" |
| 189 | + # Work-around (see: https://github.com/hashicorp/terraform/issues/11210) |
| 190 | + name = "${length(var.redirects) > 0 ? element(concat(var.redirects, list("")), count.index): ""}" |
| 191 | + type = "A" |
| 192 | + |
| 193 | + alias { |
| 194 | + name = "${element(aws_cloudfront_distribution.redirect.*.domain_name, count.index)}" |
| 195 | + zone_id = "${element(aws_cloudfront_distribution.redirect.*.hosted_zone_id, count.index)}" |
| 196 | + evaluate_target_health = false |
| 197 | + } |
| 198 | +} |
0 commit comments