当我尝试使用 AWS CloudFormation 更新 AWS::CloudFront::Distribution 资源时,收到以下错误: “One or more of your origins or origin groups do not exist.”(您的一个或多个源或源组不存在。)
解决方法
CacheBehavior 或 DefaultCacheBehavior 的 TargetOriginId 必须与 AWS::CloudFront::Distribution 资源中的 Origin 或 OriginGroups 属性中的 Id 相匹配。如果 ID 不匹配,则会收到 origins or origin groups do not exist(源或源组不存在)错误。
当您为不存在的源或在 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 最佳实践