当我尝试创建或更新包含 Amazon CloudFront 分配的 AWS CloudFormation 堆栈时,出现以下错误:
“One or more of your origins or origin groups do not exist.(您的一个或多个源或源组不存在。)”
简短描述
当 CacheBehavior 或 DefaultCacheBehavior 的 TargetOriginId 属性与 CloudFront 源或源组 ID 不匹配时,出现此错误。ID 是用户定义的字符串,用于唯一地标识源或源组。您必须在模板中定义源组并通过 CloudFormation 管理您的所有资源。
**提示:**最佳实践是避免对 CloudFormation 之外的堆栈资源进行更改。这会使您的堆栈模板与堆栈资源的当前状态不匹配。您更新或删除堆栈时,不匹配可能会导致错误。
解决方法
1. 确认 TargetOriginId 与定义的源或源组之一的 ID 相匹配。输入正确的源 ID 作为 DefaultCacheBehavior 或 CacheBehavior 的参数。
在下面的示例 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 最佳实践