Amazon cloudformation 使用CloudFormation使用S3源配置CloudFront

Amazon cloudformation 使用CloudFormation使用S3源配置CloudFront,amazon-cloudformation,Amazon Cloudformation,我第一次尝试使用CloudFormation来配置CloudFront发行版,该发行版使用S3 bucket作为其源 但是,当运行模板时,我收到错误您的一个或多个来源不存在。我假设这是由于源域名配置不正确造成的,但是我无法找到一个有效的配置 我目前拥有以下模板: { "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "AssetBucket": { "Type": "AWS::S3::Bucket",

我第一次尝试使用CloudFormation来配置CloudFront发行版,该发行版使用S3 bucket作为其源

但是,当运行模板时,我收到错误
您的一个或多个来源不存在
。我假设这是由于源域名配置不正确造成的,但是我无法找到一个有效的配置

我目前拥有以下模板:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "AssetBucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketName": "cdn-assets",
        "AccessControl": "PublicRead",
        "CorsConfiguration": {
          "CorsRules": [
            {
              "AllowedHeaders": [
                "*"
              ],
              "AllowedMethods": [
                "GET"
              ],
              "AllowedOrigins": [
                "*"
              ],
              "Id": "OpenCors",
              "MaxAge": "3600"
            }
          ]
        }
      }
    },
    "AssetCDN": {
      "Type": "AWS::CloudFront::Distribution",
      "Properties": {
        "DistributionConfig": {
          "Origins": [
            {
              "DomainName": {
                "Fn::GetAtt": [
                              "AssetBucket",
                              "DomainName"
                          ]
              },
              "Id": "AssetBucketOrigin",
              "S3OriginConfig": {}
            }
          ],
          "Enabled": "true",
          "DefaultCacheBehavior": {
            "Compress": true,
            "AllowedMethods": [
              "GET",
              "HEAD",
              "OPTIONS"
            ],
            "TargetOriginId": "origin-access-identity/cloudfront/AssetCDN",
            "ForwardedValues": {
              "QueryString": "false",
              "Cookies": {
                "Forward": "none"
              }
            },
            "ViewerProtocolPolicy": "allow-all"
          },
          "PriceClass": "PriceClass_All",
          "ViewerCertificate": {
            "CloudFrontDefaultCertificate": "true"
          }
        }
      },
      "DependsOn": [
        "AssetBucket"
      ]
    }
  }
}
我在这方面找不到太多建议,希望有人能给我指出正确的方向。

缓存行为的属性必须与S3源属性中指定的值匹配


在上面的示例中,
TargetOriginId
originaccessidentity/cloudfront/AssetCDN
,而
Id
assetbuckotorigin
,这导致了错误。

这里的真正问题是cloudfront有一个依赖项-S3 bucket。所以您应该将这个引用放在cloudfront对象中,让CFN知道首先它应该创建S3 bucket。为此,您必须更改Origins.Id和DefaultCacheBehavior.TargetOriginId属性,以引用您的bucket配置:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "AssetBucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketName": "cdn-assets",
        "AccessControl": "PublicRead",
        "CorsConfiguration": {
          "CorsRules": [
            {
              "AllowedHeaders": [
                "*"
              ],
              "AllowedMethods": [
                "GET"
              ],
              "AllowedOrigins": [
                "*"
              ],
              "Id": "OpenCors",
              "MaxAge": "3600"
            }
          ]
        }
      }
    },
    "AssetCDN": {
      "Type": "AWS::CloudFront::Distribution",
      "Properties": {
        "DistributionConfig": {
          "Origins": [
            {
              "DomainName": {
                "Fn::GetAtt": [
                              "AssetBucket",
                              "DomainName"
                          ]
              },
              "Id": { "Ref": "AssetBucket" }, /// HERE!!!!
              "S3OriginConfig": {}
            }
          ],
          "Enabled": "true",
          "DefaultCacheBehavior": {
            "Compress": true,
            "AllowedMethods": [
              "GET",
              "HEAD",
              "OPTIONS"
            ],
            "TargetOriginId": { "Ref": "AssetBucket" }, /// HERE!!!!
            "ForwardedValues": {
              "QueryString": "false",
              "Cookies": {
                "Forward": "none"
              }
            },
            "ViewerProtocolPolicy": "allow-all"
          },
          "PriceClass": "PriceClass_All",
          "ViewerCertificate": {
            "CloudFrontDefaultCertificate": "true"
          }
        }
      },
      "DependsOn": [
        "AssetBucket"
      ]
    }
  }
}

你找到解决办法了吗?