|
| 1 | +# Inspired by https://gist.github.com/danihodovic/a51eb0d9d4b29649c2d094f4251827dd |
| 2 | + |
| 3 | +provider "aws" { |
| 4 | + profile = "${var.aws_profile}" |
| 5 | + region = "${var.aws_region}" |
| 6 | +} |
| 7 | + |
| 8 | +provider "aws" { |
| 9 | + alias = "nvirginia" |
| 10 | + profile = "${var.aws_profile}" |
| 11 | + region = "us-east-1" |
| 12 | +} |
| 13 | + |
| 14 | +terraform { |
| 15 | + backend "s3" { |
| 16 | + region = "us-east-1" |
| 17 | + encrypt = true |
| 18 | + bucket = "terraform-state-bucket.rtfpessoa.xyz" |
| 19 | + dynamodb_table = "terraform-state-table" |
| 20 | + key = "diff2html.xyz" |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +resource "aws_acm_certificate" "cert" { |
| 25 | + provider = "aws.nvirginia" |
| 26 | + domain_name = "${var.domain}" |
| 27 | + subject_alternative_names = ["*.${var.domain}"] |
| 28 | + validation_method = "DNS" |
| 29 | + |
| 30 | + lifecycle { |
| 31 | + create_before_destroy = true |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +resource "aws_route53_record" "root_domain" { |
| 36 | + zone_id = "${var.hosted_zone_id}" |
| 37 | + name = "${var.domain}" |
| 38 | + type = "A" |
| 39 | + |
| 40 | + alias { |
| 41 | + name = "${aws_cloudfront_distribution.cdn.domain_name}" |
| 42 | + zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}" |
| 43 | + evaluate_target_health = false |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +resource "aws_route53_record" "www_domain" { |
| 48 | + zone_id = "${var.hosted_zone_id}" |
| 49 | + name = "www.${var.domain}" |
| 50 | + type = "A" |
| 51 | + |
| 52 | + alias { |
| 53 | + name = "${aws_cloudfront_distribution.cdn.domain_name}" |
| 54 | + zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}" |
| 55 | + evaluate_target_health = false |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +resource "aws_route53_record" "cert_validation" { |
| 60 | + zone_id = "${var.hosted_zone_id}" |
| 61 | + name = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_name}" |
| 62 | + type = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_type}" |
| 63 | + |
| 64 | + records = ["${aws_acm_certificate.cert.domain_validation_options.0.resource_record_value}"] |
| 65 | + ttl = 60 |
| 66 | +} |
| 67 | + |
| 68 | +resource "aws_acm_certificate_validation" "cert" { |
| 69 | + provider = "aws.nvirginia" |
| 70 | + certificate_arn = "${aws_acm_certificate.cert.arn}" |
| 71 | + validation_record_fqdns = ["${aws_route53_record.cert_validation.fqdn}"] |
| 72 | +} |
| 73 | + |
| 74 | +resource "aws_cloudfront_origin_access_identity" "origin_access_identity" { |
| 75 | + comment = "${var.domain} origin access identity" |
| 76 | +} |
| 77 | + |
| 78 | +resource "aws_s3_bucket" "site" { |
| 79 | + bucket = "${var.domain}" |
| 80 | + acl = "private" |
| 81 | + |
| 82 | + policy = <<EOF |
| 83 | +{ |
| 84 | + "Version": "2008-10-17", |
| 85 | + "Statement": [{ |
| 86 | + "Sid": "AllowCloudFrontRead", |
| 87 | + "Effect": "Allow", |
| 88 | + "Principal": { "AWS": "${aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn}" }, |
| 89 | + "Action": "s3:GetObject", |
| 90 | + "Resource": "arn:aws:s3:::${var.domain}/*" |
| 91 | + }] |
| 92 | +} |
| 93 | + EOF |
| 94 | +} |
| 95 | + |
| 96 | +locals { |
| 97 | + s3_origin_id = "S3-${var.domain}" |
| 98 | +} |
| 99 | + |
| 100 | +resource "aws_cloudfront_distribution" "cdn" { |
| 101 | + origin { |
| 102 | + domain_name = "${aws_s3_bucket.site.bucket_regional_domain_name}" |
| 103 | + origin_id = "${local.s3_origin_id}" |
| 104 | + |
| 105 | + s3_origin_config { |
| 106 | + origin_access_identity = "${aws_cloudfront_origin_access_identity.origin_access_identity.cloudfront_access_identity_path}" |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + # If using route53 aliases for DNS we need to declare it here too, otherwise we'll get 403s. |
| 111 | + aliases = ["${var.domain}", "www.${var.domain}"] |
| 112 | + |
| 113 | + enabled = true |
| 114 | + is_ipv6_enabled = true |
| 115 | + default_root_object = "index.html" |
| 116 | + |
| 117 | + default_cache_behavior { |
| 118 | + allowed_methods = ["GET", "HEAD", "OPTIONS"] |
| 119 | + cached_methods = ["GET", "HEAD"] |
| 120 | + target_origin_id = "${local.s3_origin_id}" |
| 121 | + |
| 122 | + forwarded_values { |
| 123 | + query_string = true |
| 124 | + cookies { |
| 125 | + forward = "none" |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + min_ttl = 0 |
| 130 | + default_ttl = 86400 |
| 131 | + max_ttl = 31536000 |
| 132 | + compress = true |
| 133 | + viewer_protocol_policy = "redirect-to-https" |
| 134 | + } |
| 135 | + |
| 136 | + price_class = "PriceClass_All" |
| 137 | + |
| 138 | + restrictions { |
| 139 | + geo_restriction { |
| 140 | + restriction_type = "none" |
| 141 | + locations = [] |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + viewer_certificate { |
| 146 | + acm_certificate_arn = "${aws_acm_certificate_validation.cert.certificate_arn}" |
| 147 | + minimum_protocol_version = "TLSv1.1_2016" |
| 148 | + ssl_support_method = "sni-only" |
| 149 | + } |
| 150 | +} |
0 commit comments