|
| 1 | +resource "aws_s3_bucket" "frontend" { |
| 2 | + bucket = "${var.project_name}-frontend-hosting" |
| 3 | +} |
| 4 | + |
| 5 | +resource "aws_s3_bucket_website_configuration" "frontend" { |
| 6 | + bucket = aws_s3_bucket.frontend.id |
| 7 | + |
| 8 | + index_document { |
| 9 | + suffix = "index.html" |
| 10 | + } |
| 11 | + |
| 12 | + error_document { |
| 13 | + key = "index.html" |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +resource "aws_s3_bucket_public_access_block" "frontend" { |
| 18 | + bucket = aws_s3_bucket.frontend.id |
| 19 | + block_public_acls = true |
| 20 | + block_public_policy = true |
| 21 | + ignore_public_acls = true |
| 22 | + restrict_public_buckets = true |
| 23 | +} |
| 24 | + |
| 25 | +resource "aws_cloudfront_origin_access_control" "this" { |
| 26 | + name = "${var.project_name}-s3-oac" |
| 27 | + description = "Origin Access Control for S3" |
| 28 | + origin_access_control_origin_type = "s3" |
| 29 | + signing_behavior = "always" |
| 30 | + signing_protocol = "sigv4" |
| 31 | +} |
| 32 | + |
| 33 | +resource "aws_s3_bucket_policy" "frontend" { |
| 34 | + bucket = aws_s3_bucket.frontend.id |
| 35 | + policy = jsonencode({ |
| 36 | + Version = "2012-10-17" |
| 37 | + Statement = [ |
| 38 | + { |
| 39 | + Effect = "Allow" |
| 40 | + Principal = { Service = "cloudfront.amazonaws.com" } |
| 41 | + Action = "s3:GetObject" |
| 42 | + Resource = "${aws_s3_bucket.frontend.arn}/*" |
| 43 | + Condition = { |
| 44 | + StringEquals = { |
| 45 | + "AWS:SourceArn" = aws_cloudfront_distribution.this.arn |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + ] |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +resource "aws_cloudfront_distribution" "this" { |
| 54 | + enabled = true |
| 55 | + is_ipv6_enabled = true |
| 56 | + comment = "Frontend for ${var.project_name}" |
| 57 | + default_root_object = "index.html" |
| 58 | + |
| 59 | + origin { |
| 60 | + domain_name = aws_s3_bucket.frontend.bucket_regional_domain_name |
| 61 | + origin_id = "S3-${aws_s3_bucket.frontend.id}" |
| 62 | + origin_access_control_id = aws_cloudfront_origin_access_control.this.id |
| 63 | + } |
| 64 | + |
| 65 | + origin { |
| 66 | + domain_name = var.api_base_url |
| 67 | + origin_id = "ALB-${var.project_name}" |
| 68 | + custom_origin_config { |
| 69 | + http_port = 80 |
| 70 | + https_port = 443 |
| 71 | + origin_protocol_policy = "http-only" |
| 72 | + origin_ssl_protocols = ["TLSv1.2"] |
| 73 | + origin_read_timeout = 30 |
| 74 | + origin_keepalive_timeout = 5 |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + default_cache_behavior { |
| 79 | + allowed_methods = ["GET", "HEAD", "OPTIONS"] |
| 80 | + cached_methods = ["GET", "HEAD", "OPTIONS"] |
| 81 | + target_origin_id = "S3-${aws_s3_bucket.frontend.id}" |
| 82 | + |
| 83 | + forwarded_values { |
| 84 | + query_string = false |
| 85 | + headers = ["Origin"] |
| 86 | + cookies { |
| 87 | + forward = "none" |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + viewer_protocol_policy = "redirect-to-https" |
| 92 | + min_ttl = 0 |
| 93 | + default_ttl = 3600 |
| 94 | + max_ttl = 86400 |
| 95 | + } |
| 96 | + |
| 97 | + ordered_cache_behavior { |
| 98 | + path_pattern = "/api/*" |
| 99 | + allowed_methods = ["GET", "HEAD", "OPTIONS", "PUT", "POST", "PATCH", "DELETE"] |
| 100 | + cached_methods = ["GET", "HEAD", "OPTIONS"] |
| 101 | + target_origin_id = "ALB-${var.project_name}" |
| 102 | + |
| 103 | + viewer_protocol_policy = "redirect-to-https" |
| 104 | + min_ttl = 0 |
| 105 | + default_ttl = 0 |
| 106 | + max_ttl = 0 |
| 107 | + |
| 108 | + forwarded_values { |
| 109 | + query_string = true |
| 110 | + headers = ["*"] |
| 111 | + cookies { |
| 112 | + forward = "all" |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + restrictions { |
| 118 | + geo_restriction { |
| 119 | + restriction_type = "none" |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + viewer_certificate { |
| 124 | + cloudfront_default_certificate = true |
| 125 | + } |
| 126 | + |
| 127 | + tags = { |
| 128 | + Project = var.project_name |
| 129 | + Environment = "frontend" |
| 130 | + } |
| 131 | +} |
0 commit comments