|
| 1 | +provider "aws" { |
| 2 | + alias = "us_east_1" |
| 3 | + region = "us-east-1" |
| 4 | +} |
| 5 | + |
| 6 | +resource "aws_s3_bucket" "this" { |
| 7 | + bucket = "${var.bucket_name_prefix}-app" |
| 8 | + acl = "public-read" |
| 9 | + |
| 10 | + policy = <<EOF |
| 11 | +{ |
| 12 | + "Id": "${var.bucket_name_prefix}-app", |
| 13 | + "Version": "2012-10-17", |
| 14 | + "Statement": [ |
| 15 | + { |
| 16 | + "Sid": "${var.bucket_name_prefix}-app-main", |
| 17 | + "Action": [ |
| 18 | + "s3:GetObject" |
| 19 | + ], |
| 20 | + "Effect": "Allow", |
| 21 | + "Resource": "arn:aws:s3:::${var.bucket_name_prefix}-app/*", |
| 22 | + "Principal": "*" |
| 23 | + } |
| 24 | + ] |
| 25 | +} |
| 26 | +EOF |
| 27 | + |
| 28 | + website { |
| 29 | + index_document = "index.html" |
| 30 | + error_document = "index.html" |
| 31 | + } |
| 32 | + |
| 33 | + force_destroy = true |
| 34 | +} |
| 35 | + |
| 36 | +locals { |
| 37 | + s3_origin_id = "${var.bucket_name_prefix}-origin" |
| 38 | +} |
| 39 | + |
| 40 | +resource "aws_cloudfront_origin_access_identity" "this" { |
| 41 | + comment = "S3 Bucket Origin" |
| 42 | +} |
| 43 | + |
| 44 | +resource "aws_cloudfront_distribution" "this" { |
| 45 | + origin { |
| 46 | + domain_name = aws_s3_bucket.this.bucket_regional_domain_name |
| 47 | + origin_id = local.s3_origin_id |
| 48 | + |
| 49 | + s3_origin_config { |
| 50 | + origin_access_identity = aws_cloudfront_origin_access_identity.this.cloudfront_access_identity_path |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + enabled = true |
| 55 | + is_ipv6_enabled = true |
| 56 | + default_root_object = "index.html" |
| 57 | + |
| 58 | + aliases = concat( |
| 59 | + ["${var.service_name}.${var.zone}"], |
| 60 | + var.aliases |
| 61 | + ) |
| 62 | + |
| 63 | + default_cache_behavior { |
| 64 | + allowed_methods = ["GET", "HEAD", "OPTIONS"] |
| 65 | + cached_methods = ["GET", "HEAD"] |
| 66 | + target_origin_id = local.s3_origin_id |
| 67 | + |
| 68 | + forwarded_values { |
| 69 | + query_string = false |
| 70 | + |
| 71 | + cookies { |
| 72 | + forward = "none" |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + viewer_protocol_policy = "redirect-to-https" |
| 77 | + min_ttl = 0 |
| 78 | + default_ttl = 3600 |
| 79 | + max_ttl = 86400 |
| 80 | + } |
| 81 | + |
| 82 | + price_class = "PriceClass_200" |
| 83 | + |
| 84 | + restrictions { |
| 85 | + geo_restriction { |
| 86 | + restriction_type = "none" |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + custom_error_response { |
| 91 | + error_caching_min_ttl = 10 |
| 92 | + error_code = 404 |
| 93 | + response_code = 200 |
| 94 | + response_page_path = "/index.html" |
| 95 | + } |
| 96 | + |
| 97 | + viewer_certificate { |
| 98 | + acm_certificate_arn = aws_acm_certificate_validation.this.certificate_arn |
| 99 | + ssl_support_method = "sni-only" |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +resource "aws_acm_certificate" "this" { |
| 104 | + provider = aws.us_east_1 |
| 105 | + domain_name = "${var.service_name}.${var.zone}" |
| 106 | + validation_method = "DNS" |
| 107 | + subject_alternative_names = var.aliases |
| 108 | + |
| 109 | + lifecycle { |
| 110 | + create_before_destroy = true |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +data "aws_route53_zone" "this" { |
| 115 | + name = var.zone |
| 116 | + private_zone = false |
| 117 | +} |
| 118 | + |
| 119 | +resource "aws_route53_record" "certificate_validation" { |
| 120 | + for_each = { |
| 121 | + for dvo in aws_acm_certificate.this.domain_validation_options : dvo.domain_name => { |
| 122 | + name = dvo.resource_record_name |
| 123 | + record = dvo.resource_record_value |
| 124 | + type = dvo.resource_record_type |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + allow_overwrite = true |
| 129 | + name = each.value.name |
| 130 | + records = [each.value.record] |
| 131 | + ttl = 60 |
| 132 | + type = each.value.type |
| 133 | + zone_id = data.aws_route53_zone.this.zone_id |
| 134 | +} |
| 135 | + |
| 136 | +resource "aws_acm_certificate_validation" "this" { |
| 137 | + provider = aws.us_east_1 |
| 138 | + certificate_arn = aws_acm_certificate.this.arn |
| 139 | + validation_record_fqdns = [for record in aws_route53_record.certificate_validation : record.fqdn] |
| 140 | +} |
| 141 | + |
| 142 | +resource "aws_route53_record" "this" { |
| 143 | + for_each = toset(concat(["${var.service_name}.${var.zone}"], var.aliases)) |
| 144 | + |
| 145 | + zone_id = data.aws_route53_zone.this.zone_id |
| 146 | + name = each.value |
| 147 | + type = "A" |
| 148 | + allow_overwrite = true |
| 149 | + |
| 150 | + alias { |
| 151 | + name = aws_cloudfront_distribution.this.domain_name |
| 152 | + zone_id = aws_cloudfront_distribution.this.hosted_zone_id |
| 153 | + evaluate_target_health = false |
| 154 | + } |
| 155 | +} |
0 commit comments