创建 CloudFront 分配时出错:NoSuchOrigin:

Error creating CloudFront Distribution: NoSuchOrigin:

我正在尝试使用 Terraform 部署 Cloudfront 发行版,但在指定 origin_id

时出现错误

Cloudfront 通过 Route53 查找指向负载均衡器。

resource "aws_cloudfront_distribution" "my-app" {
  origin {
    custom_origin_config {
      http_port              = 443
      https_port             = 443
      origin_protocol_policy = "https-only"
      origin_ssl_protocols   = ["TLSv1.2"]
    }

    domain_name = "${var.domain_name}"
    origin_id   = "Custom-${var.domain_name}"
  }

...

  default_cache_behavior {
    allowed_methods  = ["GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "DELETE"]
    cached_methods   = ["GET", "HEAD"]
    target_origin_id = "${local.origin_id}"

...

其中 var.domain_name 是 route53 记录,local.origin_id 是唯一 ID。

执行 terraform apply 时出现此错误:

aws_cloudfront_distribution.my-app: error creating CloudFront Distribution: NoSuchOrigin: One or more of your origins or origin groups do not exist.

文档说明:origin_id (Required) - A unique identifier for the origin. 是这样。

错误与缓存行为有关。 您需要确保 target_origin_id 与缓存行为中的 origin_id 相关。

像这样:

resource "aws_cloudfront_distribution" "my-app" {
  origin {
    custom_origin_config {
      http_port              = 443
      https_port             = 443
      origin_protocol_policy = "https-only"
      origin_ssl_protocols   = ["TLSv1.2"]
    }

    domain_name = "${var.domain_name}"
    origin_id   = "Custom-${var.domain_name}"
  }

...

  default_cache_behavior {
    allowed_methods  = ["GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "DELETE"]
    cached_methods   = ["GET", "HEAD"]
    target_origin_id = "Custom-${var.domain_name}"

...