-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblog.tf
executable file
·130 lines (109 loc) · 2.91 KB
/
blog.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
locals {
domain = "blog.sulami.xyz"
site_bucket = "blog.sulami.xyz"
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.1"
}
}
required_version = ">= 1.4"
}
resource "aws_cloudfront_distribution" "cdn" {
aliases = [local.domain]
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cache_policy_id = "658327ea-f89d-4fab-a63d-7e88639e58f6"
cached_methods = ["GET", "HEAD"]
compress = "true"
default_ttl = "0"
function_association {
event_type = "viewer-request"
function_arn = resource.aws_cloudfront_function.append-index-html.arn
}
target_origin_id = resource.aws_s3_bucket.site.bucket_domain_name
viewer_protocol_policy = "redirect-to-https"
}
enabled = "true"
http_version = "http2and3"
is_ipv6_enabled = "true"
origin {
connection_attempts = "3"
connection_timeout = "10"
domain_name = resource.aws_s3_bucket.site.bucket_domain_name
origin_access_control_id = "E11KDC2SQFNCI1"
origin_id = resource.aws_s3_bucket.site.bucket_domain_name
}
price_class = "PriceClass_All"
restrictions {
geo_restriction {
restriction_type = "none"
}
}
retain_on_delete = "false"
viewer_certificate {
acm_certificate_arn = resource.aws_acm_certificate.blog.arn
ssl_support_method = "sni-only"
}
}
resource "aws_acm_certificate" "blog" {
domain_name = "blog.sulami.xyz"
validation_method = "DNS"
}
resource "aws_cloudfront_function" "append-index-html" {
name = "Append-indexhtml"
runtime = "cloudfront-js-1.0"
code = <<CODE
function handler(event) {
var request = event.request;
var uri = request.uri;
// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += 'index.html';
}
// Check whether the URI is missing a file extension.
else if (!uri.includes('.')) {
request.uri += '/index.html';
}
return request;
}
CODE
}
resource "aws_s3_bucket" "site" {
bucket = local.site_bucket
force_destroy = "false"
object_lock_enabled = "false"
}
resource "aws_s3_bucket_ownership_controls" "site" {
bucket = aws_s3_bucket.site.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_policy" "site" {
bucket = resource.aws_s3_bucket.site.bucket
policy = <<POLICY
{
"Id": "PolicyForCloudFrontPrivateContent",
"Statement": [
{
"Action": "s3:GetObject",
"Condition": {
"StringEquals": {
"AWS:SourceArn": "${resource.aws_cloudfront_distribution.cdn.arn}"
}
},
"Effect": "Allow",
"Principal": {
"Service": "cloudfront.amazonaws.com"
},
"Resource": "${resource.aws_s3_bucket.site.arn}/*",
"Sid": "AllowCloudFrontServicePrincipal"
}
],
"Version": "2008-10-17"
}
POLICY
}