0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[小ネタ]TerraformでCloudFrontディストリビューションに代替ドメイン名を設定する際にハマったこと

Posted at

001samune.png

結論:CloudFrontディストリビューションに代替ドメインを設定するときは、Route 53のDNSレコードも必ず一緒に作成しよう

概要

Amazon Route 53 + ACM + Amazon CloudFront + Amazon S3のサーバレスウェブサイトをTerraformで作成していた時にハマったことと解決方法について備忘録として残しておきます。

画像2.png

ハマったところ

ディストリビューションの作成が全く終わらない

ディストリビューションのコードは以下の通りで、特段記載内容に問題はなく、紐づける証明書のDNS検証は終わっている状態でした。

modules/main.tf
resource "aws_cloudfront_distribution" "distribution" {
  enabled             = true
  wait_for_deployment = true
  default_root_object = "index.html"
  origin {
    origin_id   = "S3CustomOrigin"
    domain_name = var.website_endpoint
    custom_origin_config {
      http_port              = 80
      origin_protocol_policy = "http-only"
      https_port             = 443
      origin_ssl_protocols   = ["TLSv1"]
    }
  }
  default_cache_behavior {
    target_origin_id       = "S3CustomOrigin"
    viewer_protocol_policy = "redirect-to-https"
    allowed_methods        = ["GET", "HEAD"]
    cached_methods         = ["GET", "HEAD"]
    forwarded_values {
      query_string = false
      cookies {
        forward = "none"
      }
    }
  }
  custom_error_response {
    error_caching_min_ttl = 10
    error_code            = 404
    response_code         = 200
    response_page_path    = "/error.html"
  }
  aliases = [var.cloudfront_fqdn]
  viewer_certificate {
    cloudfront_default_certificate = false
    acm_certificate_arn            = var.cloudfront_cert_arn
    ssl_support_method             = "sni-only"
  }
  restrictions {
    geo_restriction {
      restriction_type = "none"
    }
  }
}

実際に terraform deploy を実行するも、プロンプトにはひたすら作成中の文字が流れ続ける。。。

module.cloudfront.aws_cloudfront_distribution.distribution: Still creating... [55m elapsed]

解決方法

代替ドメインのDNSレコードも一緒に作成する

そうですそれだけです。
ディストリビューションに設定した代替ドメインに合わせてDNSレコードも作成すればすんなり作成が完了しました。

modules/main.tf
resource "aws_cloudfront_distribution" "distribution" {
  enabled             = true
  wait_for_deployment = true
  default_root_object = "index.html"
  origin {
    origin_id   = "S3CustomOrigin"
    domain_name = var.website_endpoint
    custom_origin_config {
      http_port              = 80
      origin_protocol_policy = "http-only"
      https_port             = 443
      origin_ssl_protocols   = ["TLSv1"]
    }
  }
  default_cache_behavior {
    target_origin_id       = "S3CustomOrigin"
    viewer_protocol_policy = "redirect-to-https"
    allowed_methods        = ["GET", "HEAD"]
    cached_methods         = ["GET", "HEAD"]
    forwarded_values {
      query_string = false
      cookies {
        forward = "none"
      }
    }
  }
  custom_error_response {
    error_caching_min_ttl = 10
    error_code            = 404
    response_code         = 200
    response_page_path    = "/error.html"
  }
  aliases = [var.cloudfront_fqdn]
  viewer_certificate {
    cloudfront_default_certificate = false
    acm_certificate_arn            = var.cloudfront_cert_arn
    ssl_support_method             = "sni-only"
  }
  restrictions {
    geo_restriction {
      restriction_type = "none"
    }
  }
}

+ resource "aws_route53_record" "cloudfront_recordset" {
+   zone_id = var.public_hostzone_id
+   name    = var.cloudfront_fqdn
+   type    = "A"
+   alias {
+     name                   = aws_cloudfront_distribution.distribution.domain_name
+     zone_id                = aws_cloudfront_distribution.distribution.hosted_zone_id
+     evaluate_target_health = true
+   }
+ }

ちなみにCloudFormationでは、代替ドメインDNSレコードが無くても問題なくデプロイされました

まとめ

今回は、CloudFrontディストリビューションに代替ドメインを設定する際にハマったことを備忘録として記事にしました。
ディストリビューションの作成が完了しないケースに、ACMの検証が終わってない状態でディストリビューションに証明書を紐づけて作成しようとするパターンは知っていましたが、今回のようなケースがあるとは思いませんでした。
非常に稀なケースだと思いますが、どなたかの役に立てば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?