Skip to content

Commit 940bc21

Browse files
authored
Merge pull request #478 from rust-lang/rustup-builds-distribution
Create CloudFront distribution for Rustup builds
2 parents 6baa14d + ee77ca0 commit 940bc21

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

terragrunt/accounts/legacy/rustup-prod/rustup/terragrunt.hcl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ include {
66
path = find_in_parent_folders()
77
merge_strategy = "deep"
88
}
9+
10+
inputs = {
11+
builds_domain_name = "rustup-builds.rust-lang.org"
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module "certificate" {
2+
source = "../acm-certificate"
3+
4+
providers = {
5+
aws = aws.us-east-1
6+
}
7+
8+
domains = [
9+
var.builds_domain_name,
10+
]
11+
12+
legacy = true
13+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
resource "aws_cloudfront_distribution" "builds" {
2+
comment = var.builds_domain_name
3+
4+
enabled = true
5+
wait_for_deployment = false
6+
is_ipv6_enabled = true
7+
price_class = "PriceClass_All"
8+
9+
aliases = [
10+
var.builds_domain_name,
11+
]
12+
13+
viewer_certificate {
14+
acm_certificate_arn = module.certificate.arn
15+
ssl_support_method = "sni-only"
16+
minimum_protocol_version = "TLSv1.1_2016"
17+
}
18+
19+
default_cache_behavior {
20+
target_origin_id = "builds"
21+
allowed_methods = ["GET", "HEAD"]
22+
cached_methods = ["GET", "HEAD"]
23+
compress = true
24+
viewer_protocol_policy = "redirect-to-https"
25+
26+
forwarded_values {
27+
headers = [
28+
// Following the spec, AWS S3 only replies with the CORS headers when
29+
// an Origin is present, and varies its response based on that. If we
30+
// don't forward the header CloudFront is going to cache the first CORS
31+
// response it receives, even if it's empty.
32+
"Origin",
33+
]
34+
35+
query_string = false
36+
37+
cookies {
38+
forward = "none"
39+
}
40+
}
41+
}
42+
43+
origin {
44+
origin_id = "builds"
45+
domain_name = aws_s3_bucket.builds.bucket_regional_domain_name
46+
origin_access_control_id = aws_cloudfront_origin_access_control.builds.id
47+
}
48+
49+
restrictions {
50+
geo_restriction {
51+
restriction_type = "none"
52+
}
53+
}
54+
}
55+
56+
resource "aws_cloudfront_origin_access_control" "builds" {
57+
name = "rustup-builds"
58+
description = var.builds_domain_name
59+
origin_access_control_origin_type = "s3"
60+
signing_behavior = "always"
61+
signing_protocol = "sigv4"
62+
}

terragrunt/modules/rustup/dns.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
data "aws_route53_zone" "builds" {
2+
// Convert foo.bar.baz into bar.baz
3+
name = join(".", reverse(slice(reverse(split(".", var.builds_domain_name)), 0, 2)))
4+
}
5+
6+
resource "aws_route53_record" "cloudfront_builds_domain" {
7+
zone_id = data.aws_route53_zone.builds.id
8+
name = var.builds_domain_name
9+
type = "CNAME"
10+
ttl = 300
11+
records = [aws_cloudfront_distribution.builds.domain_name]
12+
}

terragrunt/modules/rustup/s3.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,32 @@ module "ci_role" {
1313
branch = "master"
1414
}
1515

16+
resource "aws_s3_bucket_policy" "cloudfront" {
17+
provider = aws.us-east-1
18+
19+
bucket = aws_s3_bucket.builds.id
20+
21+
policy = jsonencode({
22+
Version = "2012-10-17"
23+
Statement = [
24+
{
25+
Sid = "AllowCloudFrontReadOnlyAccess"
26+
Effect = "Allow"
27+
Principal = {
28+
Service = "cloudfront.amazonaws.com"
29+
}
30+
Action = ["s3:GetObject"]
31+
Resource = ["${aws_s3_bucket.builds.arn}/*"]
32+
Condition = {
33+
StringEquals = {
34+
"AWS:SourceArn" = aws_cloudfront_distribution.builds.arn
35+
}
36+
}
37+
}
38+
]
39+
})
40+
}
41+
1642
resource "aws_iam_policy" "upload_builds" {
1743
name = "upload-rustup-builds"
1844
policy = jsonencode({
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "builds_domain_name" {
2+
description = "The domain for the CloudFront distribution that serves the builds"
3+
type = string
4+
}

0 commit comments

Comments
 (0)