-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
105 lines (87 loc) · 2.73 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
102
103
104
105
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.52.0"
}
}
}
provider "aws" {
region = var.aws_region
}
data "aws_s3_bucket" "selected" {
bucket = var.aws_s3_bucket
}
resource "aws_s3_bucket_policy" "allow_access_from_cloudfront_only" {
bucket = data.aws_s3_bucket.selected.id
policy = data.aws_iam_policy_document.allow_access_from_cloudfront_only.json
}
data "aws_iam_policy_document" "allow_access_from_cloudfront_only" {
statement {
sid = "AllowCloudFrontServicePrincipalReadOnly"
effect = "Allow"
principals {
type = "Service"
identifiers = ["cloudfront.amazonaws.com"]
}
actions = [
"s3:GetObject",
]
resources = [
"${data.aws_s3_bucket.selected.arn}/*",
]
condition {
test = "StringEquals"
variable = "AWS:SourceArn"
values = [aws_cloudfront_distribution.s3_distribution.arn]
}
}
}
resource "aws_cloudfront_origin_access_control" "default" {
name = "S3Assets"
description = "S3 Assets CDN Policy"
origin_access_control_origin_type = "s3"
signing_behavior = "always"
signing_protocol = "sigv4"
}
# Gets the AWS managed cache policy
data "aws_cloudfront_cache_policy" "caching_optimized" {
count = length(var.prebuilt_policy_name) > 0 ? 1 : 0
name = var.prebuilt_policy_name
}
resource "aws_cloudfront_distribution" "s3_distribution" {
origin {
# DNS of bucket
domain_name = data.aws_s3_bucket.selected.bucket_regional_domain_name
origin_access_control_id = aws_cloudfront_origin_access_control.default.id
origin_id = var.origin_name
}
enabled = var.enable_cdn
logging_config {
include_cookies = false
bucket = "${var.aws_s3_log_bucket}.s3.amazonaws.com"
prefix = var.aws_s3_log_prefix
}
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = var.origin_name
viewer_protocol_policy = "redirect-to-https"
compress = true
# Uses AWS Prebuilt caching optimized policy
# cache_policy_id = data.aws_cloudfront_cache_policy.caching_optimized[0].id
cache_policy_id = length(var.prebuilt_policy_name) > 0 ? data.aws_cloudfront_cache_policy.caching_optimized[0].id : aws_cloudfront_cache_policy.compression[0].id
}
restrictions {
geo_restriction {
restriction_type = "whitelist"
locations = ["US", "CA", "GB", "DE"]
}
}
tags = {
Environment = "dev"
}
viewer_certificate {
cloudfront_default_certificate = true
}
}