如何解决 AWS CloudFormation 中“您的一个或多个源或源组不存在”错误?
上次更新时间:2020 年 6 月 19 日
当我尝试创建或更新其中包含 Amazon CloudFront 分配的 AWS CloudFormation 堆栈时,我从 AWS CloudFormation 收到以下错误:“您的一个或多个源或源组不存在。” 如何解决此错误?
简短描述
当 CacheBehavior 或 DefaultCacheBehavior 的 TargetOriginId 属性与 CloudFront 源或源组 ID 不匹配时,您从 AWS CloudFormation 收到此错误。 ID 是用户定义的字符串,用于唯一地标识源或源组。
在 AWS CloudFormation 支持源组之前,您可以手动创建源组并在 TargetOriginId 上引用源组。现在,您必须在模板中定义源组并通过 AWS CloudFormation 管理您的所有资源。
提示:最佳实践是避免对 AWS CloudFormation 之外的堆栈资源进行更改。这会使您的堆栈模板与堆栈资源的当前状态不匹配。如果您更新或删除堆栈,则不匹配可能会导致错误。
解决方法
1. 要确认 TargetOriginId 与其中一个定义的源或源组的 ID 匹配,请输入正确的源 ID,作为 DefaultCacheBehavior 或 CacheBehavior 的参数。
在下面的示例 JSON 和 YAML 模板代码段中,具有单一源的 CloudFront 分配将由 DefaultCacheBehavior 定义和使用。此外,该源使用源访问身份 (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. 要验证您的 AWS CloudFormation 堆栈是否已创建或更新,请测试您的 CloudFront 分配。