如何解決 AWS CloudFormation 中的「一個或多個來源或來源群組不存在」錯誤?

2 分的閱讀內容
0

當我嘗試建立或更新包含 Amazon CloudFront 分佈的 AWS CloudFormation 堆疊時,出現以下錯誤: 「您的一個或多個來源或來源群組不存在。」

簡短說明

CacheBehaviorDefaultCacheBehaviorTargetOriginId 屬性不符合 CloudFront 來源來源群組 ID。此 ID 是使用者定義的字串,可唯一識別來源或來源群組。您必須在範本中定義來源群組,並透過 CloudFormation 管理您的資源。

**提示:**最佳實務是避免在 CloudFormation 之外對堆疊資源進行變更。這可能會導致堆疊範本和堆疊資源的目前狀態之間不相符。當您更新或刪除堆疊時,不相符可能會導致錯誤。

解決方法

1.    確認 TargetOriginId 與其中一個已定義來源或來源群組的 ID 相符。輸入正確的源 ID 作為 DefaultCacheBehaviorCacheBehavior 參數。

在下列 JSON 和 YAML 範本程式碼片段範例中,由 DefaultCacheBehavior 定義並取用單一來源的 CloudFront 分佈。此外,針對身分驗證來源使用原始存取身分 (OAI)。在範例中,源 ID 設定為 my-s3-origin

JSON:

{
  "AWSTemplateFormatVersion": "2010-09-09T00:00:00.000Z",
  "Resources": {
    "cloudfrontdistribution": {
      "Type": "AWS::CloudFront::Distribution",
      "Properties": {
        "DistributionConfig": {
          "DefaultCacheBehavior": {
            "ViewerProtocolPolicy": "https-only",
            "DefaultTTL": 3600,
            "ForwardedValues": {
              "Cookies": {
                "Forward": "none"
              },
              "QueryString": true
            },
            "TargetOriginId": "my-s3-origin"
          },
          "Enabled": true,
          "Origins": [
            {
              "DomainName": "my-s3-bucket.s3.amazonaws.com",
              "Id": "my-s3-origin",
              "S3OriginConfig": {
                "OriginAccessIdentity": {
                  "Fn::Sub": "origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}"
                }
              },
              "OriginPath": "/my-content"
            }
          ]
        }
      }
    },
    "CloudFrontOriginAccessIdentity": {
      "Type": "AWS::CloudFront::CloudFrontOriginAccessIdentity",
      "Properties": {
        "CloudFrontOriginAccessIdentityConfig": {
          "Comment": {
            "Ref": "AWS::StackName"
          }
        }
      }
    }
  }
}

**注意:**將 my-s3-origin 替換為您的源 ID、將 my-s3-bucket.s3.amazonaws.com 替換為您的網域名稱,並將 /my-content 替換為您的來源路徑。

YAML:

AWSTemplateFormatVersion: 2010-09-09
Resources:
  cloudfrontdistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        DefaultCacheBehavior:
          ViewerProtocolPolicy: https-only
          DefaultTTL: 3600
          ForwardedValues:
            Cookies:
              Forward: none
            QueryString: true
          TargetOriginId: my-s3-origin
        Enabled: true
        Origins:
          - DomainName: 'my-s3-bucket.s3.amazonaws.com'
            Id: my-s3-origin
            S3OriginConfig:
              OriginAccessIdentity: !Sub origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}
            OriginPath: /my-content

  CloudFrontOriginAccessIdentity:
    Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
    Properties:
      CloudFrontOriginAccessIdentityConfig:
        Comment: !Sub ${AWS::StackName}

2.    測試您的 CloudFront 分佈以驗證您的 CloudFormation 堆疊是否已建立或更新。


相關資訊

搭配 CloudFront 分佈使用各種來源

在 AWS CloudFormation 主控台建立堆疊

AWS CloudFormation 最佳實務

AWS 官方
AWS 官方已更新 2 年前