【发布时间】:2021-05-30 08:29:44
【问题描述】:
我正在使用 Terraform 创建 Cloudfront 发行版。我已经启动并运行它,但我可以访问它的唯一方法是通过https://<id>.cloudfront.net/ 地址。我想使用 Route 53 区域中的记录,我必须重定向到 Cloudfront 分发。任何想法如何做到这一点?
variable "www_domain_name" {
default = "example.com"
}
S3 存储桶用于托管静态代码。这对公众开放,并使用允许公众访问的政策。
resource "aws_s3_bucket" "www" {
bucket = var.www_domain_name
acl = "public-read"
policy = <<POLICY
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"AddPerm",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::${var.www_domain_name}/*"]
}
]
}
POLICY
website {
index_document = "index.html"
error_document = "404.html"
}
}
AWS Certificate Manager 用于为域创建 SSL 证书。这可能需要很长时间才能申请,并且需要您使用您的电子邮件地址进行确认。
resource "aws_acm_certificate" "certificate" {
domain_name = "*.${var.root_domain_name}"
validation_method = "EMAIL"
subject_alternative_names = [ var.root_domain_name ]
}
AWS Cloudfront 用于将网站负载分配到亚马逊的边缘站点。
resource "aws_cloudfront_distribution" "www_distribution" {
/**
* The distribution's origin needs a "custom" setup in order to redirect
* traffic from <domain>.com to www.<domain>.com. The values bellow are the
* defaults.
*/
origin {
custom_origin_config {
http_port = "80"
https_port = "443"
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
/**
* This connects the S3 bucket created earlier to the Cloudfront
* distribution.
*/
domain_name = aws_s3_bucket.www.website_endpoint
origin_id = var.www_domain_name
}
enabled = true
default_root_object = "index.html"
default_cache_behavior {
viewer_protocol_policy = "redirect-to-https"
compress = true
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = var.www_domain_name
min_ttl = 0
default_ttl = 86400
max_ttl = 31536000
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
/**
* This sets the aliases of the Cloudfront distribution. Here, it is being
* set to be accessible by <var.www_domain_name>.
*/
aliases = [ var.www_domain_name ]
restrictions {
geo_restriction {
restriction_type = "none"
}
}
/**
* The AWS ACM Certificate is then applied to the distribution.
*/
viewer_certificate {
acm_certificate_arn = aws_acm_certificate.certificate.arn
ssl_support_method = "sni-only"
}
}
需要创建 Route 53 区域,以便其名称服务器可以指向 Cloudfront 分发。
resource "aws_route53_zone" "zone" {
name = var.root_domain_name
}
这是重定向到 Cloudfront Distribution 的 Route 53 记录。
resource "aws_route53_record" "www" {
zone_id = aws_route53_zone.zone.zone_id
name = var.www_domain_name
type = "A"
alias {
name = aws_cloudfront_distribution.www_distribution.domain_name
zone_id = aws_cloudfront_distribution.www_distribution.hosted_zone_id
evaluate_target_health = false
}
}
【问题讨论】:
-
全部应用后有什么问题?一目了然,我觉得很合适。
-
问题是当我在Safari中访问url时,没有页面。当我访问云前端给我的 url 时,我看到了页面。我想 Route 53 重定向存在问题。
-
没有页面因为没有加载或没有页面因为有错误?如果是这样,错误会显示什么?您是否尝试过以任何方式进行调试?
-
没有加载任何页面。没有错误,只是不加载任何东西。云前端网址效果很好。我想将 www.domain.com 重定向到云端 url。
-
您愿意/能够共享该域吗?如果没有看到您遇到的问题,这听起来很难调试。
标签: amazon-web-services terraform amazon-cloudfront