-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
101 lines (93 loc) · 3.19 KB
/
main.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
provider "aws" {
alias = "us_east_1"
region = "us-east-1"
}
resource "aws_s3_bucket" "this" {
# AWS recommends against using dots in S3 bucket names. See
# https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html.
bucket = replace(var.domain, ".", "-")
}
resource "aws_s3_bucket_public_access_block" "this" {
bucket = aws_s3_bucket.this.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
data "aws_iam_policy_document" "allow_access_from_cloudfront" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudfront.amazonaws.com"]
}
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.this.arn}/*"]
condition {
test = "StringEquals"
variable = "AWS:SourceArn"
values = [aws_cloudfront_distribution.this.arn]
}
}
}
resource "aws_s3_bucket_policy" "this" {
bucket = aws_s3_bucket.this.id
policy = data.aws_iam_policy_document.allow_access_from_cloudfront.json
}
# To refer to this CloudFront distribution by its alias (i.e. var.domain),
# add a CNAME record to the domain registrar where var.domain is registered.
resource "aws_cloudfront_distribution" "this" {
enabled = true
default_root_object = "index.html"
# see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html#create-oac-overview-s3
origin {
domain_name = aws_s3_bucket.this.bucket_regional_domain_name
origin_id = aws_s3_bucket.this.id
origin_access_control_id = aws_cloudfront_origin_access_control.this.id
}
aliases = [var.domain]
# AWS Managed-CachingOptimized
default_cache_behavior {
cache_policy_id = data.aws_cloudfront_cache_policy.managed_caching_optimized.id
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD", "OPTIONS"]
target_origin_id = aws_s3_bucket.this.id
# Redirect HTTP to HTTPS.
viewer_protocol_policy = "redirect-to-https"
dynamic "function_association" {
for_each = var.functions
content {
event_type = function_association.value["event_type"]
function_arn = function_association.value["function_arn"]
}
}
}
restrictions {
geo_restriction {
restriction_type = "none"
locations = []
}
}
viewer_certificate {
acm_certificate_arn = aws_acm_certificate.this.arn
ssl_support_method = "sni-only"
}
}
resource "aws_cloudfront_origin_access_control" "this" {
name = aws_s3_bucket.this.bucket_regional_domain_name
origin_access_control_origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}
data "aws_cloudfront_cache_policy" "managed_caching_optimized" {
name = "Managed-CachingOptimized"
}
resource "aws_acm_certificate" "this" {
# The certificate must be in us-east-1 according to https://stackoverflow.com/a/75795466.
provider = aws.us_east_1
domain_name = var.domain
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}