@@ -29,13 +29,55 @@ resource "aws_s3_bucket" "b" {
29
29
}
30
30
}
31
31
32
- resource "aws_s3_bucket_acl" "b_acl" {
33
- bucket = aws_s3_bucket.b.id
34
- acl = "private"
32
+ # See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html
33
+ data "aws_iam_policy_document" "origin_bucket_policy" {
34
+ statement {
35
+ sid = "AllowCloudFrontServicePrincipalReadWrite"
36
+ effect = "Allow"
37
+
38
+ principals {
39
+ type = "Service"
40
+ identifiers = ["cloudfront.amazonaws.com"]
41
+ }
42
+
43
+ actions = [
44
+ "s3:GetObject",
45
+ "s3:PutObject",
46
+ ]
47
+
48
+ resources = [
49
+ "${aws_s3_bucket.b.arn}/*",
50
+ ]
51
+
52
+ condition {
53
+ test = "StringEquals"
54
+ variable = "AWS:SourceArn"
55
+ values = [aws_cloudfront_distribution.s3_distribution.arn]
56
+ }
57
+ }
58
+ }
59
+
60
+ resource "aws_s3_bucket_policy" "b" {
61
+ bucket = aws_s3_bucket.b.bucket
62
+ policy = data.aws_iam_policy_document.origin_bucket_policy.json
35
63
}
36
64
37
65
locals {
38
66
s3_origin_id = "myS3Origin"
67
+ my_domain = "mydomain.com"
68
+ }
69
+
70
+ data "aws_acm_certificate" "my_domain" {
71
+ region = "us-east-1"
72
+ domain = "*.${local.my_domain}"
73
+ statuses = ["ISSUED"]
74
+ }
75
+
76
+ resource "aws_cloudfront_origin_access_control" "default" {
77
+ name = "default-oac"
78
+ origin_access_control_origin_type = "s3"
79
+ signing_behavior = "always"
80
+ signing_protocol = "sigv4"
39
81
}
40
82
41
83
resource "aws_cloudfront_distribution" "s3_distribution" {
@@ -50,13 +92,7 @@ resource "aws_cloudfront_distribution" "s3_distribution" {
50
92
comment = "Some comment"
51
93
default_root_object = "index.html"
52
94
53
- logging_config {
54
- include_cookies = false
55
- bucket = "mylogs.s3.amazonaws.com"
56
- prefix = "myprefix"
57
- }
58
-
59
- aliases = ["mysite.example.com", "yoursite.example.com"]
95
+ aliases = ["mysite.${local.my_domain}", "yoursite.${local.my_domain}"]
60
96
61
97
default_cache_behavior {
62
98
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
@@ -136,7 +172,26 @@ resource "aws_cloudfront_distribution" "s3_distribution" {
136
172
}
137
173
138
174
viewer_certificate {
139
- cloudfront_default_certificate = true
175
+ acm_certificate_arn = data.aws_acm_certificate.my_domain.arn
176
+ ssl_support_method = "sni-only"
177
+ }
178
+ }
179
+
180
+ # Create Route53 records for the CloudFront distribution aliases
181
+ data "aws_route53_zone" "my_domain" {
182
+ name = local.my_domain
183
+ }
184
+
185
+ resource "aws_route53_record" "cloudfront" {
186
+ for_each = aws_cloudfront_distribution.s3_distribution.aliases
187
+ zone_id = data.aws_route53_zone.my_domain.zone_id
188
+ name = each.value
189
+ type = "A"
190
+
191
+ alias {
192
+ name = aws_cloudfront_distribution.s3_distribution.domain_name
193
+ zone_id = aws_cloudfront_distribution.s3_distribution.hosted_zone_id
194
+ evaluate_target_health = false
140
195
}
141
196
}
142
197
```
0 commit comments