使用 S3 网站来源的 Cloudfront 的对流层模板

Troposphere template for Cloudfront using S3 website origin

我正在尝试自动部署静态网站。我在存储桶中有一个静态网站 (angular/javascript/html),需要使用 AWS Cloudfront CDN。

   s3dnsname = self.template.add_parameter(Parameter(
        "S3DNSName",
        Description="The DNS name of an existing S3 bucket to use as the "
                    "Cloudfront distribution origin",
        Type="String",
    ))

    myDistribution = self.template.add_resource(Distribution(
        "myDistribution",
        DistributionConfig=DistributionConfig(
            Origins=[Origin(Id="Origin 1", DomainName=Ref(s3dnsname),
                            S3OriginConfig=S3Origin())],
            DefaultCacheBehavior=DefaultCacheBehavior(
                TargetOriginId="Origin 1",
                ForwardedValues=ForwardedValues(
                    QueryString=False
                ),
                ViewerProtocolPolicy="allow-all"),
            Enabled=True,
            HttpVersion='http2'

        )
    ))

这是我的代码,但出现错误 "The parameter Origin DomainName does not refer to a valid S3 bucket."

我就此调查了几个 articles/blogs,它们都指向添加以下内容:

custom_origin_config { 
    origin_protocol_policy = "http-only" 
    http_port              = 80 
    https_port             = 443 
    origin_ssl_protocols   = ["TLSv1.2", "TLSv1.1", "TLSv1"] 
} 

任何人都可以指出我需要添加这部分的地方吗?

更新: 我尝试用下面的 CustomOriginConfig 替换 S3OriginConfig,如下所示:

   myDistribution = self.template.add_resource(Distribution(
        "myDistribution",
        DistributionConfig=DistributionConfig(
            Origins=[Origin(
                Id="Origin 1",
                DomainName=Ref(s3dnsname),
                CustomOriginConfig=CustomOriginConfig(
                    OriginProtocolPolicy="http-only",
                    HTTPPort=80,
                    HTTPSPort=443))],
            DefaultCacheBehavior=DefaultCacheBehavior(
                TargetOriginId="Origin 1",
                ForwardedValues=ForwardedValues(
                    QueryString=False
                ),
                ViewerProtocolPolicy="allow-all"),
            Enabled=True,
            HttpVersion='http2'
        )
    ))

在 运行 之后,我得到一个错误 "NameError: global name 'CustomOriginConfig' is not defined"

我在这里遗漏了一些我无法识别的东西。

这是对我有用的解决方案:

from troposphere.cloudfront import S3Origin, CustomOrigin

并将代码更改为:

Origins=[Origin(Id="Origin 1", DomainName=Ref(s3dnsname),
                            CustomOriginConfig=CustomOrigin(
                                HTTPPort=80,
                                HTTPSPort=443,
                                OriginProtocolPolicy="http-only"
                            ))],

我正在使用这个例子:https://github.com/cloudtools/troposphere/blob/master/examples/CloudFront_S3.py

希望这对遇到同样问题的人有所帮助。