Skip to content

Commit 8843156

Browse files
authored
docs: Update the example (#44276)
* Use bucket policy instead of bucket ACL * Add OAC resource * Remove logging configuration * Add arguments related to ACM * Add some resources of Route 53 to register aliases
1 parent 80fe0fb commit 8843156

File tree

1 file changed

+66
-11
lines changed

1 file changed

+66
-11
lines changed

website/docs/r/cloudfront_distribution.html.markdown

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,55 @@ resource "aws_s3_bucket" "b" {
2929
}
3030
}
3131
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
3563
}
3664
3765
locals {
3866
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"
3981
}
4082
4183
resource "aws_cloudfront_distribution" "s3_distribution" {
@@ -50,13 +92,7 @@ resource "aws_cloudfront_distribution" "s3_distribution" {
5092
comment = "Some comment"
5193
default_root_object = "index.html"
5294
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}"]
6096
6197
default_cache_behavior {
6298
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
@@ -136,7 +172,26 @@ resource "aws_cloudfront_distribution" "s3_distribution" {
136172
}
137173
138174
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
140195
}
141196
}
142197
```

0 commit comments

Comments
 (0)