當我嘗試使用 AWS CloudFormation 更新 AWS::CloudFront::Distribution 資源時,我收到下列錯誤: 「您的一個或多個來源或來源群組不存在。」
解決方法
CacheBehavior 或 DefaultCacheBehavior 的 TargetOriginId 必須與 AWS::CloudFront::Distribution 資源中來自 Origin 或 OriginGroups 的屬性的 Id 相符。如果 ID 不相符,則您會收到來源或****來源群組不存在的錯誤。
當您設定不存在或在 CloudFormation 之外建立之來源的 TargetOriginId 時,就會發生此錯誤。在更新作業中,CloudFormation 會刪除您在 CloudFormation 之外建立的分佈中所有來源和來源群組。如果您在 CloudFormation 嘗試刪除時使用來源或來源群組,則會收到錯誤。
**注意:**最佳實務是不要修改 CloudFormation 以外的堆疊資源。CloudFormation 以外的修改可能會在堆疊的範本與堆疊資源的目前狀態之間產生不匹配的情況。
若要解決此問題,請完成下列步驟:
- 在 CloudFormation 範本中開啟 AWS:: CloudFront::Distribution 資源。
- 請確定每個 TargetOriginId 與在 Origins 或 OriginGroups 屬性中定義的其中一個來源或來源群組的 ID 相符。如果 ID 不相符,請輸入正確的來源 ID 作為 DefaultCacheBehavior 或 CacheBehavior 的參數。
在下列範本片段範例中,由 DefaultCacheBehavior 定義並取用單一來源的 CloudFront 分佈。此外,來源使用原始存取身分 (OAI) 進行驗證,而來源是 Amazon Simple Storage Service (Amazon S3)。
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"
}
}
}
}
}
}
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}
**注意:**在先前的範例,將 my-s3-origin 替換為您的源 ID、將 my-s3-bucket.s3.amazonaws.com 替換為您的網域名稱,並將 /my-content 替換為您的來源路徑。
- 測試您的 CloudFront 分佈以驗證您的 CloudFormation 堆疊是否已建立或更新。
相關資訊
搭配 CloudFront 分佈使用各種來源
從 CloudFormation 主控台建立堆疊
AWS CloudFormation 最佳實務